当前位置:网站首页>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 ;
边栏推荐
- Vsync+ triple cache mechanism +choreographer
- Mysql8.0 learning record 17 -create table
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- The fixed assets management system enables enterprises to dynamically master assets
- ES6 decoupling top-level objects from windows
- [pytorch learning] torch device
- 3D打印Arduino 四轴飞行器
- SDN_简单总结
- How to realize the usage of connecting multiple databases in idel
- Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
猜你喜欢

nacos简易实现负载均衡

How to launch circle of friends marketing and wechat group activities

Error org apache. catalina. core. StandardContext. FilterStart start filter exception

Mysql 优化

3D打印Arduino 四轴飞行器

Bird recognition app

Jetson nano installs tensorflow GPU and problem solving

Why is the Ltd independent station a Web3.0 website!

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据

MySQL optimization
随机推荐
Learning practice: comprehensive application of cycle and branch structure (II)
NiO zero copy
Football and basketball game score live broadcast platform source code /app development and construction project
Niuke monthly race 22 tree sub chain
js 使用toString 区分Object、Array
[interview brush 101] linked list
【pytorch】nn.AdaptiveMaxPool2d
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
es6-顶层对象与window的脱钩
类加载
Short circuit operator lazy evaluation
Closure implementation iterator effect
Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
Design and manufacture of simple digital display electronic scale
Bird recognition app
【电赛训练】红外光通信装置 2013年电赛真题
记一次redis超时
ES6 decoupling top-level objects from windows
富文本实现插值
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))