SQL Server additions and deletions change operation

Source: Internet
Author: User

Author: GW Load Please specify the source

--------------------------------------

I. Inserting data

(1) inserting a single tuple

INSERT into < table name >[(< column name 1>[,< column name 2> ...)]

VALUES (< value 1>[,< value 2>].);

(2) Inserting sub-query results

INSERT into < table name >[(< column name 1>[,< column name 2> ...)]

Sub-query;

-------------------------------------

--View the details of a table if you don't know the structure and constraints of the table beforehand, you can view it through Sp_hele.

Sp_help SC;

--Inserting a single piece of data

--Insert attention to data type matching in order and to meet constraints

INSERT into student values (' 2014019 ', ' d ' ma ', ' Male ', ' 20 ', ' 005 ');

INSERT into student (Sno,sname) VALUES (' 2014020 ', ' Prince ');

Insert into course values (' 009 ', ' Test class ', null,4);

--Import multiple data at once

- -Create a table

CREATE TABLE Test (Sno char (9) Primary Key,sname char (10));

--Import names and school numbers from the student table into the test table

INSERT into Test (sno,sname) select Sno,sname from student;

--View Results

SELECT * from Test;

Two. Updating data

--Grammar

UPDATE < table name >

Set < column name > = < expression >[,< Column name > = < expression;] ... [WHERE < conditions >];

Note

(1) The expression can be a specific value, calculation result, sub-query;

(2) Omit where to modify all tuples;

(3) Pay attention to ensure the consistency of data.

--View the data in the student table

SELECT * from student;

--It's a little bit of a problem when you enter this watch.

--fuck!! When did Xiahouyuan become a woman?

Update student set ssex= ' man ' where sno= ' 2014003 ';

--in fact, modifying the data in the table can also remove the data before adding

Three. Delete data

--Grammar

DELETE from < table name > [WHERE < Condition >];

Note

(1) Omit where to delete all tuples in the table

(2) Delete only the data of the table, without deleting the definition of the table

(3) Ensure that data consistency is ensured

--Delete data

Delete from student where sno= ' 2014019 ';

---------------------------------------------------------------------------------------------------------------

For beginners, sometimes when inserting, deleting, updating, there will be some exceptions and the corresponding actions will fail.

Can be caused by some constraints, see the SQL Server Constraint section for a solution to these issues.

SQL Server additions and deletions change operation

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.