sql在插⼊语句使⽤⼦查询_SQL插⼊查询
sql在插⼊语句使⽤⼦查询
SQL INSERT Query also referred as SQL INSERT INTO is the command used for inserting data into database. SQL INSERT statement allows insertion of data into one table at a time.
SQL INSERT查询(也称为SQL INSERT INTO)是⽤于将数据插⼊数据库的命令。 SQL INSERT语句允许⼀次将数据插⼊⼀个表中。SQL插⼊查询 (SQL Insert Query)
INSERT statement inserts data into a table of the database. There are three ways to insert data into database using the INSERT INTO statement. Let’s try to understand all the ways we can use insert query in sql statements.
INSERT语句将数据插⼊数据库的表中。 有三种使⽤INSERT INTO语句将数据插⼊数据库的⽅法。 让我们尝试了解在sql语句中使⽤插⼊查询的所有⽅式。
SQL插⼊范例 (SQL Insert Examples)
The three ways to use INSERT INTO statement are as mentioned below.
下⾯介绍了使⽤INSERT INTO语句的三种⽅法。
1. SQL列名称插⼊ (SQL Insert with Column Names)
In order to insert data into a table we have to specify the columns and the corresponding values as shown below.
Syntax:
INSERT INTO table_name (column(s)) VALUES value(s);
In the syntax mentioned above the values should be in the same order as the column mentioned in the INSERT INTO statement. Otherwise it might throw error or data might go into wrong columns. Here is an example of sql insert with column names.
为了将数据插⼊表中,我们必须指定列和相应的值,如下所⽰。
语法 :
INSERT INTO table_name (column(s)) VALUES value(s);
在上述语法中,值的顺序应与INSERT INTO语句中提到的列的顺序相同。 否则可能会引发错误,或者数据可能会进⼊错误的列。 这是带有列名的sql插⼊⽰例。
2. 不带列名SQL插⼊查询 (SQL Insert Query without Column Names)
In order to insert data into a table without specifying the columns, it is important to provide the values in the same order as the columns are present in the table. Also, this should be used only when we want to insert data into every column of the table.
Syntax:
INSERT INTO table_name VALUES value(s).
Example:
This type of insert is error prone, specially when you add another column in the table later on. The data might get corrupted because of getting inserted into wrong columns, this type of insert query should be avoided wherever possible.
为了在不指定列的情况下将数据插⼊表中,重要的是要提供与表中列相同的顺序的值。 另外,仅当我们要将数据插⼊表的每⼀列时,才应使⽤此⽅法。
语法 :
INSERT INTO table_name VALUES value(s).
范例 :
这种类型的插⼊容易出错,尤其是稍后在表中添加另⼀列时。 数据可能会由于插⼊错误的列⽽损坏,因此应尽可能避免这种插⼊查询。
3. SQL插⼊选择 (SQL INSERT INTO SELECT)
SQL INSERT statement can be used along with the SELECT statement. This is usually used when we have to insert data from one table to other table.
Syntax:
INSERT INTO first_table_name [column(s)] SELECT column(s) FROM second_table_name WHERE condition;
Example:
SQL INSERT语句可以与SELECT语句⼀起使⽤。 当我们必须将数据从⼀个表插⼊到另⼀表时,通常使⽤此⽅法。
语法 :
INSERT INTO first_table_name [column(s)] SELECT column(s) FROM second_table_name WHERE condition;
范例 :
SQL插⼊多⾏ (SQL Insert Multiple Rows)
There are situations when multiple record needs to be inserted in the database table. Let’s try to understand how to accomplish such requirements.
在某些情况下,需要在数据库表中插⼊多个记录。 让我们尝试了解如何实现这些要求。
Using the below mentioned syntax we can make multiple insertion in the database table.
使⽤下⾯提到的语法,我们可以在数据库表中进⾏多次插⼊。
INSERT INTO table_name [column(s)] VALUES [value(s)], [value(s)];
Example:
范例 :
INSERT INTO Student (StudentId,SudentName) VALUES (1, 'John'), (2, 'Steve'), (3, 'Henry');
In the above mentioned example multiple rows will be inserted in the Student table.
在上⾯提到的⽰例中,将在Student表中插⼊多⾏。
SQL插⼊⽇期 (SQL Insert Date)
Before I conclude SQL insert tutorial, let’s see how to insert date into tables. This is slightly tricky because there are vendor specific functions to get current date and time. Also there are vendor specific functions to parse string to date. Below queries will work for inserting date in MySQL database table.
在我结束SQL插⼊教程之前,让我们看⼀下如何在表中插⼊⽇期。 这有点棘⼿,因为有特定于供应商
的功能来获取当前⽇期和时间。 此外,还有⼀些特定于供应商的函数可以解析字符串⾄今。 下⾯的查询将在MySQL数据库表中插⼊⽇期。
-- insert current date
insert into DateExample (name, insert_date)
values ('Pankaj', now());
--auto convert string to date because of standard format
insert into DateExample (name, insert_date)
values ('David', '2018-01-23');
sql语句查询不包含-- use STR_TO_DATE function for non-standard format
insert into DateExample (name, insert_date)
values ('Lisa', STR_TO_DATE('2018-01-23', '%Y-%m-%d'));
Corresponding insert queries for Oracle database.
Oracle数据库的相应插⼊查询。
-- insert current date
insert into DateExample (id, name, insert_date)
values (1, 'Pankaj', sysdate);
--auto convert string to date because of standard format
insert into DateExample (id, name, insert_date)
values (2, 'David', '23-JAN-18');
-- use TO_DATE function for non-standard format
insert into DateExample (id, name, insert_date)
values (3, 'Lisa', to_date('23-01-2018','DD-MM-YYYY'));
That’s all for insert query in sql statements.
这就是sql语句中的插⼊查询的全部内容。
Reference:
参考:
翻译⾃:
sql在插⼊语句使⽤⼦查询
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论