Conditional filtering insert the data - Constraints
After the table is created, and can not check whether the data in the table lawful, if you want to do some filtering for the data in the table, it can be done through constraint, the constraint is the main function of the data to ensure the legitimacy of the table, in accordance with the constraints classification, a total of five constraints:
non-null constraint
The only constraint
primary key constraint
check constraint
foreign key constraint
Non-empty constraint / NOT NULL / NK
The SQL> Create Table haha (
2 MID Number,
. 3 name VARCHAR2 (20 is) Not null);
insert data:
the SQL> INSERT INTO haha (MID, name) values (. 1, 'zhangsan');
the SQL> INSERT INTO haha (MID, name) values (. 1, 'zhangsan');
the SQL> iNSERT INTO haha (MID, name) values (. 3, 'Lala');
when inserting the name is empty error data:
the SQL> iNSERT INTO haha (MID) values ( 5);
The only constraint / UNIQUE / UK
Create a unique constraint table
the SQL> Create Table huhu (
2 MID Number,
. 3 name VARCHAR2 (20 is) Not null,
. 4 In Email VARCHAR2 (20 is) UNIQUE);
inserting the correct data
SQL> insert into huhu (mid, name, email) values ( . 1, 'xixi', '[email protected]');
the SQL> iNSERT INTO huhu (MID, name, email) values (2, 'Kiki', '[email protected]');
insertion error of duplicate data email
In order to make the error more clear :
add a constraint:
the SQL> Create Table huhu (
2 MID Number,
. 3 name VARCHAR2 (20 is),
. 4 In Email VARCHAR2 (20 is),
. 5 constraint uk_email UNIQUE (In Email));
insertion error data:
the SQL> iNSERT into huhu (mid, name, email ) values (2, 'kiki', '[email protected]')
Primary key constraint / PRIMARY KEY / PK
The SQL> Create Table huhu (
2 MID Number Primary Key,
. 3 name VARCHAR2 (20 is) Not null);
inserting the correct data:
the SQL> INSERT INTO huhu (MID, name) values (. 1, 'Lisi');
into the master key is empty erroneous data:
inserting a primary key duplicate data:
add a constraint, making error clearer :
the SQL> Create Table huhu (
2 MID Number,
. 3 name VARCHAR2 (20 is) Not null,
. 4 constraint pk_mid primary key (MID));
an input primary key repeated error data:
# Create composite primary key
the SQL> Create Table huhu (
2 MID Number,
. 3 name VARCHAR2 (20 is) Not null,
. 4 constraint pk_mid_name primary key (MID, name));
inserting the correct data:
insert composite primary key duplication - only when all the fields are repeated composite primary key repeat is considered
Check constraint / CHECK / CK
The SQL> Create Table huhu (
2 MID Number,
. 3 name VARCHAR2 (20 is) Not null,
. 4 Sex VARCHAR2 (20 is) Not null,
. 5 Age Number (. 3),
. 6 constraint pk_mid Primary Key (MID),
. 7 constraint ck_sex Check (Sex in ( 'NaN3', 'NV', 'qita')),
. 8 constraint ck_age check (Age BETWEEN 0 and 100));
inserted beyond age limit check constraint data;
insertion error data gender
Foreign key constraint # -
Create two tables ------ MID field associated with the two tables
insert data
number # statistics each person has a book
# check out each person's number, name and user name of the book there is
at this time if the table in insert mid = 3/4 ,,, not in the parent table xixi mid range within the data table, directly into the still not given, thus causing pollution of the data
thus recreate the two table and add foreign key constraints:
insert data:
input error data:
since 3 and 9 data member.mid not specified, so book.mid data if there is an error, you can not perform update operations, let book.mid realized the value of the field is determined by the member.mid, if member .mid real existence, it indicates that normal data can be updated
1, resulting in a foreign key constraint when you delete the table, only to delete the word data in the table to delete data in the parent table
using a cascade delete functionality
to modify the database created scripts;
; insert data
until then delete the data in the parent table
found at this time the sub-data information table are all gone
after 2, this time due to the cascade delete operation, the data in the main table to delete , Data corresponding to the child table will also be deleted.
When deleting data in the main table, the data table corresponding to the sub-items related to desired set to null, instead of deleting. Can continue to modify the script to create the database:
insert data
# delete operation again
DELETE FROM MEMBER WHERE MID = 1;
at this time appears to be empty, rather than no data
3, can only delete the word table when you delete the table, and then delete the parent table
Solution:
# forcibly remove the table, no longer concerned about the constraints
DROP TABLE MEMBER CASCADE CONSTRAINT PURGE;
but sub-table book is still
so: Better practices:
Data tables removed in a future time, it is best to delete the child table, and then delete the parent table