SQL Server 基础语句学习(一)

使用SQL Server 08

1. Create Database 

Create Database database_name

2. Create Table

Create Table table_name
(column1_name Type, column2_name Type, …)

Tips:
(1). 选择database 并判断表如果已存在,删除原有表

Use Database_name
Go
If exists (select * from sys.objects where object_id=OBJECT_ID(N'table_name') and type in (N'U'))
Drop table table_name
--Or 
IF OBJECT_ID(N'tablename','U') IS NOT NULL
DROP TABLE table_name

Go
Create Table table_name 
(column1_name Type,
 column2_name Type,
 …)

(2). 设主键, 联合主键

Create Table table_name
(column1_name Type, Not Null Primary Key,
 column2_name Type,
 …)

Create Table table_name
(column1_name Type Not Null,
  column2_name Type Not Null,
  …,
  Constraint constraint_name Primary Key (column1_name, column2_name)
 ) 

 3. Select 

Select column_name(s) from table_name

Tips:
(1). 返回唯一值  Distinct

Select distinct column_name from table_name

(2).  排序 order by column_name, 升序 asc, 降序 desc

Select * from table_name order by column1_name, column2_name desc 

4. Insert into

Insert into table_name (column_name, column_name) values (value, value)

 

posted on 2013-06-21 13:54  百花宫  阅读(501)  评论(0编辑  收藏  举报