Complete syntax for various statements in SQL Server

Source: Internet
Author: User
Tags join min table name create database backup

The following statements are part of the MS SQL statement and are not available in Access.

SQL Category:

ddl-Data Definition language (create,alter,drop,declare)

dml-Data Manipulation Language (Select,delete,update,insert)

dcl-Data Control Language (Grant,revoke,commit,rollback)

First, briefly introduce the underlying statement:

1, Description: Create a database

CREATE DATABASE Database-name

2, Note: Delete the database

Drop Database dbname

3, Description: Backup SQL Server

Device to create backup data---

Use master

EXEC sp_addumpdevice ' disk ', ' testback ', ' C:mssql7backupmynwind_1.dat '

---start Backup

BACKUP DATABASE pubs to Testback

4, Description: Create a new table

CREATE TABLE TabName (col1 type1 [NOT NULL] [primary key],col2 type2 [NOT NULL],..)

To create a new table from an existing table:

A:create table tab_new like Tab_old (Create a new table using the old table)

B:create table tab_new as Select Col1,col2 ... from tab_old definition only

5. Description:

Delete new table: Drop table TabName

6. Description:

Add one column: Alter table tabname add column col type

Note: The column will not be deleted after it has increased. The data type can not be changed when the column in the DB2 is added, and the only change is the length of the varchar type.

7. Description:

Add primary key: Alter table TabName Add primary key (COL)

Description

Delete primary key: Alter table tabname drop primary key (COL)

8. Description:

Creating index: Create [unique] index idxname on tabname (col ...)

Deleting indexes: Drop INDEX Idxname

Note: The index is not to be changed and you want to change the rebuild must be deleted.

9. Description:

Creating a View: Create VIEW viewname AS SELECT statement

Delete view: Drop View ViewName

10, Description: A few simple basic SQL statements

Selection: SELECT * FROM table1 where

Inserting: INSERT INTO table1 (field1,field2) VALUES (value1,value2)

Delete: Delete from table1 where scope

Update: UPDATE table1 set field1=value1 where scope

Find: SELECT * FROM table1 where field1 like '%value1% '---the syntax of like is very subtle, look for information!

Sort: SELECT * from table1 ordered by FIELD1,FIELD2 [DESC]

Total: SELECT Count * as TotalCount from table1

Sum: Select SUM (field1) as Sumvalue from table1

Average: Select AVG (field1) as Avgvalue from table1

Max: Select Max (field1) as MaxValue from table1

Min: select min (field1) as MinValue from table1

11, Description: Several advanced query operation words

A:union operator

The UNION operator derives a result table by combining the other two result tables (such as TABLE1 and TABLE2) and eliminating any duplicate rows in the table. When all is used with union (that is, union ALL), duplicate rows are not eliminated. In both cases, each row of the derived table is either from TABLE1 or from TABLE2.

B:except operator

The EXCEPT operator derives a result table by including all rows that are in TABLE1 but not in TABLE2, and all duplicate rows are eliminated. When all is used with EXCEPT (EXCEPT all), duplicate rows are not eliminated.

C:intersect operator

The INTERSECT operator derives a result table by including only the rows in TABLE1 and TABLE2 and eliminates all duplicate rows. When all is used with INTERSECT (INTERSECT all), duplicate rows are not eliminated.

Note: Several query result rows that use an operator must be consistent.

12, Description: Use of external connections

A, LEFT OUTER join:

Left OUTER join (left connection): The result set includes a matching row for the join table and all rows of the left-attached table.

Sql:select a.a, A.B, A.C, B.C, B.D, B.f from a left-out JOIN b on a.a = B.C

B:right outer join:

Right outer join (right connection): The result set includes both matching connection rows for the join table and all rows of the right join table.

C:full outer join:

All outer joins include not only matching rows for symbolic join tables, but also all records in two connected tables.

Second, let's look at some good SQL statements

1, Description: Replication table (only copy structure, source table name: A new table name: B) (Access available)

Law one: SELECT * into B from a where 1<>1

Method Two: Select top 0 * into B from a

2, Description: Copy table (copy data, source table name: A target table name: B) (Access available)

Insert into B (A, B, c) select d,e,f from B;

3, note: cross-database copy of the table (specific data using absolute path) (Access available)

Insert into B (A, B, c) Select d,e,f from B in ' specific database ' where condition

Example:.. From B in ' "&server.mappath (". ") & "Data.mdb" & "where.

4, Description: Subquery (table name 1:a table name 2:b)

Select A,b,c from a where a in (select D from B) or: Select A,b,c from a where a where a (1,2,3)

5. Description: Display article, author and final reply time

Select A.title,a.username,b.adddate from Table A, (select Max (adddate) adddate from table where Table.title=a.title) b

6, Description: External connection query (table name 1:a table name 2:b)

Select A.a, A.B, A.C, B.C, B.D, B.f from-a left-out JOIN b on a.a = B.C

7, Description: Online view query (table name 1:a)

SELECT * FROM (select A,b,c from a) T where t.a > 1;

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.