当前位置:网站首页>SQL learning notes (03) -- data constraint relationship
SQL learning notes (03) -- data constraint relationship
2022-07-01 09:26:00 【Yuxuan godlike】
The contents of the catalog
One 、 Entity integrity
1. Primary key constraint (primary key)
- only , No repetition , Can't be empty , There is only one primary key constraint in a table
- Fields in the primary key can be set
Automatic growth (auto_increment)
1-1. Create a primary key constraint while creating a table
Format 1 :
create table Table name (
Name 1 data type primary key,
Name 2 data type
);
create table stu(sno int(10) primary key auto_increment);
Format two : The writing of the primary key constraint name :PK_ Name
create table Table name (
Name 1 data type ,
Name 2 data type ,
constraint The name of the primary key constraint
);
create table stu(
sno int(10),
sname varchar(10),
constraint PK_name primary key(sname)
);
Format three :
create table Table name (
Name 1 data type ,
Name 2 data type ,
primary key ( Name 1)
);
create table stu(sno int(10),primary key(sno));
1-2. For existing tables , Add primary key constraint
Format 1 :
alter table Table name modity Name data type primary key;
Format two :
alter table Table name add primary key( Name );
Format three :
alter table Table name add constraint The name of the primary key constraint primary key( Name );
2. Unique constraint (unique)
- 1. It is not allowed to have
repeatValue , Guarantee the dataUniqueness. - 2. There can be
Null value - 3. In a table , There can be
MultipleUnique constraint - 4. By default , The names of unique constraints and column names remain — Cause
- 5. Add columns with unique constraints , The system will also add a... To this column by default
unique index Indexes : A catalogue equivalent to a book , In the future, it can speed up the query of data .
2-1. Create a table while creating a table — constraint (UN_ Name )
create table Table name (
Name 1 data type unique,
Name 2 data type unique,
Name 3 data type
);
create table stu(sno int(10) unique,sname varchar(20) unique,ssex varchar(10));
2-2. For existing tables , Add unique constraints
alter table Table name add unique( Name 1[, Name 2]);
2-3. Delete unique constraint
alter table Table name drop index The name of the unique constraint ;
Two 、 Domain integrity
1. Default constraint (default)
- Ensure that... Is not entered in the table
InvalidThe data of - When a column is decorated with default constraints , Even if the modified column does not write data, it will default to a value
1-1. Create default constraints while creating tables
create table Table name (
Name 1 data type default ' The default value of string type or date type ',
Name 2 data type default The number ,
Name 3 data type
);
create table student(ssex varchar(10) default ' male ',record int(10) default '70');
1-2. For existing constraints , Add default constraint
alter table Table name modify Name data type default ' The number ';
alter table student modify record int(10) default '80';
1-3. Delete default constraint
alter table Table name drop Name data type ;
alter table student drop ssex varchar(10);
2. Non empty constraint (not null) The current column must have a value
2-1. Create a non empty constraint while creating the table
create table Table name (
Name 1 data type not null,
Name 2 data type
);
create table student(sno int(10),sname varchar(20) not null);
2-2. For existing constraints , Add non empty constraints
alter table Table name modify Name data type not null;
alter table student modify ssex varchar(10) not null;
2-3. Delete non empty constraints
alter table Table name drop Name data type ;
alter table student drop ssex varchar(10);
3、 ... and 、 Referential integrity
Foreign key constraints (foreign key)
- Foreign key constraints are based on
From the table; - When the column of the secondary table refers to the column of the primary table , Column names can be different , But in the column The data type and content should be consistent ;
- Reference the columns of the main table from the table , The columns of the main table must have Primary key constraint or unique constraint ;
- When the data of the main table , Referenced from table , The data in the main table cannot be deleted ;
- When you want to operate from the data in the table , First ask the main table for advice , The main table has , Allow operations from tables , The main table doesn't have , Reject operation from table .
Create foreign key constraints while creating tables
create table Table name (
Name 1 data type ,
Name 2 data type ,
constraint The name of the foreign key constraint foreign key( From the column name of the table 1) references Main table name ( The column name of the main table 1)
);
For existing tables , Add a foreign key constraint
alter table From table name add constraint The name of the foreign key constraint foreign key( Column name from table 1) references Main table name ( Name of main table );
Delete foreign key constraint
alter table Table name drop foreign key The name of the foreign key constraint ;
边栏推荐
- dsPIC30F6014a LCD 方块显示
- The fixed assets management system enables enterprises to dynamically master assets
- Click the screen with your finger to simulate F11 and enter the full screen
- Meituan machine test in 2022
- Class loading
- Tree structure -- binary tree 2 non recursive traversal
- Wechat applet WebView prohibits page scrolling without affecting the implementation of overflow scrolling in the business
- JS原型链
- Phishing identification app
- Microcomputer principle - bus and its formation
猜你喜欢

2.2 【pytorch】torchvision.transforms

【pytorch】softmax函数

队列的实现和应用

【pytorch】nn. Crossentropyloss() and nn NLLLoss()

3D printing Arduino four axis aircraft

Principle and application of single chip microcomputer timer, serial communication and interrupt system

I use flask to write the website "one"

Tree structure -- binary tree 2 non recursive traversal

Dspic30f6014a LCD block display

Phishing identification app
随机推荐
JS variable lifting
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
2.2 【pytorch】torchvision. transforms
[interview brush 101] linked list
Learning practice: comprehensive application of cycle and branch structure (II)
Weidongshan board compilation kernel problem solving
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
LeetCode 344. Reverse string
js valueOf 与 toString 区别
JS scope chain and closure
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Analysis and solution of JS this loss
Microcomputer principle - bus and its formation
SQL学习笔记(04)——数据更新、查询操作
[video game training] real topic of 2013 video game of infrared optical communication device
Construction of esp8266 FreeRTOS development environment
Tree structure -- binary tree 2 non recursive traversal
Redis source code learning (29), compressed list learning, ziplist C (II)
3D printing Arduino four axis aircraft