Inserting Rows into a Table
After creating a table, we can insert rows into it using an INSERT
statement.
Following is the general syntax of an INSERT
statement:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
In the above syntax, the table_name
refers to the name of the table in which we want to insert a row.
The sequence of values (value1, value2, value3, ...)
needs to be in the same order as the sequence of the columns that are specified at (column1, column2, column3, ...)
.
For example:
INSERT INTO students (first_name, second_name, age) VALUES ('John', 'Doe', 24);
// OR
INSERT INTO students (second_name, first_name, age) VALUES ('Doe', 'John', 24);
// OR
INSERT INTO students (age, first_name, second_name) VALUES (24, 'John', 'Doe');
// etc
Demonstration:
Tip:
There is a shorter syntax for inserting rows into a table:
INSERT INTO table_name VALUES (value1, value2, value3 β¦)
For example:
INSERT INTO students VALUES ('John', 'Doe', 24);
In this case, the order in which the values are stated should be the same as the default order of the columns.
The difference between the use cases of the two syntaxes we explored will become more apparent in the later chapters.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 1.72
Inserting Rows into a Table
Swipe to show menu
After creating a table, we can insert rows into it using an INSERT
statement.
Following is the general syntax of an INSERT
statement:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
In the above syntax, the table_name
refers to the name of the table in which we want to insert a row.
The sequence of values (value1, value2, value3, ...)
needs to be in the same order as the sequence of the columns that are specified at (column1, column2, column3, ...)
.
For example:
INSERT INTO students (first_name, second_name, age) VALUES ('John', 'Doe', 24);
// OR
INSERT INTO students (second_name, first_name, age) VALUES ('Doe', 'John', 24);
// OR
INSERT INTO students (age, first_name, second_name) VALUES (24, 'John', 'Doe');
// etc
Demonstration:
Tip:
There is a shorter syntax for inserting rows into a table:
INSERT INTO table_name VALUES (value1, value2, value3 β¦)
For example:
INSERT INTO students VALUES ('John', 'Doe', 24);
In this case, the order in which the values are stated should be the same as the default order of the columns.
The difference between the use cases of the two syntaxes we explored will become more apparent in the later chapters.
Thanks for your feedback!