SQL INSERT statement Note

Source: Internet
Author: User
Tags table definition

Insert a data row using insert

[Insert row of data at once]

Full write:

INSERT into Renshi (name, sex, age, tel)

VALUES (' Hu elder sister ', ' female ', ' 35 ', ' 136334***12 ')

Shorthand:

INSERT Renshi

VALUES (' Hu elder sister ', ' female ', ' 35 ', ' 136334***12 ')

Insert data for the default value column

INSERT into Renshi

VALUES (' Hu elder sister ', ' female ', ' + ',DEFAULT)

displaying results: Hu elder sister female (NULL)

Error wording:

INSERT Renshi VALUES (' Hu elder sister ', ' female ', ' 35 ')

Insert error: The column name or the number of supplied values does not match the table definition.

INSERT into Renshi (id,name, sex, Age, tel) VALUES (' 1 ', ' Hu elder sister ', ' female ', ' 35 ', ' 136334***12 ')

The INSERT statement cannot specify a value for an identity column because its number is automatically increased.

[insert multiple rows of data at once]

to add data from an existing table to a new table by using the Insert SELECT statement

INSERT into Renshi_ (Name,sex,age,tel)

SELECT Name,sex,age,tel

from Renshi

The number of data that is inserted in the order data type must be consistent with the inserted item.

Second add data from an existing table to a new table by using the SELECT INTO statement ( cannot be pre-existing )

SELECT Name, Sex,age,tel

into renshi__

from Renshi

The difference from the above insertion: this new table is created when executing a query statement and cannot be pre-existing.

The identity column is not allowed to be specified, so we can create a new identity column.

SELECT IDENTITY (int,1,1) as ID, name, Sex,age,tel

into renshi__

from Renshi

Triple insert by combining data with the Union keyword

The Union statement is used to combine two different data or query results into a new result set

INSERT into Renshi (name, Sex,age,tel)

SELECT ' Hu elder sister _1 ', ' female ', ' a ', ' UNION
SELECT ' Hu elder sister _2 ', ' female ', ' a ', ' UNION
SELECT ' hu elder sister _3 ', ' female ', ' a ', ' UNION
SELECT ' Hu elder sister _4 ', ' female ', ' a ', ' UNION
SELECT ' Hu elder sister _5 ', ' female ', ' 35 ', '

SQL INSERT statement Note

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.