How to create a table in mySQL and also give the code to insert multip...
Creating a Table in MySQL
To create a table in MySQL, you can use the `CREATE TABLE` statement. Here’s a step-by-step guide:
1. **Define the Table Structure**
Specify the table name and the columns you want to include, along with their data types.
sql
CREATE TABLE Students (
StudentID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Grade CHAR(1)
);
2. **Explanation of the Code**
- `Students`: The name of the table.
- `StudentID`: A unique identifier for each student, set to auto-increment.
- `FirstName` and `LastName`: Strings for the student’s name.
- `Age`: An integer representing the student's age.
- `Grade`: A character representing the grade level.
Inserting Multiple Values into the Table
To insert values into the table, use the `INSERT INTO` statement. Here’s how to insert multiple rows:
sql
INSERT INTO Students (FirstName, LastName, Age, Grade) VALUES
('John', 'Doe', 15, 'A'),
('Jane', 'Smith', 14, 'B'),
('Emily', 'Jones', 16, 'A'),
('Michael', 'Brown', 15, 'C');
3. **Explanation of the Code**
- `INSERT INTO Students`: Specifies the table where data will be inserted.
- `(FirstName, LastName, Age, Grade)`: The columns in which values will be inserted.
- `VALUES`: The list of values to insert; each set of parentheses represents a new row.
By following these steps, you can successfully create a table and insert multiple records into it in MySQL.
To make sure you are not studying endlessly, EduRev has designed Class 10 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 10.