SQL is required. Note chapter 15th inserts data. SQL is required.

Source: Internet
Author: User
Tags table definition

SQL is required. Note chapter 15th inserts data. SQL is required.
15.1 data insertion

Insert can be used in several ways:
(1) Insert the complete row
(2) Insert a part of the row
(3) Insert some query results

15.1.1 Insert the complete row
INSERT INTO CustomersVALUES('1000000006',     'Toy Land',     '123 Any Street',     'New York',     'NY',     '11111',     'USA',     NULL,     NULL);

A value must be provided for each column. If a column has no value, a NULL value should be used.
Ensures the security of INSERT statements

INSERT INTO Customers(cust_id,     cust_name,     cust_address,     cust_city,     cust_state,     cust_zip,     cust_country,     cust_contact,     cust_email)VALUES('1000000006',     'Toy Land',     '123 Any Street',     'New York',     'NY',     '11111',     'USA',     NULL,     NULL);

Because the column name is provided, VALUES must match the specified column name in the specified order, not necessarily in the order listed in the actual table. Its advantages are,This INSERT statement works correctly even if the table structure is changed.
Always use the column list:Generally, do not use an INSERT statement that does not explicitly list columns.Using the column list can make the SQL code continue to play a role, even if the table structure has changed.

15.1.2 insert some rows

The recommended method for using INSERT is to explicitly specify the column name of the table. This syntax can also be used to omit columns.

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

Omitted columns: If the table definition permits, some columns can be omitted in the INSERT operation. The omitted column must meet one of the following conditions:
(1) This column is defined to allow NULL values.
(2) The default value is given in the table definition. This indicates that if no value is provided, the default value is used.

15.1.3 Insert the Retrieved Data

INSERT can be used to INSERT the results of a SELECT statement into the table. This is the so-called insert select.

INSERT INTO Customers(cust_id,     cust_name,     cust_address,     cust_city,     cust_state,     cust_zip,     cust_country)SELECT cust_id,     cust_name,     cust_address,     cust_city,     cust_state,     cust_zip,     cust_countryFROM CustNew;

The column names are not necessarily the same.
The SELECT statement in the insert select statement can contain the WHERE clause to filter the inserted data.
INSERT multiple rows: INSERT inserts only one row. To INSERT multiple rows, multiple INSERT statements must be executed. Insert select is an exception. It can INSERT multiple rows with a single INSERT statement, regardless of the number of rows returned by the SELECT statement.

15.2 copy from one table to another

To copy a table to another table (the table created in the operation ). You can use the select into statement.
Insert select and SELECTINTO: an important difference between them is that the former exports data, while the latter imports tables.

SELECT *INTO CustCopyFROM Customers;

The syntax used by MySQL and Oracle is slightly different.

CREATE TABLE CustCopy ASSELECT *FROM Customers;

Note the following when using select:
(1) Any SELECT option and clause can be used, including WHERE and group.
(2) data can be inserted from multiple tables using joins.
(3) No matter how many tables are retrieved, data can only be inserted into a single table.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.