The fourth module mysql python learning _ integrity constraints
not null与default
unique
primary key
auto_increment
foreign key
An introduction
Constraints width data type, are optional parameters
Role: to ensure data integrity and consistency
are divided into:
Primary Key - the identifier of the field for the table's primary key can uniquely identify a record Foreign Key - identify the foreign key fields for table Not null - identifier of the field can not be blank UNIQUE Key - the value of the identification field is the only AUTO_INCREMENT - value of the identification field of the automatic growth (primary key and an integer type) default - set the field to the default value unsigned - unsigned ZEROFILL - filled with 0s
Two not null and default
create table tb11( id int not null default 2, num int not null );
mysql> desc tb11; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | | 2 | | | num | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+-------+
mysql> insert into tb11(num) values(21); Query OK, 1 row affected (0.00 sec) mysql> select * from tb11; +----+-----+ | id | num | +----+-----+ | 2 | 21 | +----+-----+
mysql> create table student( -> name char(20) not null, -> age int(3) unsigned not null default 18, -> sex enum('male','female') default 'male', -> hobby set('play','study','read','music') default 'play,music' -> ); Query OK, 0 rows affected (0.04 sec) mysql> insert into student(name) values('egon'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +------+-----+------+------------+ | name | age | sex | hobby | +------+-----+------+------------+ | egon | 18 | male | play,music | +------+-----+------+------------+ 1 row in set (0.00 sec)
三 unique
1.设置唯一约束 UNIQUE
-- 方法一 create table tt1( id int, name char(20) unique, comment char(20) ); -- 方法二 create table tt2( id int, name char(20), comment char(20), unique(name) );
create table tt11( id int not null unique); 等于 create table tt12( id int primary key );
2. Set a unique joint
mysql> create table service( -> id int primary key auto_increment, -> name char(20), -> host varchar(15) not null, -> port int not null, -> unique(host,port) -> ); Query OK, 0 rows affected (0.03 sec) mysql> insert into service values -> (1,'nginx','192.168.2.1',80), -> (2,'haproxy','192.168.2.2',80), -> (3,'mysql','192.168.2.3',3306) -> ; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> insert into service(name,host,port) values('nginx','192.168.2.1',80); ERROR 1062 (23000): Duplicate entry '192.168.2.1-80' for key 'host'
四 primary key
Primary key field value is not null and unique
A table can be
single primary key is
multi-column as a primary key (composite primary key)
But within a table can have only one primary key primary key
1. The primary key is a single column
- Method a null + UNIQUE Not Create Table TA1 ( ID int Not null UNIQUE , name VARCHAR ( 20 is ), Comment VARCHAR ( 100 ) ); - Method Two Key Primary Create Table TA2 ( ID int Primary Key , name VARCHAR ( 20 is ), Comment VARCHAR ( 100 ) ); - method three defined primary key separately after all fields Create Table ta3( id int , name varchar(20), comment varchar(100), primary key(id) );
More than two columns as a primary key
create table service( ip varchar(15), port char(5), service_name varchar(10) not null, primary key(ip,port) );
Five auto_increment
Field is automatically constrained growth, constrained key field must be constrained
1. Not specified id, automatically increase
2 to specify ID
3. For the self-energizing field, after deleting a delete, insert the value of the field is still in accordance with position before deleting continue to grow
4. empty tables truncate truncate table tb11;
create table student( id int primary key auto_increment, name varchar(20), sex enum('male','female') default 'male' );
Six foreign key
# 1 , establishing relationships between tables: # first build table is associated, and to ensure that the field is associated with only Create Table DEP ( ID int Primary Key , name char ( 16 ), Comment char ( 50 ) ); # re-association table Create Table EMP ( ID int Primary Key , name char ( 10 ), Sex enum ( ' MALE ' , ' FEMALE ' ), the dep_id int , Foreign Key (the dep_id) References DEP (ID) ON Delete Cascade ON Update Cascade ); # 2 , insert data # prior to the association table inserted into the recording INSERT INTO DEP values ( . 1 , "the IT", "technical capacity limited sector "), ( 2 ," sales "," lack of capacity sales department "), ( 3 ," Finance "," money special multi-sectoral "); # Beyond associated table to insert records iNSERT INTO emp values ( 1 , ' Egon ' ,'male',1); insert into emp values (2,'alex','male',1), (3,'wupeiqi','female',2), (4,'yuanhao','male',3), (5,'jinximn','male',2);