SQL Server Database Common commands

Source: Internet
Author: User
Tags bulk insert

To create a database:

Command: Create database name;

Example: Create database student;

To delete a database:

Command: drop database name;

Example: Drop database student;

New table:

Command: CREATE TABLE table name

(Column name data type, column Name 2 ...)

Example: CREATE TABLE Student

(Sname char (), Sid Int)

To delete a table:

Command: DROP table name

Example: Drop table Student

To modify a table structure:

(Insert (new) column)

Command: ALTER TABLE name

Add new column name data type

Example: ALTER TABLE student

Add Sage int

(Delete column)

Command: ALTER TABLE name

Drop Column Name

Example: ALTER TABLE student

Drop Column SID

(Modify column type)

Command: ALTER TABLE name

ALTER COLUMN name data type

Example: ALTER TABLE student

Alter column SID float (floating point type)

(New constraint)

Command: ALTER TABLE name

ALTER COLUMN name new data type

Example: ALTER TABLE student

ALTER COLUMN PK_SID PRIMARY KEY (The new constraint type is a PRIMARY KEY constraint)

(delete constraint)

Command: ALTER TABLE name

Drop Column Name

Example: ALTER TABLE student

Drop Pk_sid

Query table content:

Command: Select the data column name to query

From table name

where filter condition (unable to filter data after grouping)

(Advanced search) "Group By column name (group)

Having filter criteria (only filtered data after grouping)

ORDER BY ordering (the final output of the control data is arranged in a positive order: ASC, Flashback: DESC) "

Example: Select SID

From student

where sid=2

The GROUP by Sid

Having sid=1

ORDER BY DESC "

Inserting data into a table: (the value must correspond to the column one by one)

command: INSERT INTO table name

(column name, column name)

Values

(Value, value)

Example: INSERT INTO table name

(Sname,sid,sage)

Values

(' Zhang San ', 12,15)

To modify data values in a table:

command: Update from table name

Set Column name = new value

Example: Update from student

Set Sname= ' John Doe '

Query mode: (BULK insert multiple data)

command: INSERT into table name (the total number of values must be the same as the total number of columns)

Select value, value, value UNION ALL

Selevt value, value, value

Example: INSERT INTO table name

Select ' Zhang San ', 15,18

Select ' John Doe ', 16,19

View:

Command: CREATE VIEW name

As

Select column

From table name

Example: CREATE VIEW students

As

Select Sname

From student

SQL Server Database Common commands

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.