Fork me on GitHub

SQL创建库、创建表

常用的一些关键字:
like  模糊查询
and(between and) 和
or (in(‘’,‘’))或

between and XXX之间

drop table TableName  --删除
top x percent 百分之多少

函数(count(*)多少条字段 sum求和 avg平均值  max 最大值 min最小值 )

自增identity(1,1)
主键 primary key
不为空 not null
默认 default
唯一健 unique

FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)--外键

--default 默认值

check(len(字段)=3 )--约束  字符长度要等于三位  

check (gender= '男'or gender= '女') ,--性别

--创建数据库

use master
create database DB
on(
name='DB',
filename='E:\DB.mdf'
)
log on(
name='DB_log',
filename='E:\DB_log.mdf'
)
--创建表
use DB
create table Table1(
ID int identity(1,1)  primary key not null,--类型int  自增identity(1,1) 主键 primary key  不为空 not null
name nvarchar(10) unique not null ,--类型nvarchar(10) 唯一健 非空    default默认
age int not null ,
sex nvarchar(2)
)

--向表内添加数据
insert into Table1 values ('','19','')--单条添加
insert into Table1
select '','52','' union all   --union 连接
select '','20','' union all
select '','12','' union all
select'','20',''             --多条添加
--删除数据
delete from Table1 where ID=2  --根据id删除
--更新数据
update  Table1 set name =''  where  ID=1   --根据id修改
--查询数据
select * from Table1--查询

--方法二:

--创建数据库  创建表同上
create database[DB]
go
use [DB]  --选择到DB
create table Table1(
ID int identity(1,1)  primary key not null,--类型int    
name nvarchar(10) unique not null default '未填写' ,--类型nvarchar(10)  非空  
age int not null ,
sex nvarchar(2)
)

 

posted @ 2017-07-06 20:41  都一样*  阅读(298)  评论(0编辑  收藏  举报