YouTip LogoYouTip

Sql Insert

## SQL INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. ## INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways. 1. Specify both the column names and the values to be inserted: ```sql INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows: ```sql INSERT INTO table_name VALUES (value1, value2, value3, ...); ``` ## Demo Database Below is a selection from the "Websites" table: ``` +----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | Youtube | https://www.youtube.com/ | 2 | USA | | 3 | | | 3 | CN | | 4 | Taobao | https://www.taobao.com/ | 13 | CN | | 5 | Twitter | https://twitter.com/ | 3 | USA | +----+--------------+---------------------------+-------+---------+ ``` ## INSERT INTO Example The following SQL statement inserts a new record in the "Websites" table: ## Example ```sql INSERT INTO Websites (name, url, alexa, country) VALUES ('', '', 3, 'CN'); ``` ( The following SQL statement will only insert data in the "name", "url", and "country" columns of the "Websites" table: ## Example ```sql INSERT INTO Websites (name, url, country) VALUES ('Stackoverflow', 'https://stackoverflow.com/', 'USA'); ``` ( ## Insert Data in Specified Columns The following SQL statement will insert a new record, but only insert data in the "name", "url", and "country" columns (id will be updated automatically): ## Example ```sql INSERT INTO Websites (name, url, country) VALUES ('Youtube', 'https://www.youtube.com/', 'USA'); ``` ( ## Insert Multiple Rows The following SQL statement inserts three new records in the "Websites" table: ## Example ```sql INSERT INTO Websites (name, url, alexa, country) VALUES ('Taobao', 'https://www.taobao.com/', 13, 'CN'), ('QQ', 'https://www.qq.com/', 9, 'CN'), ('Facebook', 'https://www.facebook.com/', 3, 'USA'); ``` ( The following is the selection from the "Websites" table after the insertion: ``` +----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | Youtube | https://www.youtube.com/ | 2 | USA | | 3 | | | 3 | CN | | 4 | Taobao | https://www.taobao.com/ | 13 | CN | | 5 | Twitter | https://twitter.com/ | 3 | USA | | 6 | Stackoverflow| https://stackoverflow.com/| 0 | USA | | 7 | QQ | https://www.qq.com/ | 9 | CN | | 8 | Facebook | https://www.facebook.com/| 3 | USA | +----+--------------+---------------------------+-------+---------+ ```
← Sql UpdateSql Orderby β†’