SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Insert - SQL基础教程 - 笔下光年
网站首页
SQL Insert
In this tutorial you will learn how to insert records in a database table using SQL. ## Inserting Data in Table In the [preceding chapter](http://www.bixiaguangnian.com/manual/sql/3390.html "preceding chapter") we've created a table named persons in our demo database. Now it's time to insert some data inside our newly created database table. The `INSERT INTO` statement is used to insert new rows in a database table. ## Syntax The basic syntax for inserting data into a table can be given with: ```sql INSERT INTO table_name (column1,column2,...) VALUES (value1,value2,...); ``` Here the column1, column2,..., etc. represents the name of the table columns, whereas the value1, value2,..., and so on represents the corresponding values for these columns. Let's insert some records into the persons table. ### Step 1: View Table Structure Before adding record it's a good idea to obtain the information about the table structure. Execute the following command on MySQL command-line. It will display the information about the columns in the persons table i.e. column name, data type, constraints etc. ``` mysql> DESCRIBE persons; ``` You can see the column information or structure of any table in MySQL and Oracle database using the command `DESCRIBE table_name;`, whereas `EXEC sp_columns table_name; `in SQL Server (replace the table_name with actual table name). ### Step 2: Adding Records to a Table The following statement inserts a new row in persons table. ```sql INSERT INTO persons (name, birth_date, phone) VALUES ('Peter Wilson', '1990-07-15', '0711-020361'); ``` Did you notice, we didn't insert any value for `id` field? Because, if you remember from the [create table](http://www.bixiaguangnian.com/manual/sql/3390.html "create table") chapter, the `id` field was marked with `AUTO_INCREMENT` flag, which tells MySQL to automatically assign a value to this field if it is left unspecified. <div class="callout callout-info mb-3">Note: Non-numeric values like strings and dates must always be surrounded by quotes, whereas numeric values should never be enclosed within quotes. Also, if your string itself contains quotes you should escape it with backslash like 'Let\'s go'.</div> Similarly, insert another row into the persons table, as follow: ```sql INSERT INTO persons (name, birth_date, phone) VALUES ('Carrie Simpson', '1995-05-01', '0251-031259'); ``` Insert one more row into the persons table, in a similar manner: ```sql INSERT INTO persons (name, birth_date, phone) VALUES ('Victoria Ashworth', '1996-10-17', '0695-346721'); ``` Now if you select the records from persons table, the output will now look like this: ``` +----+--------------------+------------+-------------+ | id | name | birth_date | phone | +----+--------------------+------------+-------------+ | 1 | Peter Wilson | 1990-07-15 | 0711-020361 | | 2 | Carrie Simpson | 1995-05-01 | 0251-031259 | | 3 | Victoria Ashworth | 1996-10-17 | 0695-346721 | +----+--------------------+------------+-------------+ ``` We'll learn about SQL statement for selecting records from the tables, in the next chapter.
上一篇:
SQL Constraints
下一篇:
SQL Select