Inline PRIMARY KEY & UNIQUE CONSTRAINTS
create table students (
student_id integer
constraint student_pk
primary key,
email_address varchar2(320)
constraint stud_email_u
unique,
registration_number varchar2(100)
)
Table created.
A table can only have one primary key
alter table students
add primary key ( registration_number )
ORA-02260: table can have only one primary keyMore Details: https://docs.oracle.com/error-help/db/ora-02260
A table can have many unique constraints
alter table students
add unique ( registration_number )
Table altered.
View the constraints
select constraint_name, constraint_type
from user_constraints
where table_name = 'STUDENTS'
CONSTRAINT_NAME | CONSTRAINT_TYPE | STUDENT_PK | P | STUD_EMAIL_U | U | SYS_C00114593131 | U |
---|
insert into students values ( 1, 'first.id@test.com', 'ABC123' )
1 row(s) inserted.
select * from students
STUDENT_ID | EMAIL_ADDRESS | REGISTRATION_NUMBER | 1 | first.id@test.com | ABC123 |
---|
insert into students values ( 1, 'duplicate.id@test.com', 'DEF456' )
ORA-00001: unique constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STUDENT_PK) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
insert into students values ( 2, 'first.id@test.com', 'GHI879' )
ORA-00001: unique constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STUD_EMAIL_U) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
insert into students values ( 3, 'duplicate.reg@test.com', 'ABC123' )
ORA-00001: unique constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.SYS_C00114593131) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
insert into students values ( null, 'null.id@test.com', 'JKL123' )
ORA-01400: cannot insert NULL into ("SQL_LBZXMQQATQMTXRTDILZWERGGD"."STUDENTS"."STUDENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-01400
insert into students values ( 4, null, null )
1 row(s) inserted.
insert into students values ( 5, null, null )
1 row(s) inserted.
No duplicates inserted
select * from students
STUDENT_ID | EMAIL_ADDRESS | REGISTRATION_NUMBER | 1 | first.id@test.com | ABC123 | 4 | - | - | 5 | - | - |
---|
Unique constraints are not case-sensitive
insert into students values ( 6, 'FIRST.ID@TEST.COM', 'abc123' )
1 row(s) inserted.
with rws as (
select
/* Enable case-insenstive comparison */
email_address collate binary_ci email_ci,
registration_number collate binary_ci reg_ci
from students
)
select email_ci, reg_ci, count(*)
from rws
group by email_ci, reg_ci
having count(*) > 1
EMAIL_CI | REG_CI | COUNT(*) | first.id@test.com | ABC123 | 2 | - | - | 2 |
---|
delete students
where student_id = 6
1 row(s) deleted.
Remove unique constraint
alter table students
drop unique ( email_address )
Table altered.
alter table students
add unique ( email_address collate binary_ci )
ORA-00904: : invalid identifierMore Details: https://docs.oracle.com/error-help/db/ora-00904
Case-insensitive uniqueness
create unique index stud_email_u
on students ( email_address collate binary_ci )
Index created.
Unique indexes
select index_name, index_type, uniqueness,
column_name, data_default
from user_indexes
join user_ind_columns using ( table_name, index_name )
join user_tab_cols using ( table_name, column_name )
where table_name = 'STUDENTS'
INDEX_NAME | INDEX_TYPE | UNIQUENESS | COLUMN_NAME | DATA_DEFAULT | STUDENT_PK | NORMAL | UNIQUE | STUDENT_ID | - | STUD_EMAIL_U | FUNCTION-BASED NORMAL | UNIQUE | SYS_NC00004$ | NLSSORT("EMAIL_ADDRESS" COLLATE "BINARY_CI",'nls_sort=''BINARY_CI''') | SYS_C00114593131 | NORMAL | UNIQUE | REGISTRATION_NUMBER | - |
---|
insert into students values ( 6, 'FIRST.ID@TEST.COM', 'ZZZ999' )
ORA-00001: unique constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STUD_EMAIL_U) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
drop index stud_email_u
Index dropped.
Define column as case-insenstive
alter table students
modify email_address collate binary_ci
Table altered.
alter table students
add constraint stud_email_u
unique ( email_address )
Table altered.
insert into students values ( 6, 'FIRST.ID@TEST.COM', 'abc123' )
ORA-00001: unique constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STUD_EMAIL_U) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
Indexes
select index_name, index_type, uniqueness,
column_name, data_default
from user_indexes
join user_ind_columns using ( table_name, index_name )
join user_tab_cols using ( table_name, column_name )
where table_name = 'STUDENTS'
INDEX_NAME | INDEX_TYPE | UNIQUENESS | COLUMN_NAME | DATA_DEFAULT | STUDENT_PK | NORMAL | UNIQUE | STUDENT_ID | - | STUD_EMAIL_U | FUNCTION-BASED NORMAL | UNIQUE | SYS_NC00004$ | NLSSORT("EMAIL_ADDRESS",'nls_sort=''BINARY_CI''') | SYS_C00114593131 | NORMAL | UNIQUE | REGISTRATION_NUMBER | - |
---|
Remove unique constraint
alter table students
drop unique ( registration_number )
Table altered.
Preserve the index when removing constraints
alter table students
drop primary key
keep index
Table altered.
Check the indexes
select index_name, index_type,
column_name, data_default
from user_indexes
join user_ind_columns using ( table_name, index_name )
join user_tab_cols using ( table_name, column_name )
where table_name = 'STUDENTS'
INDEX_NAME | INDEX_TYPE | COLUMN_NAME | DATA_DEFAULT | STUDENT_PK | NORMAL | STUDENT_ID | - | STUD_EMAIL_U | FUNCTION-BASED NORMAL | SYS_NC00004$ | NLSSORT("EMAIL_ADDRESS",'nls_sort=''BINARY_CI''') |
---|
USING INDEX clause
alter table students
add constraint student_pk
primary key ( student_id )
using index student_pk
Table altered.
create table courses (
course_id integer
constraint course_pk primary key,
course_code varchar2(30)
constraint cour_code_u unique
)
Table created.
begin
insert into courses values ( 1, 'SQL101' );
insert into courses values ( 2, 'PLSQL101' );
end;
Statement processed.
Foreign keys
create table student_courses (
student_id
constraint stco_student_fk
references students,
course_id
constraint stco_course_fk
references courses,
constraint student_course_pk
primary key ( student_id, course_id )
)
Table created.
select column_name, data_type, data_length, data_precision, data_scale
from user_tab_columns
where table_name = 'STUDENT_COURSES'
COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_PRECISION | DATA_SCALE | STUDENT_ID | NUMBER | 22 | - | 0 | COURSE_ID | NUMBER | 22 | - | 0 |
---|
insert into student_courses values ( 1, 1 )
1 row(s) inserted.
insert into student_courses values ( 2, 99 )
ORA-02291: integrity constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCO_COURSE_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
insert into student_courses values ( 42, 2 )
ORA-02291: integrity constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCO_STUDENT_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
select * from student_courses
STUDENT_ID | COURSE_ID | 1 | 1 |
---|
drop table student_courses
cascade constraints purge
Table dropped.
Foreign keys can reference unique constraints
create table student_courses (
email_address
constraint stco_student_fk
references students ( email_address ),
course_code
constraint stco_course_fk
references courses ( course_code ),
constraint student_course_pk
primary key ( email_address, course_code )
)
Table created.
select column_name, data_type, data_length, data_precision, data_scale
from user_tab_columns
where table_name = 'STUDENT_COURSES'
COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_PRECISION | DATA_SCALE | EMAIL_ADDRESS | VARCHAR2 | 320 | - | - | COURSE_CODE | VARCHAR2 | 30 | - | - |
---|
insert into student_courses values ( 'first.id@test.com', 'SQL101' )
1 row(s) inserted.
insert into student_courses values ( 'first.id@test.com', 'ZZZZZZ' )
ORA-02291: integrity constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCO_COURSE_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
insert into student_courses values ( 'missing@test.com', 'SQL101' )
ORA-02291: integrity constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCO_STUDENT_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
select * from student_courses
EMAIL_ADDRESS | COURSE_CODE | first.id@test.com | SQL101 |
---|
Cascading updates
update students
set email_address = 'new@test.com'
where email_address = 'first.id@test.com'
ORA-02292: integrity constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCO_STUDENT_FK) violated - child record found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02292
Reset table so FKs point to PKs
drop table student_courses
cascade constraints purge
Table dropped.
create table student_courses (
student_id
constraint stco_student_fk
references students,
course_id
constraint stco_course_fk
references courses,
constraint student_course_pk
primary key ( student_id, course_id )
)
Table created.
insert into student_courses values ( 1, 1 )
1 row(s) inserted.
update students
set email_address = 'new@test.com'
where email_address = 'first.id@test.com'
1 row(s) updated.
Multi-column foreign keys
create table student_course_results (
student_course_result_id integer
constraint student_course_result_pk
primary key,
student_id ,
course_id ,
exam_id integer,
percent_correct number,
constraint stcr_student_course_fk
foreign key ( student_id, course_id )
references student_courses,
unique ( student_id, course_id, exam_id )
)
Table created.
Beware optional multicolumn FKs!
insert into student_course_results values ( 1, 99, null, 1, 0 )
1 row(s) inserted.
Orphaned values in optional multicolumn FKs
select * from student_course_results stcr
where not exists (
select * from student_courses stco
where stco.student_id = stcr.student_id
)
STUDENT_COURSE_RESULT_ID | STUDENT_ID | COURSE_ID | EXAM_ID | PERCENT_CORRECT | 1 | 99 | - | 1 | 0 |
---|
truncate table student_course_results
Table truncated.
Not null check constraints
alter table student_course_results
add constraint stcr_course_nn
check ( course_id is not null )
Table altered.
alter table student_course_results
add constraint stcr_student_nn
student_id integer not null
ORA-01430: column being added already exists in tableMore Details: https://docs.oracle.com/error-help/db/ora-01430
NOT NULL constraints
alter table student_course_results
modify (
student_id constraint stcr_student_nn not null
)
Table altered.
select constraint_name, constraint_type, search_condition
from user_constraints
where table_name = 'STUDENT_COURSE_RESULTS'
and constraint_type = 'C'
CONSTRAINT_NAME | CONSTRAINT_TYPE | SEARCH_CONDITION | STCR_COURSE_NN | C | course_id is not null | STCR_STUDENT_NN | C | "STUDENT_ID" IS NOT NULL |
---|
insert into student_course_results values ( 1, 99, null, 1, 0 )
ORA-02290: check constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_COURSE_NN) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
insert into student_course_results values ( 2, null, 99, 1, 0 )
ORA-01400: cannot insert NULL into ("SQL_LBZXMQQATQMTXRTDILZWERGGD"."STUDENT_COURSE_RESULTS"."STUDENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-01400
select column_name, nullable
from user_tab_columns
where table_name = 'STUDENT_COURSE_RESULTS'
and column_name in ( 'STUDENT_ID', 'COURSE_ID' )
COLUMN_NAME | NULLABLE | STUDENT_ID | N | COURSE_ID | Y |
---|
select * from student_course_results
where student_id is null
no data found
NOT NULL optimization
select *
from dbms_xplan.display_cursor(:LIVESQL_LAST_SQL_ID, null, 'TYPICAL LAST +PREDICATE')
PLAN_TABLE_OUTPUT | SQL_ID 14u9w893qdv0f, child number 0 | ------------------------------------- | select * from student_course_results where student_id is null | Plan hash value: 4267664529 | --------------------------------------------------------------------------------------------- | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | --------------------------------------------------------------------------------------------- | | 0 | SELECT STATEMENT | | | | 1 (100)| | | |* 1 | FILTER | | | | | | | | 2 | TABLE ACCESS FULL| STUDENT_COURSE_RESULTS | 1 | 65 | 2 (0)| 00:00:01 | | --------------------------------------------------------------------------------------------- | Predicate Information (identified by operation id): | --------------------------------------------------- | 1 - filter(NULL IS NOT NULL) | Note | ----- | - dynamic statistics used: dynamic sampling (level=2) |
---|
select * from student_course_results
where course_id is null
no data found
..but not for CHECK NOT NULL
select *
from dbms_xplan.display_cursor(:LIVESQL_LAST_SQL_ID, null, 'TYPICAL LAST +PREDICATE')
PLAN_TABLE_OUTPUT | SQL_ID gx91bmy0z7phz, child number 0 | ------------------------------------- | select * from student_course_results where course_id is null | Plan hash value: 536234621 | -------------------------------------------------------------------------------------------- | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | -------------------------------------------------------------------------------------------- | | 0 | SELECT STATEMENT | | | | 2 (100)| | | |* 1 | TABLE ACCESS FULL| STUDENT_COURSE_RESULTS | 1 | 65 | 2 (0)| 00:00:01 | | -------------------------------------------------------------------------------------------- | Predicate Information (identified by operation id): | --------------------------------------------------- | 1 - filter("COURSE_ID" IS NULL) | Note | ----- | - dynamic statistics used: dynamic sampling (level=2) |
---|
Remove NOT NULL constraint
alter table student_course_results
modify ( student_id null )
Table altered.
Remove CHECK constraint
alter table student_course_results
drop constraint stcr_course_nn
Table altered.
Multi-column optionality
alter table student_course_results
add constraint stcr_student_course_both_neither_nn
check (
coalesce ( student_id, course_id ) is null
or ( student_id is not null and course_id is not null )
)
Table altered.
insert into student_course_results values ( 1, 1, 1, 1, 0 )
1 row(s) inserted.
insert into student_course_results values ( 2, null, null, 1, 0 )
1 row(s) inserted.
insert into student_course_results values ( 3, 99, null, 1, 0 )
ORA-02290: check constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_STUDENT_COURSE_BOTH_NEITHER_NN) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
insert into student_course_results values ( 4, null, 99, 1, 0 )
ORA-02290: check constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_STUDENT_COURSE_BOTH_NEITHER_NN) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
select * from student_course_results
STUDENT_COURSE_RESULT_ID | STUDENT_ID | COURSE_ID | EXAM_ID | PERCENT_CORRECT | 1 | 1 | 1 | 1 | 0 | 2 | - | - | 1 | 0 |
---|
alter table student_course_results
add constraint stcr_percent_correct_0_to_100
check ( percent_correct between 0 and 100 )
Table altered.
truncate table student_course_results
Table truncated.
insert into student_course_results values ( 1, 1, 1, 2, -9999 )
ORA-02290: check constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_PERCENT_CORRECT_0_TO_100) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
insert into student_course_results values ( 2, 1, 1, 3, 9999 )
ORA-02290: check constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_PERCENT_CORRECT_0_TO_100) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
insert into student_course_results values ( 3, 1, 1, 4, null )
1 row(s) inserted.
Law of excluded middle failure
select * from student_course_results
where percent_correct between 0 and 100
or percent_correct not between 0 and 100
no data found
select * from student_course_results
STUDENT_COURSE_RESULT_ID | STUDENT_ID | COURSE_ID | EXAM_ID | PERCENT_CORRECT | 3 | 1 | 1 | 4 | - |
---|
select * from student_course_results
where percent_correct < 0
no data found
Check constraint optimizations
select *
from dbms_xplan.display_cursor(:LIVESQL_LAST_SQL_ID, null, 'TYPICAL LAST +PREDICATE')
PLAN_TABLE_OUTPUT | SQL_ID c5wmsf5u91k9s, child number 0 | ------------------------------------- | select * from student_course_results where percent_correct < 0 | Plan hash value: 4267664529 | --------------------------------------------------------------------------------------------- | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | --------------------------------------------------------------------------------------------- | | 0 | SELECT STATEMENT | | | | 1 (100)| | | |* 1 | FILTER | | | | | | | |* 2 | TABLE ACCESS FULL| STUDENT_COURSE_RESULTS | 1 | 65 | 3 (0)| 00:00:01 | | --------------------------------------------------------------------------------------------- | Predicate Information (identified by operation id): | --------------------------------------------------- | 1 - filter(NULL IS NOT NULL) | 2 - filter("PERCENT_CORRECT"<0) | Note | ----- | - dynamic statistics used: dynamic sampling (level=2) |
---|
Reset table
drop table student_course_results
cascade constraints purge
Table dropped.
create table student_course_results (
student_id integer,
course_id integer,
exam_id integer,
percent_correct number
)
Table created.
insert into student_course_results
select student_id, course_id,
1, round ( dbms_random.value ( -100, 1000 ) )
from students
cross join courses
/* Insert duplicates */
cross join ( select * from dual connect by level <= 2 )
12 row(s) inserted.
select * from student_course_results
STUDENT_ID | COURSE_ID | EXAM_ID | PERCENT_CORRECT | 1 | 1 | 1 | 49 | 1 | 2 | 1 | 588 | 1 | 1 | 1 | 555 | 1 | 2 | 1 | 573 | 4 | 1 | 1 | 513 | 4 | 2 | 1 | 180 | 4 | 1 | 1 | 627 | 4 | 2 | 1 | 519 | 5 | 1 | 1 | 460 | 5 | 2 | 1 | 252 | 5 | 1 | 1 | 275 | 5 | 2 | 1 | 56 |
---|
alter table student_course_results
add constraint stcr_percent_c
check ( percent_correct between 0 and 100 )
ORA-02293: cannot validate (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_PERCENT_C) - check constraint violatedMore Details: https://docs.oracle.com/error-help/db/ora-02293
Adding constraints is a blocking operation
declare
procedure add_constraint as
pragma autonomous_transaction;
begin
execute immediate '
alter table student_course_results
add constraint stcr_percent_c
check ( percent_correct between 0 and 100 )
';
end ;
begin
insert into student_course_results
values ( 1, 1, 1, 999 );
add_constraint();
end;
ORA-00060: deadlock detected while waiting for resource ORA-06512: at line 5 ORA-06512: at line 15 ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00060
NOVALIDATE constraints
alter table student_course_results
add constraint stcr_percent_c
check ( percent_correct between 0 and 100 )
novalidate
Table altered.
insert into student_course_results
values ( 1, 1, 1, 9999 )
ORA-02290: check constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STCR_PERCENT_C) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
select * from student_course_results
where percent_correct < 0
no data found
Fix the invalid data
update student_course_results
set percent_correct = 0
where percent_correct not between 0 and 100
10 row(s) updated.
You can now VALIDATE the constraint
alter table student_course_results
modify constraint stcr_percent_c
validate
Table altered.
select * from student_course_results
where percent_correct < 0
no data found
Adding a NOVALIDATE NOT NULL
alter table student_course_results
modify student_id
constraint stcr_student_nn not null
novalidate
Table altered.
Validating NOT NULL constraints
alter table student_course_results
modify constraint stcr_student_nn
validate
Table altered.
NOVALIDATE Primary key
alter table student_course_results
add constraint student_course_result_pk
primary key (
student_id, course_id, exam_id
)
novalidate
ORA-02437: cannot validate (SQL_LBZXMQQATQMTXRTDILZWERGGD.STUDENT_COURSE_RESULT_PK) - primary key violatedMore Details: https://docs.oracle.com/error-help/db/ora-02437
NOVALIDATE Primary key
alter table student_course_results
add constraint student_course_result_pk
primary key (
student_id, course_id, exam_id
)
using index (
/* Can create new index or use existing here */
create index stcr_student_course_date_i
on student_course_results (
student_id, course_id, exam_id
)
)
novalidate
Table created.
insert into student_course_results values ( 5, 5, 5, 1 )
1 row(s) inserted.
insert into student_course_results values ( 5, 5, 5, 99 )
ORA-00001: unique constraint (SQL_LBZXMQQATQMTXRTDILZWERGGD.STUDENT_COURSE_RESULT_PK) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
Remove the duplicate rows
delete student_course_results
where rowid not in (
select min ( rowid )
from student_course_results
group by student_id, course_id, exam_id
)
6 row(s) deleted.
alter table student_course_results
modify constraint student_course_result_pk
validate
Table altered.
CTAS constraints
create table student_course_results_backup as
select * from student_course_results
Table created.
select * from student_courses
STUDENT_ID | COURSE_ID | 1 | 1 |
---|
select constraint_name, table_name, constraint_type, search_condition
from user_constraints
where table_name like 'STUDENT_COURSE_RESULT%'
order by 1, 2
CONSTRAINT_NAME | TABLE_NAME | CONSTRAINT_TYPE | SEARCH_CONDITION | STCR_PERCENT_C | STUDENT_COURSE_RESULTS | C | percent_correct between 0 and 100 | STCR_STUDENT_NN | STUDENT_COURSE_RESULTS | C | "STUDENT_ID" IS NOT NULL | STUDENT_COURSE_RESULT_PK | STUDENT_COURSE_RESULTS | P | - | SYS_C00114593165 | STUDENT_COURSE_RESULTS_BACKUP | C | "STUDENT_ID" IS NOT NULL |
---|
Defining constraints with CTAS
create table student_course_results_backup_constraints (
student_id,
course_id,
exam_id not null ,
percent_correct check ( percent_correct between 0 and 100 ),
primary key ( student_id, course_id, exam_id )
) as
select * from student_course_results
where nvl ( percent_correct, -1 ) between 0 and 100
Table created.
select constraint_name, table_name, constraint_type, search_condition
from user_constraints
where table_name = 'STUDENT_COURSE_RESULTS_BACKUP_CONSTRAINTS'
order by 1, 2
CONSTRAINT_NAME | TABLE_NAME | CONSTRAINT_TYPE | SEARCH_CONDITION | SYS_C00114593166 | STUDENT_COURSE_RESULTS_BACKUP_CONSTRAINTS | C | "STUDENT_ID" IS NOT NULL | SYS_C00114593167 | STUDENT_COURSE_RESULTS_BACKUP_CONSTRAINTS | C | "EXAM_ID" IS NOT NULL | SYS_C00114593168 | STUDENT_COURSE_RESULTS_BACKUP_CONSTRAINTS | C | percent_correct between 0 and 100 | SYS_C00114593169 | STUDENT_COURSE_RESULTS_BACKUP_CONSTRAINTS | P | - |
---|