CREATE TABLE [Tb_name];
1. Create a PRIMARY KEY constraint for the table: the primary key is the function that uniquely identifies a field, and when the field is the primary key, its value must be unique and cannot be empty.
Mysql>create TABLE student (id INT PRIMARY KEY,
stu_id INT,
COURSE_ID INT); ID is the primary key and all fields are integers
Mysql>create TABLE student (id INT PRIMARY KEY,
stu_id INT,
course_id INT,
PRIMARY KEY (id,stu_id)
); IDs and stu_id become primary keys, and the combination of the two uniquely identifies a record
2. Create a FOREIGN KEY constraint for the table (CONSTRAINT: foreign key alias; FOREIGN key: foreign key field; REFERENCES parent table): The foreign key is associated with another table, the foreign key must be the primary key of the other table, and the foreign key can be empty.
Mysql>create TABLE student (id INT PRIMARY KEY,
stu_id INT,
course_id INT,
CONSTRAINT c_fk FOREIGN KEY (stu_id,course_id)
REFERENCES example2 (Stu_id,course)
The stu_id,course_id of table student is the foreign key, the foreign key to the STU_ID,COURSE_ID primary key of the Example2 table. Example2 is the parent table of student. The foreign key of the child table must be associated with the parent table's primary key. Also, the data types must be consistent.
3. Set non-null constraint for table (NOT NULL): Define field value cannot be empty
Mysql>create TABLE student (id INT not NULL PRIMARY KEY,
Name VARCHAR () is not NULL,
stu_id INT,
CONSTRAINT e_fk FOREIGN KEY (stu_id)
REFERENCES example1 (stu_id)
); The ID is the primary key, the NOT NULL definition ID and stu_id are non-null constraints, constraint defines the foreign key alias, FOREIGN key defines the foreign key field, and references defines the parent table;
4. Set the unique constraint for the table (unique): Defines the field value unique.
Mysq>create TABLE student (id INT not NULL PRIMARY KEY,
Name VARCHAR () is not NULL,
stu_id INT UNIQUE,
Sex CHAR (6),
CONSTRAINT e_fk FOREIGN KEY (Sex)
REFERENCES example2 (Sex),
); ID is the primary key and cannot be null; name is a string type and cannot be empty; sut_id is shaped and can only be unique; sex is a string and is a foreign key, Example2 is a student parent key.
5, set the property value of the table automatically increase: Digital auto-growth (auto_increment)
Mysql>create TABLE student (id INT PRIMARY KEY auto_increment,
Name VARCHAR () is not NULL,
stu_id UNIQUE Auto_increment,
Sex CHAR (6),
CONSTRAINT e_fk FOREIGN KEY (Sex)
REFERENCES example2 (Sex),
); ID is primary key and autogrow cannot be empty, name is string cannot be empty, stu_id is unique key autogrow, sex is foreign key, example2 is parent table of student.
6, set the default value of the table property (if inserting a new record does not assign a value to this field, the database system will automatically insert the default value for this field): Defaults
Mysql>create TABLE student (id INT PRIMARY KEY auto_increment,
Name CHAR (a) is not NULL,
stu_id UNIQUE INT auto_increment,
Sex CHAR (6) Defalut ' man ',
); ID is an auto-grow integer type, and is a primary key; name is a character type, cannot be empty; stu_id is the unique key autogrow; the default value for sex is man.
mysql-creating tables and their constraints