当前位置:网站首页>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 null

  • Uniqueness constraint   unique

  • Primary key constraint     primary key

  • Foreign key constraints     foreign key

  • Check 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));

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

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);

MySQL What are the conditions for creating a table

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

MySQL What are the conditions for creating a table

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) );

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

not null and unique Can be used in combination with

MySQL What are the conditions for creating a table

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));

MySQL What are the conditions for creating a table

Single primary key , Epipolar constraint

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

MySQL What are the conditions for creating a table

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)    -> );

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

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)    -> );

MySQL What are the conditions for creating a table

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 )snameclassno( Foreign key constraints )
1jack100
2lucy100
3kk100
4smith200
5frank300
6jhh300
  • 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

MySQL What are the conditions for creating a 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 cascase

  • cascading deletion :on delete cascase

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

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

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

update cascade

MySQL What are the conditions for creating a table

MySQL What are the conditions for creating a table

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 .

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211959412696.html