How do you write the code to create a student table in MySQL?
In order to create a student table, you need to use the CREATE TABLE statement. Here is an example code for creating a student table named “students”:
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT,
gender ENUM('男', '女'),
major VARCHAR(50)
);
In the code above, we have defined the following columns:
- ID: Student ID, use INT data type, designate as primary key and auto increment using PRIMARY KEY and AUTO_INCREMENT properties.
- The student’s name is stored as a VARCHAR(50) data type and is specified as not null.
- age: the age of the student, stored as an integer.
- Gender: Student gender, using ENUM type, only allowed values are ‘male’ or ‘female’.
- Major: student’s field of study, stored as VARCHAR(50).
You can make appropriate adjustments and extensions based on your own needs. Please note that the above code is just an example, and may need to be changed according to specific requirements in actual applications.