当前位置:网站首页>Mysql database foundation: constraint and identification columns
Mysql database foundation: constraint and identification columns
2022-06-30 10:43:00 【Long lasting bangbangjun】
constraint
1、 Definition
A limitation , Used to limit the data in the table , In order to ensure the accuracy and reliability of the data in the table .
2、 classification
- NOT NULL: Non empty , To ensure that the value of this field cannot be empty
- DEFAULT: Default , Used to ensure that the field has a default value
- PRIMARY KEY: Primary key , Used to ensure that the value of this field is unique , And not empty
- UNIQUE: only , Used to ensure that the value of this field is unique , Can be null .
- CHECK: Check constraint (mysql China does not support it. )
- FOREIGN KEY: Foreign key constraints , Limit the relationship between two tables , To ensure that the value of this field must come from the value of the associated column of the main table . Add foreign key constraint from table , Used to reference the value of a column in the main table .
3、 Add constraints when creating tables
3.1 Column level constraints
Column level constraints are usually written directly after the field type , as follows :
create table Table name (
Field name Field type Column level constraints ,
Field name Field type Column level constraints ,
);
- for example
# Student list
create table student(
id int primary key, # Primary key
stuName varchar(20) not null, # Non empty
gender char(1) check(gender=' male ' or gender=' Woman '), # Check
seat int unique, # Unique constraint
age int default 18, # Default constraint
majorId int foreign key references major(id) # Foreign key constraints
)
# The curriculum
create table major(
id int primary key,
majorName varchar(20)
);
Be careful : The six constraints are syntactically supported , No mistake. , But foreign key constraints and check constraints have no effect
3.2 Table level constraints
Table level constraints are usually at the bottom of all fields , The grammar is as follows :
create table Table name (
Field name Field type ,
Field name Field type ,
# Table level constraints
【constraint Alias 】 Constraint type ( Field name ),
...
);
- for example
# Student list
create table student(
id int,
stuName varchar(20),
gender char(1),
seat int,
age int,
majorId int,
# Add table level constraints
constraint pk primary key(id), # to id Add primary key constraint
constraint uq unique(seat),
constraint ck check(gender=' male ' or gender=' Woman '), # Check constraint
constraint fk_student_major foreign key(majorId) references major(id) # Foreign key constraints
)
# The curriculum
create table major(
id int primary key,
majorName varchar(20)
);
In table level constraints , Non null and default constraints are not supported
Compare primary key with unique key
(1) Both primary keys and unique keys must be unique
(2) The primary key cannot be empty , The only key can be empty
(3) At most one primary key in a table , But you can have multiple unique keys
4、 Add constraints when modifying tables
grammar :
# Column level constraints
alter table Table name modify column Name type Constraint type
# Token constraint
alter table Table name add 【constraint Constraint name 】 Constraint type ( Field name ) 【 Foreign key references 】
for example :
# Add non empty constraints
alter table student modify column stuName varchar(20) not null;
# Add default constraint
alter table student modify column age int default 18;
# Add primary key
# (1) Column level constraints
alter table student modify column id int primary key;
# (2) Table level constraints
alter table student add primary key(id);
# Add unique key
# (1) Column level constraints
alter table student modify column seat int unique;
# (2) Table level constraints
alter table student add unique(seat);
# Add foreign keys
alter table student add foreign key(majorid) references major(id);
Column level constraints do not support aliasing , Table level constraints can be aliased
4、 Delete constraints when modifying tables
# Delete non empty constraints
alter table student modify column stuName varchar(20) null;
# Delete primary key
alter table student drop primary key;
# Delete default constraint
alter table student modify column age int;
# Delete unique
alter table student drop index seat;
# Delete foreign key
alter table student drop foreign key majorid;
Identity column
Definition
Also known as self growing column , You don't have to insert values manually , The system provides the default sequence value
Use
- Set identity column when creating table
create table tab_identity(
id int primary key auto_increment,
name varchar(20)
);
# insert data
insert into tab_identity values(null,'john');
insert into tab_identity values(null,'john');
insert into tab_identity values(null,'john');
insert into tab_identity values(null,'john');
select * from tab_identity;

Through the following statement, you can view the step size and starting value of the identification column
show variables like '%auto_increment%';

The step size of the identification column can be modified by the following statement ,mysql Modifying the starting value... Is not supported in .
set auto_increment_increment=3;
characteristic
(1) The identification column needs to be matched with a key ( Primary key , Unique key, etc )
(2) A table can have at most one identity column
(3) The type of the identity column can only be numeric
- Set the identity column when modifying the table
alter table tab_identity modify column id int primary key auto_increment;
- Delete identity column when modifying table
alter table tab_identity modify column id int ;
边栏推荐
- 吴恩达2022机器学习专项课测评来了!
- 【Rust日报】2021-01-23 几个新库发布
- R语言aov函数进行重复测量方差分析(Repeated measures ANOVA、其中一个组内因素和一个组间因素)、分别使用interaction.plot函数和boxplot对交互作用进行可视化
- ArcGIS PRO + PS vectorized land use planning map
- 半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- [rust daily] the first rust monthly magazine on January 22, 2021 invites everyone to participate
- GD32 RT-Thread RTC驱动函数
- Skill combing [email protected] control a dog's running on OLED
- 再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
猜你喜欢

透过华为军团看科技之变(五):智慧园区

Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

Configure Yii: display MySQL extension module verification failed

Criu enables hot migration

CSDN blog operation team 2022 H1 summary

【深度学习】深度学习检测小目标常用方法

Robotframework learning notes: environment installation and robotframework browser plug-in installation
[email protected] somatosensory manipulator"/>Skill combing [email protected] somatosensory manipulator

吴恩达2022机器学习专项课测评来了!
随机推荐
The performance of arm's new CPU has been improved by 22%, up to 12 cores can be combined, and the GPU is first equipped with hardware optical tracking. Netizen: the gap with apple is growing
[rust daily] several new libraries were released on January 23, 2021
Leetcode question brushing (II) -- sorting (go Implementation)
GeoffreyHinton:我的五十年深度学习生涯与研究心法
无心剑中译狄金森《灵魂择其伴侣》
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
Oracle creates a stored procedure successfully, but the compilation fails
移植完整版RT-Thread到GD32F4XX(详细)
Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development
Skill combing [email protected] somatosensory manipulator
机器学习面试准备(一)KNN
Auto SEG loss: automatic loss function design
技能梳理[email protected]+adxl345+电机震动+串口输出
Leetcode question brushing (III) -- binary search (go Implementation)
Ant s19xp appeared in 140t, why is it called the computing power ceiling by the world
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
The rising star of Goldshell STC box
【Rust日报】2021-01-23 几个新库发布
超长干货 | Kubernetes命名空间详解
Kernel linked list (general linked list) "list.h" simple version and individual comments