How to create a student table and add data in MySQL?
To create a student table and add data, you can follow these steps:
- First, login to the MySQL database.
- Create a database (if it doesn’t already exist) using the following command:
CREATE DATABASE school;
- Access the newly created database.
USE school;
- Create a table named ‘students’ using the following command:
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender ENUM('Male', 'Female'),
grade INT
);
The above code creates a table called students, with fields for id, name, age, gender, and grade. The id field serves as the primary key with automatic increment, name represents the student’s name, age is the student’s age, gender is the student’s gender, and grade is the student’s grade.
- To add data to the students table, you can utilize the following command:
INSERT INTO students (name, age, gender, grade)
VALUES ('张三', 18, 'Male', 90),
('李四', 17, 'Female', 85),
('王五', 16, 'Male', 92);
The above code inserts three student data into the students table: Zhang San, Li Si, and Wang Wu.
Once you have completed the above steps, you have successfully created a student table and added data to it. You can use the SELECT statement to query the data in the table, such as:
SELECT * FROM students;
This will retrieve all the data from the students table.