当前位置:网站首页>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 ;
边栏推荐
- Flink interview questions
- 【pytorch学习】torch.device
- Phishing identification app
- 集成积木报表报错 org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
- 短路运算符惰性求值
- Redis -- lattice connects to redis cluster
- An overview of the design of royalties and service fees of mainstream NFT market platforms
- dsPIC30F6014a LCD 方块显示
- Niuke monthly race 22- collect pieces of paper
- js作用域链与闭包
猜你喜欢

Installation and use of NoSQL database

树结构---二叉树2非递归遍历
![2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder](/img/19/cce8d8a7cdcb1021166c46adf803c1.png)
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder

MapReduce programming basics

Imitation of Baidu search results top navigation bar effect

MapReduce编程基础

Mysql 优化

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

MySQL optimization

How to realize the usage of connecting multiple databases in idel
随机推荐
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
In the middle of the year, where should fixed asset management go?
JS functionarguments object
JS原型链
Bird recognition app
2.4 激活函数
Key points of NFT supervision and overseas policies
Design and manufacture of simple digital display electronic scale
Principle and application of single chip microcomputer timer, serial communication and interrupt system
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
JS prototype inheritance can only inherit instances, not constructors
js valueOf 与 toString 区别
2022.02.15_ Daily question leetcode six hundred and ninety
[pytorch] softmax function
微信小程序 webview 禁止页面滚动,同时又不影响业务内overflow的滚动的实现方式
JS prototype trap
3D printing Arduino four axis aircraft
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder