MySQL INSERT INTO

MySQL INSERT INTO Statement

The INSERT INTO statement in MySQL is used to add new records (rows) into a table.


Syntax:


INSERT INTO table_name (column1, column2, column3) 
VALUES (value1, value2, value3);


  • table_name → The name of the table where data is inserted.
  • (column1, column2, column3) → The columns where values will be inserted.
  • VALUES (value1, value2, value3) → The actual values to insert.

Demo Database

Below is a selection from the "Students" table in the school_db database:


student_id

name 

gender

city

age

score

course_id

email

phone_number

1

Alice

Female

Delhi

20

85

101

NULL

1234567890

2

Bob

Male

Mumbai

22

75

NULL

bob@email.com

NULL

3

Charlie

Male

Delhi

21

95

102

NULL

NULL

4

David

Male

Bangalore

23

65

101

david@email.com

NULL

5

Eve

Female

Mumbai

20

80

103

NULL

NULL

6

Frank

Male

Delhi

22

90

103

NULL

NULL

7

Alice

Female

Mumbai

19

60

102

NULL

NULL

Inserting a Record

Example
INSERT INTO students (name, age, city)
VALUES ('John Doe', 20, 'New York');
  • Adds a student named John Doe, aged 20, from New York into the Students table.

Inserting Multiple Records at Once

Example
INSERT INTO students (name, age, city)
VALUES
('Alice', 22, 'Los Angeles'),
('Bob', 19, 'Chicago'),
('Charlie', 21, 'Houston');
  • Inserts three students in one query.

Inserting Data Without Specifying Columns

Example
INSERT INTO students VALUES (1, 'David', 23, 'Miami');
  • Works only if you provide values for all columns in the correct order.

Insert Data Only in Specified Columns

In MySQL, you don’t have to insert data into all columns of a table. You can specify only the columns you want to insert values into, and the rest will either:

✅ Take their default value (if defined).

✅ Remain NULL (if allowed).


Syntax:


INSERT INTO table_name (column1, column2) 
VALUES (value1, value2);


  • Columns not mentioned will be set to NULL or their default value.



Example
INSERT INTO students (name, age, city)
VALUES ('Bob', 19, 'Chicago');

  • gender will be default value (Male). Other columns will be null.


The selection from the "Students" table will now look like this:

student_id

name 

gender

city

age

score

course_id

email

phone_number

1

Alice Johnson

Female

Delhi

20

85

101

NULL

1234567890

2

Bob

Male

Mumbai

22

75

NULL

bob@email.com

NULL

3

Charlie

Male

Delhi

21

95

102

NULL

NULL

4

David

Male

Bangalore

23

65

101

david@email.com

NULL

5

Eve

Female

Mumbai

20

80

103

NULL

NULL

6

Frank

Male

Delhi

22

90

103

NULL

NULL

7

Alice

Female

Mumbai

19

60

102

NULL

NULL

8

Bob

Male

Chicago

19

NULL

NULL

NULL

NULL


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.