当前位置:网站首页>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 ;
边栏推荐
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
- PR training notes
- Phishing identification app
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
- Niuke monthly race 22- collect pieces of paper
- JS use toString to distinguish between object and array
- delete和delete[]引发的问题
- Summary of reptile knowledge points
- 【pytorch学习】torch.device
- 计网01-物理层
猜你喜欢

Nacos service configuration and persistence configuration
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d

2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder

Learning practice: comprehensive application of cycle and branch structure (II)

Simple load balancing with Nacos

【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()

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

MapReduce编程基础

FAQ | FAQ for building applications for large screen devices

SQL学习笔记(01)——数据库基本知识
随机推荐
Key points of NFT supervision and overseas policies
[interview brush 101] linked list
NoSQL数据库的安装和使用
Day06 branch structure and cycle (III)
Naoqi robot summary 28
Preparing for the Blue Bridge Cup -- bit operation
SDN_ Simple summary
Flink interview questions
树结构---二叉树2非递归遍历
Simple load balancing with Nacos
【pytorch】nn.AdaptiveMaxPool2d
es6-顶层对象与window的脱钩
计网01-物理层
MapReduce programming basics
JS functionarguments object
Log4j log framework
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
An overview of the design of royalties and service fees of mainstream NFT market platforms
3D打印Arduino 四轴飞行器