Create and manage tables in oracle database - Constraints

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');
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
when inserting the name is empty error data:
the SQL> iNSERT INTO haha (MID) values ( 5);
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:

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);
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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));
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
insertion error data:
the SQL> iNSERT into huhu (mid, name, email ) values (2, 'kiki', '[email protected]')
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:

Primary key constraint / PRIMARY KEY / PK

The SQL> Create Table huhu (
2 MID Number Primary Key,
. 3 name VARCHAR2 (20 is) Not null);
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
inserting the correct data:
the SQL> INSERT INTO huhu (MID, name) values (. 1, 'Lisi');
into the master key is empty erroneous data:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
inserting a primary key duplicate data:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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));
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
an input primary key repeated error data:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
# 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));
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
inserting the correct data:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
insert composite primary key duplication - only when all the fields are repeated composite primary key repeat is considered
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:

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));
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
inserted beyond age limit check constraint data;
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
insertion error data gender
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:

Foreign key constraint # -

Create two tables ------ MID field associated with the two tables
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
insert data
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
number # statistics each person has a book
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
# check out each person's number, name and user name of the book there is
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
insert data:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
input error data:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
using a cascade delete functionality
to modify the database created scripts;
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
; insert data
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
until then delete the data in the parent table
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
found at this time the sub-data information table are all gone
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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:
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
insert data
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
# delete operation again
DELETE FROM MEMBER WHERE MID = 1;
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
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
Hypertext Transfer Protocol http + ssl certification --- https - between the application layer and the transport layer plus Ssl built on tcp, three characteristics:
so: Better practices:
Data tables removed in a future time, it is best to delete the child table, and then delete the parent table

Published 31 original articles · won praise 19 · views 1450

Guess you like

Origin blog.csdn.net/Alkaid__3/article/details/104309715