Restrict only '1' and '0' value in sql - sql

I'm currently working in Oracle SQL and I am trying to constrain the integer datatype to only take the values 0 and 1 (for an active item or not).
This is what I currently have:
CREATE TABLE ACTIVE_EX (
ACTIVE int NOT NULL
CONSTRAINT check_active_ind CHECK (ACTIVE = 0 OR ACTIVE = 1)
);
SELECT * FROM ACTIVE_EX;
INSERT INTO ACTIVE_EX(ACTIVE)values(2);
When I enter this command and run the Select * from ACTIVE_EX line, the value 2 will still be entered into the table with no error.

How about this?
SQL> create table test
2 (active int not null
3 constraint ch_active check (active in (0, 1))
4 );
Table created.
SQL>
SQL> insert into test values (-1);
insert into test values (-1)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_ACTIVE) violated
SQL> insert into test values (2);
insert into test values (2)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_ACTIVE) violated
SQL> insert into test values (100);
insert into test values (100)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_ACTIVE) violated
SQL> insert into test values (0.3);
1 row created.
SQL> insert into test values (1.2);
1 row created.
SQL> insert into test values (3.8);
insert into test values (3.8)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_ACTIVE) violated
SQL> select * From test;
ACTIVE
----------
0
1
SQL>
As you can see, decimal values between 0 and 1 can be entered, but will be rounded so the final result will still be 0 or 1.

Related

How to add a BIT column in SQL

I want to add a BIT column to a table. I have provided the statement to do so, but an error declares the BIT in my statement as a invalid datatype. What would be the correct way to add a bit column?
ALTER TABLE Persons
ADD is_person BIT NULL
[You should specify database you use; this is Oracle example, see if it helps].
There's no such a datatype in Oracle, so you'd use NUMBER and constrain it:
SQL> create table persons
2 (id number,
3 bit number(1,0) check (bit in (0, 1)) --> this
4 );
Table created.
A few examples:
SQL> insert into persons (id, bit) values (1, 2);
insert into persons (id, bit) values (1, 2)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C008434) violated
SQL> insert into persons (id, bit) values (1, 1);
1 row created.
SQL> insert into persons (id, bit) values (1, 0);
1 row created.
SQL> insert into persons (id, bit) values (1, 10);
insert into persons (id, bit) values (1, 10)
*
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column
SQL>
Or, if you'd want to add it (as table already exists), then
alter table persons add bit number(1, 0) check (bit in (0, 1));

Want to create a database column with following checks on oracle database sql

Column should follow below validation needs to be implemented with constraints in oracle db
Should Be Only in Upper Case
Should Not Contain Leading and Trailing Spaces
Should Not Contain Extra Spaces Other Than One Space Between The Words
Should Not Contain Special Characters and Numbers Anywhere in The Text
See if this helps.
SQL> create table test
2 (col varchar2(20),
3 --
4 constraint ch1_upper check (col = upper(col)),
5 --
6 constraint ch2_letraspc check (col = trim(col)),
7 --
8 constraint ch3_wrdspc check (regexp_like(col, '^ *(\w+ ?)+ *$')),
9 --
10 constraint ch4_spec check (regexp_like(col, '^[[:alpha:] ]+$'))
11 );
Table created.
Testing:
SQL> insert into test values ('abc');
insert into test values ('abc')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH1_UPPER) violated
SQL> insert into test values (' DEF');
insert into test values (' DEF')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH2_LETRASPC) violated
SQL> insert into test values ('DEF ');
insert into test values ('DEF ')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH2_LETRASPC) violated
SQL> insert into test values (' DEF ');
insert into test values (' DEF ')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH2_LETRASPC) violated
SQL> insert into test values ('GHI23');
insert into test values ('GHI23')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH4_SPEC) violated
SQL> insert into test values ('GHI#');
insert into test values ('GHI#')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH4_SPEC) violated
SQL> insert into test values ('GHI JKL');
insert into test values ('GHI JKL')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH3_WRDSPC) violated
SQL> insert into test values ('GHI JKL MNO');
insert into test values ('GHI JKL MNO')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH3_WRDSPC) violated
SQL> insert into test values ('GHI JKL MNO');
1 row created.
SQL>

Is there any difference on a table having PK and having and UNIQUE constraint

In TOAD, I can see my table said doesn't have PK. But there is an unique constraint for the PK candidate.
I try to make the field PK but toad said there is already a constraint for it on the table.
And can't remove the constraint because said someone else depend on it.
So should I leave it like that. Or go the extra mile disable all dependencies remove the unique constraint and create a PK?
Let me try to explain it. As Eric said, unique key constraint will accept (many) nulls for the constrained column.
First, create a table with unique key constraint (the one you have now):
SQL> create table test (id number constraint uk_test unique, --> unique key constraint
2 name varchar2(20));
Table created.
SQL> -- This is the first record - no problem with it
SQL> insert into test (id, name) values (1, 'Little');
1 row created.
SQL> -- Uniqueness violated
SQL> insert into test (id, name) values (1, 'Foot');
insert into test (id, name) values (1, 'Foot')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_TEST) violated
SQL> -- Let's insert some NULL values into the constrained column
SQL> insert into test (id, name) values (null, 'Foot');
1 row created.
SQL> insert into test (id, name) values (null, 'Big');
1 row created.
SQL> -- The result: not too pretty, eh?
SQL> select * From test;
ID NAME
---------- --------------------
1 Little
Foot
Big
A step further: apply a new, NOT NULL constraint to the unique key column, so that it "acts" as if it were a primary key:
SQL> delete from test;
3 rows deleted.
SQL> -- add NOT NULL constraint
SQL> alter table test modify id not null;
Table altered.
SQL> -- The first record is OK
SQL> insert into test (id, name) values (1, 'Little');
1 row created.
SQL> -- Uniqueness violated
SQL> insert into test (id, name) values (1, 'Foot');
insert into test (id, name) values (1, 'Foot')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_TEST) violated
SQL> -- This worked previously, but won't any longer because of the NOT NULL constraint
SQL> insert into test (id, name) values (null, 'Foot');
insert into test (id, name) values (null, 'Foot')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."ID")
Finally, to show that now it acts as if it were a primary key constraint:
SQL> delete from test;
1 row deleted.
SQL> -- Let's drop the unique key constraint
SQL> alter table test drop constraint uk_test;
Table altered.
SQL> -- Add the primary key constraint (no duplicates, no nulls)
SQL> alter table test add constraint pk_test primary key (id);
Table altered.
SQL> -- The first record is OK
SQL> insert into test (id, name) values (1, 'Little');
1 row created.
SQL> -- Uniqueness violated
SQL> insert into test (id, name) values (1, 'Foot');
insert into test (id, name) values (1, 'Foot')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.PK_TEST) violated
SQL> -- Not null violated
SQL> insert into test (id, name) values (null, 'Foot');
insert into test (id, name) values (null, 'Foot')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."ID")
Basically, now you got the same errors as previously, i.e. primary key = unique key + NOT NULL.
You can't create a primary key if the column is already constrained by the unique key - you already know that.
As you can't drop the unique key constraint (because foreign keys reference it), apply the NOT NULL constraint to that column.
Alternatively,
drop all foreign key constraints
drop unique key
create primary key
recreate foreign key constraints
You can replace the UNIQUE constraint for a PK in three easy steps:
Just create the PK with the same column(s).
Then change all FKs to make them point to the PK constraint instead of the UNIQUE constraint.
Finally, drop the UNIQUE constraint.
Easy.

Add constraint unique column with null value

I need to create a constraint of integrity that ensures that in a column of a table there is only one null valor. The classic UNIQUE is not good because it does not detect multiple nulls ...
how can I do?
CREATE TABLE qwe(
id int
);
CREATE UNIQUE INDEX qwe_idx ON qwe(
CASE WHEN id IS null THEN 'NULL' ELSE to_char(id) END
);
INSERT INTO qwe VALUES(1);
1 row inserted.
INSERT INTO qwe VALUES(1);
Error starting at line : 9 in command -
INSERT INTO qwe VALUES(1)
Error report -
ORA-00001: naruszono więzy unikatowe (TEST.QWE_IDX)
INSERT INTO qwe VALUES(NULL);
1 row inserted.
INSERT INTO qwe VALUES(NULL);
Error starting at line : 9 in command -
INSERT INTO qwe VALUES(NULL)
Error report -
ORA-00001: naruszono więzy unikatowe (TEST.QWE_IDX)
SELECT * FROM qwe;
ID
----------
1
(null)

PL/SQL developer how to get the row that made the insert fail?

I'm doing a method that inserts into the table which has a unique column. What I don't know is if I can access the insert value that made the insert fail.
For example:
table1(id,name, phone);
name is unique.
insert (1,a,123);
insert (2,a,1234);
What I want is when I do the second insert I to return the id value '1' without having to recur to a query.
Thank you in advance.
From oracle 10g r2 you can use log errors clause of insert command to log errors in a separate table. Here is an example:
SQL> create table test_table(
2 id number primary key,
3 col1 varchar2(7)
4 )
5 ;
Table created
-- creates a table for logging errors (table name will be prefaced with err$_)
SQL> begin dbms_errlog.create_error_log('TEST_TABLE'); end;
2 /
PL/SQL procedure successfully completed
-- violates primary key constraint
SQL> insert into test_table(id, col1)
2 ( select 1, level
3 from dual
4 connect by level <= 3)
5 log errors reject limit unlimited;
1 row inserted
SQL> commit;
SQL> select * from test_table;
ID COL1
---------- -------
1 1
SQL> select * from err$_test_table;
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_ROWID$ ORA_ERR_OPTYP$ ORA_ERR_TAG$ ID COL1
--------------- ------------------------------------------------------------------------------------------------------------
1 ORA-00001: unique constraint (HR.SYS_C008315) violated I 1 2
1 ORA-00001: unique constraint (HR.SYS_C008315) violated I 1 3
maybe you can write a trigger(before insert) on your table, on which insert about to happen. In this you can check if the column value(name) already exists in table.
In case it does you may insert this duplicate record in another table for further reference
Another approach is to write the insert in a procedure where the name may be checked and the duplicate name could be stored in a table.
Hope it helps