SQL must know will be - insert data (xv)

1, data insertion

INSERT for inserting rows into (or added) to the database table. There are several ways to insert:

  • Insert complete row
  • Some portions of the inserted row
  • Insert the results of some queries

note:
1, using the INSERT statement may require specific security permissions to client / server DBMS. Before you try to use INSERT, you should ensure that they have sufficient security permissions

2.1, insert a complete row

INSERT INTO customers
VALUES(
'Toy Land'
,'123 Any Street'
,'New York'
,'NY'
,'11111'
,'USA'
,'NULL'
,'NULL'
);

note:
1, in some SQL implementations, with INSERT INTO keyword following is optional. However, in a timely manner is not necessarily required, it is best to provide this keyword, so that will be done to ensure that SQL code is portable between DBMS
2, the SQL statement above is highly dependent on the columns defined in the table, but also on its easy access order Information. Such information can be obtained even if the order does not guarantee that the columns of a table structure after the next change in order to maintain identical. Therefore, the preparation depends on the particular sequence of SQL statements is very safe, so sooner or later go wrong

INSERT INTO customers
(
cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email
)
VALUES(
'Toy Land',
'123 Any Street',
'New York',
'NY',
'11111',
'USA',
'NULL',
'NULL',
)
--在插入行时,DBMS将用VALUES列表中的相应值贴入列表中的对应项。VALUES中的第一个值对应第一个指定列名,
第二个值对应于第二个列名,如此等等。

note:
1, as provided column names, VALUES must match the column names specified in its order specified, not necessarily for each actual order listed in the table now. The advantage is that, even if the table structure changes, which can be any natural INSERT statement work correctly.

2.2, the insertion portion rows

INSERT INTO customers
(
cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
)
VALUES(
'Toy Land',
'123 Any Street',
'New York',
'NY',
'11111',
'USA',
)

note:
Omitted columns must satisfy certain conditions at
1, the column is defined to allow a NULL value (null value or no value)
2, default values are given in the table definition. This means that if the value is not given, the default value

2.3, the insertion of data retrieved

INSERT INTO customers
(
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
)
SELECT 
	cust_name,
	cust_address,
	cust_city,
	cust_state,
	cust_zip,
	cust_country
FROM custnew;

note:
1, for simplicity, this example uses the same column name in INSERT and SELECT statements. However, it does not necessarily require column names to match. In fact, DBMS do not care about the column names in the SELECT returned. He uses the position of the column.

2.4, copied from one table into another

SELECT *
INTO custcopy
FROM customers;
--这条SELECT语句创建一个名为custcopy的新表,并把customers表的整个内容复制到新表中。
因为这里使用的时select *,所以将在custcopy表中创建(并填充)于customers表的每一列相同的列。

note:

  • SELECT clause and any election can be used, including the WHERE and GROUP BY
  • Coupling may be utilized to insert data from a plurality of tables
  • No matter how many tables to retrieve data from, the data can only be inserted into a table

Guess you like

Origin blog.csdn.net/qq_28285403/article/details/94735094