Set the gender to male and female;Example: Set check Constraints
Drop table member PURGE; create table member (mid NUMBER, name VARCHAR2 (50) Not null, sex VARCHAR2 (10) Not null, age NUMBER (3), CONSTRAINT pk_mid primary key (mid), CONSTRAINT ck_sex CHECK (sex IN ('male', 'female '), CONSTRAINT ck_age CHECK (age0AND200));
Example: Add correct data
Insert into member (mid, name, sex, age) VALUES (1, 'Zhang san', 'male ','26');
Example: Add wrong gender -- ORA-02290: Violation check constraints (SCOTT. CK_SEX)
Insert into member (mid, name, sex, age) VALUES (2, 'Lee 4', 'af ','26');
Example: add the wrong age-ORA-02290: violation of check constraints (SCOTT. CK_AGE)
Insert into member (mid, name, sex, age) VALUES (2, 'Lily', 'female ','260');
The check operation is to filter the input data.
V. Primary-foreign key constraints
The preceding four constraints are implemented in a single table, while the primary-foreign key constraint is performed in two tables. The two tables have a parent-child relationship, that is: the value range of a field in the sub-table is determined by the parent table.
For example, it is now required to indicate a relationship. If each person has multiple books, two data tables should be defined: member (primary) and book (sub );
DROP TABLE member PURGE;DROP TABLE book PURGE;CREATE TABLE member( mid NUMBER, name VARCHAR2(50) NOT NULL, CONSTRAINT pk_mid PRIMARY KEY(mid));CREATE TABLE book( bid NUMBER, title VARCHAR2(50) NOT NULL, mid NUMBER, CONSTRAINT pk_bid PRIMARY KEY(bid));
In this case, two independent data tables are created as required. Insert the following data records:
Insert into member (mid, name) VALUES (1, 'Zhang san'); insert into member (mid, name) VALUES (2, 'Li si'); insert into book (bid, title, mid) VALUES (101, 'Java developer ',1); Insert into book (bid, title, mid) VALUES (102, 'Java Web developer ',2); Insert into book (bid, title, mid) VALUES (103, 'Ejb developer ',2); Insert into book (bid, title, mid) VALUES (105, 'Android developer ',1); Insert into book (bid, title, mid) VALUES (107, 'Ajax developing ',1);
The simplest way to verify whether the data is meaningful is to write two queries.
Example: count the number of books owned by each person
SELECT m.mid,m.name,COUNT(b.bid)FROM member m,book bWHERE m.mid=b.midGROUP BY m.mid,m.name;
Example: query the number, name, and name of each person who owns the book.
SELECT m.mid,m.name,b.titleFROM member m,book bWHERE m.mid=b.mid;
That is to say, the current book. mid field should be associated with the member. mid field, but the following data can also be added because there is no restriction set in this program:
Insert into book (bid, title, mid) VALUES (108, 'Photoshop user Manual ',3); Insert into book (bid, title, mid) VALUES (109, 'Flex development Manual ',8);
Now two new records are added, and the records can be saved in the data table, but the two records are meaningless because member. the content of the mid field is not 3 or 8. To solve this problem, the foreign key constraint must be used to solve the problem.
Let the value of the book. mid field be determined by member. mid. If the data of member. mid actually exists, it means it can be updated.
DROP TABLE member PURGE;DROP TABLE book PURGE;CREATE TABLE member( mid NUMBER, name VARCHAR2(50) NOT NULL, CONSTRAINT pk_mid PRIMARY KEY(mid));CREATE TABLE book( bid NUMBER, title VARCHAR2(50) NOT NULL, mid NUMBER, CONSTRAINT pk_bid PRIMARY KEY(bid), CONSTRAINT fk_mid FOREIGN KEY(mid) REFERENCES member(mid));
At this point, only a constraint is added, so if the input data is incorrect, the following prompt will appear:
ORA-02291: violating the complete constraint condition (SCOTT. FK_MID)-parent keyword not found
Because no data is specified for member. mid, if the data in book. mid is incorrect, the update operation cannot be performed.
The biggest advantage of using a foreign key is that it controls the value range of some data in the subtable, but it also brings a lot of problems;
1. When deleting data, if the data in the master table has corresponding sub-table data, it cannot be deleted;
Example: delete data with the mid value of 1 in the member table
DELETE FROM member WHERE mid=1;
Error message: ORA-02292: a complete Constraint Violation (SCOTT. FK_MID)-subrecord found ".
In this case, you can only delete the sub-Table record before deleting the parent table record:
DELETE FROM book WHERE mid=1;DELETE FROM member WHERE mid=1;
However, this operation is obviously inconvenient. If you want to delete the data in the primary table, the corresponding data in the subtable can also be deleted, you can specify a cascade delete function when setting up a foreign key constraint and modify the database creation script:
DROP TABLE member PURGE;DROP TABLE book PURGE;CREATE TABLE member( mid NUMBER, name VARCHAR2(50) NOT NULL, CONSTRAINT pk_mid PRIMARY KEY(mid));CREATE TABLE book( bid NUMBER, title VARCHAR2(50) NOT NULL, mid NUMBER, CONSTRAINT pk_bid PRIMARY KEY(bid), CONSTRAINT fk_mid FOREIGN KEY(mid) REFERENCES member(mid) ON DELETE CASCADE);
In this case, the data in the corresponding sub-table is also deleted after the data in the master table is deleted.
2. When deleting data, set the data in the subtable to null.
After the data in the primary table is deleted, you also want to set the data related items in the corresponding sub-table to null instead of deleting the data. In this case, you can continue to modify the data table creation script:
Drop table member PURGE; drop table book PURGE; create table member (mid NUMBER, name VARCHAR2 (50) Not null, CONSTRAINT pk_mid primary key (mid); create table book (bid NUMBER, title VARCHAR2 (50) Not null, mid NUMBER, CONSTRAINT pk_bid primary key (bid), CONSTRAINT fk_mid foreign key (mid) REFERENCES member (mid) on delete set null); insert into member (mid, name) VALUES (1, 'Zhang san'); insert into member (mid, name) VALUES (2, 'Li si'); insert into book (bid, title, mid) VALUES (101, 'Java developer ',1); Insert into book (bid, title, mid) VALUES (102, 'Java Web developer ',2); Insert into book (bid, title, mid) VALUES (103, 'Ejb developer ',2); Insert into book (bid, title, mid) VALUES (105, 'Android developer ',1); Insert into book (bid, title, mid) VALUES (107, 'Ajax developing ',1);
3. before deleting a parent table, you must first Delete the corresponding sub-table; otherwise, the sub-table cannot be deleted.
DROP TABLE book PURGE;DROP TABLE member PURGE;
However, this is obviously troublesome, because for an unknown database, if you want to follow this method, you must first know its parent-child relationship, therefore, Oracle provides a special mandatory table deletion operation, that is, you do not care about the constraints. When you delete a table, write "cascade constraint ".
DROP TABLE member CASCADE CONSTRAINT PURGE;DROP TABLE book CASCADE CONSTRAINT PURGE;
In this case, if you do not care whether the Sub-table exists, you can delete the parent table forcibly.
Reasonable practice: When deleting a data table in the future, it is best to delete the child table before deleting the parent table.
6. Modify Constraints
The constraint itself is also a database object, so it can certainly be modified, and as long as it is modified, the ALTER command is used. The constraint modification mainly refers to the following two operations: