当前位置:网站首页>What are the conditions for MySQL to create tables
What are the conditions for MySQL to create tables
2022-06-21 22:00:00 【Yisu cloud】
MySQL What are the conditions for creating a table
This article “MySQL What are the conditions for creating a table ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “MySQL What are the conditions for creating a table ” Article bar .
Due to addition, deletion and modification emp The records in the table , So here you re create a script and use
create database bjpowernnode;use bjpowernode;source C:\Users\Administrator\Desktop\bjpowernode.sql;
constraint
1. What are constraints ?
A constraint is a constraint in a table
The key word for the constraint is :constraint
2. Classification of constraints
Non empty constraint
not nullUniqueness constraint
uniquePrimary key constraint
primary keyForeign key constraints
foreign keyCheck constraint MySQL Database does not support ,Oracle Database support
1. not null( Non empty constraint )
not null Constrained fields , Not for null value , Specific data must be given
Create table , Add... To the field Non empty constraint 【 The email address of the user cannot be empty 】
drop table if exists t_user;create table t_user( id int(10), name varchar(32) not null, email varchar (32));


2. unique( Uniqueness constraint )
Create table , Ensure that the email address is unique
create table t_user(id int(10),name varchar(32) not null,email varchar(128) unique);

unique Constraint fields cannot be repeated , But it can be for null

The above constraints are column level constraints
Table level constraints :
create table t_user( id int(10), name varchar(32), email varchar(128), unique(email) );
1. Use table level constraints to add constraints to multiple fields
create table t_user( id int(10), name varchar(32), email varchar(128), unique(name,email));
2. Table level constraints can be named , In the future, you can delete the constraint by this name
create table t_user( id int(10), name varchar(32), email varchar(128), constraint t_user_email_unique unique(email) );



not null and unique Can be used in combination with

3. primary key ( Primary key constraint )
1. Terms involved in the primary key :
Primary key constraint
Primary key field
Primary key value
2. The relationship among the above three :
After adding a primary key constraint to a field in the table , This field is called the primary key field
Every data appearing in the primary key field is called the primary key value
3. After adding a primary key constraint to a field , This field cannot be repeated , It can't be empty
Primary key constraint effect and ''not null unique'' identical , But the essence is different ,
Primary key constraints can be done in addition to ''not null unique'' outside
The primary key field will also be added by default '' Indexes -index''
4. A table should have primary key fields , without , Indicates that this table is invalid
The primary key value is the unique identifier of the current row data
The primary key value is the ID number of the current row data
Even if the data of the two rows in the table are identical ,
However, due to different primary key values , Think of these as two completely different lines of fields
5. Whether it is a single primary key or a composite primary key , There can only be one primary key constraint for a table
Add a primary key constraint to a field , It is called a single primary key constraint
Add a primary key constraint to the union of multiple fields , It is called a composite primary key
6. Primary keys are classified by nature :
Naturally : The primary key value is a natural number , This primary key has nothing to do with the current business
Business primary key : The primary key value is closely related to the current business
When the business changes , The pass of primary key values will be affected , Therefore, business primary keys are rarely used .
Single primary key , Column level constraints
create table t_user( id int(10) primary key, name varchar(32));

Single primary key , Epipolar constraint
create table t_user( id int(10), name varchar(32), primary key(id));

Composite primary key : Only table level constraints can be used
mysql> create table t_user( -> id int(10), -> name varchar(32), -> primary key(id,name) -> );


auto_increment: The primary key increases automatically
MySQL A self increasing number is provided in the data management system , It is specially used to automatically generate primary key values
The primary key value does not need to be maintained by the user , You don't need to provide them , Automatically generated ,
This self increasing number defaults from 1 Start with 1 Increasing :1,2,3,4,....
mysql> create table t_user( -> id int(10) primary key auto_increment, -> name varchar(32) -> );

4. foreign key( Foreign key constraints )
1. Terms involved in foreign key constraints :
Foreign key constraints
Foreign key value
Foreign key field
2. The relationship among the above three :
A field is called a foreign key field after adding a foreign key constraint
Each data in a foreign key field is called a foreign key value
3. Foreign keys are divided into single foreign keys and composite foreign keys
Single foreign key : Add a foreign key constraint to a field
Compound foreign key : Add foreign key constraints to multiple fields
4. A table can have multiple foreign key fields
Design a database table , Used to store student and class information , Give two solutions :
The relationship between student information and class information : One class corresponds to more than one student , This is a typical one to many relationship
Add a foreign key to the more side
The first design : Store student information and class information in a table
The second design : The student information and class information are stored in two separate tables , Student list + Class table
Student list t_student
| sno( Primary key constraint ) | sname | classno( Foreign key constraints ) |
|---|---|---|
| 1 | jack | 100 |
| 2 | lucy | 100 |
| 3 | kk | 100 |
| 4 | smith | 200 |
| 5 | frank | 300 |
| 6 | jhh | 300 |
Class table t_calss
| cno( Primary key constraint ) | cname |
|---|---|
| 100 | three 1 class |
| 200 | three 2 class |
| 300 | three 3 class |
In order to ensure t_student In the table classno The data in the field must come from t_class In the table cno The data in the field , It is necessary to give t_student In the table classno Field add foreign key constraint ,classno Foreign key fields are called , The value in this field is called the foreign key value .
Be careful :
1. Foreign key value can be empty
2. Does the foreign key field have to refer to the primary key in this table ?
When a foreign key field refers to a field of a table , The referenced field must be unique
With a unique constraint , Not necessarily a primary key
3. The class table is the parent table , The student table is a sub table
You should create the parent table first , Create a sub table
When deleting data , You should delete the data in the sub table first , Then delete the data in the parent table
When inserting data , You should insert the data in the parent table first , Delete the data in the sub table

DROP TABLE IF EXISTS t_student;DROP TABLE IF EXISTS t_class; CREATE TABLE t_class( cno INT(3) PRIMARY KEY, cname VARCHAR(128) NOT NULL UNIQUE ); CREATE TABLE t_student( sno INT(3) PRIMARY KEY, sname VARCHAR(32) NOT NULL, classno INT(3),-- Foreign keys CONSTRAINT t_student_class_fk FOREIGN KEY(classno) REFERENCES t_class(cno) ); INSERT INTO t_class(cno,cname) VALUES(100,' three 1 class '); INSERT INTO t_class(cno,cname) VALUES(200,' three 2 class '); INSERT INTO t_class(cno,cname) VALUES(300,' three 3 class '); INSERT INTO t_student(sno,sname,classno) VALUES(1,'jack',100); INSERT INTO t_student(sno,sname,classno) VALUES(2,'lucy',100); INSERT INTO t_student(sno,sname,classno) VALUES(3,'hh',100); INSERT INTO t_student(sno,sname,classno) VALUES(4,'frank',200); INSERT INTO t_student(sno,sname,classno) VALUES(5,'smith',300); INSERT INTO t_student(sno,sname,classno) VALUES(6,'jhh',300); SELECT * FROM t_student; SELECT * FROM t_class; -- Add failure , Because there are foreign key constraints INSERT INTO t_student(sno,sname,classno) VALUES(8,'kk',500);
a key : Typical one to many relationship , When designing, add a foreign key to the multiple side
5. Cascade update and cascade delete
When deleting data in the parent table , Cascade delete the data in the sub table
When updating the data in the parent table , Cascade updates the data in the sub table
The above cascading updates and cascading deletes should be used with caution ,
Because cascading operation will change or delete data , The data is priceless .
grammar :
update cascade :
on update cascasecascading deletion :
on delete cascase


MySQL It is troublesome to modify some constraints in , So you should delete the constraint first , Add constraints again
Delete foreign key constraint :
alter table t_student drop foreign key t_student_class_fk
Add foreign key constraints and cascade updates :
alter table t_student add constraint t_student_class_fk foreign key(classno)references t_class(no) on delete cascade;
Add foreign key constraints and cascade deletions :
alter table t_student add constraint t_student_class_fk foreign key(classno)references t_class(no) on update cascade;
cascading deletion


update cascade


That's about “MySQL What are the conditions for creating a table ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .
边栏推荐
- Tutorial on the implementation of smart contracts by solidity (4) -erc1155 contracts
- LeetCode508-出现次数最多的子树元素和-深搜
- Tc3608h high efficiency 1.2MHz DC-DC booster IC
- 杰理之获取当前音频文件(长)文件名和当前(长)文件夹名【篇】
- 三维度八视图
- China micro semiconductor has passed the registration: the annual revenue is 1.1 billion, and the actual controller is New Zealand nationality
- Go language self-study series | golang standard library encoding/xml
- 杰理之SD 卡复用 iic 注意问题【篇】
- 杰理之开启四声道打开 EQ 后播歌卡顿问题【篇】
- 企业数据防泄漏解决方案分享
猜你喜欢

Six interesting web page effects that can be used

Tx9116 Synchronous Boost IC

Ln2220 2A overcurrent 5v1a High Efficiency Boost IC chip dc/dc voltage regulator
![There is no sound solution to the loopback when jerryzhi launches Bluetooth [chapter]](/img/ba/377ec19ca22c2c106f227e864f1e9e.png)
There is no sound solution to the loopback when jerryzhi launches Bluetooth [chapter]

Yanyu saltalk obtained USD 8million round a financing: continue to expand team and market coverage

Pinduoduo 618 mobile phone brand official flag sales increased by 124% year-on-year, and 4000+ high-priced mobile phones increased by 156% year-on-year

撰写学术论文引用文献时,标准的格式是怎样的?

怎样有效率地进行外文文献检索?

Does the whole house smart home brand "zhiting technology" have an experience center?

Improve four performance! D-wave releases next generation quantum annealing prototype
随机推荐
大不列颠泰迪熊加入PUBG 手游
大型语言模型教会智能体进化,OpenAI这项研究揭示了二者的互补关系
广东疾控提醒:暑期将至,返粤大学生这样安全“归巢”
JS异步的执行顺序是什么
Go单元测试对数据库CRUD进行Mock测试
赋·新生,链·未来!城链科技产业赋能创新发展大会隆重举行
Phpmailer sends mails through SMTP. Some of the same sending contents succeed and some fail
英文论文要怎么查重?
Unity dynamically reads and plays external music
C language array and pointer exercises (original question + analysis + original code)
Large language models teach agents to evolve. Openai reveals the complementary relationship between the two
传承百年经典的瑞吉管家静待您的优雅旅程再次开启
企业数据防泄漏解决方案分享
What websites or software are available to translate English literature into Chinese?
Go language self-study series | golang standard library encoding/xml
There is no sound solution to the loopback when jerryzhi launches Bluetooth [chapter]
Which iPad apps can read English literature well?
ctfshow 105-127
Tx9116 Synchronous Boost IC
Go unit test mocks the database CRUD