SQL sever 基础入门(一)

//创建一个表

create table <tableName>(  <colName>  <datatype> <constraint>,
……
  );
alter table <tableName> add <colName> <datatype> //添加列
alter table <tableName> drop column <colName> //删除列


//datatype
int,smallint,tinyint,bigint   数 4字节、2字节、1字节、8字节 
char(size)       容纳固定长度的字符串,
varchar(size)     容纳可变长度的字符串,
date(yyyymmddd)     容纳日期。
numeric(p,d) 由p位数字(不包含符号、小数点)组成,小数点后面有d位数字,小数部分未满d位时自动补0,最大38位
decimal(p,d),dec(p,d) 同numeric
float(n) 可选精度浮点数,精度至少为n位数 
real 近似浮点数 类似float
boolean 布尔值true或false
date 日期输入(注意输入形式为yyyy-mm-dd)
timestamp 时间戳(不需要输入,电脑随机生成,数据更新时改变)
time 时间(格式为HH:MM:SS)
boolean sql sever 没有布尔类型 可以用bit代替


//constraint
not null                   约束强制列不接受 NULL 值。
unique                     约束唯一标识数据库表中的每条记录。
primary key               primary key 约束唯一标识数据库表中的每条记录(primary key约束的列必须是not null)
foreign key             一个表中的 fpreign key 指向另一个表中的 primary key。
CHECK                       约束用于限制列中的值的范围。
DEFAULT                     约束用于向列中插入默认值,未声明时改用默认值


/*not null*/
alter table <tableName> alter column <colName> <datatype> not null //添加“不可空”属性
alter table <tableName> alter column <colname> <datatyep> null  //撤回“不可空”属性
 
/*unique*/
alter table <tableName> add unique(<ColName>)   //后设置unique
alter table <tableName> add constraint <constraintName> unique(<ColName>) //命名unique约束
alter table <tableName> drop constraint <constraintName>  //撤销unique约束

/*primary key*/
alter table <tableName> add primary key(<ColName>) //后设置primary key
alter table <tableName> add constraint <constraintName> primary key(<ColName>) //命名primary key约束
alter table <tableName> drop constraint <constraintName>  //撤销primary key约束

/*foreign key*/
alter table <fTableName> add constraint <constraintName> foreign key(<fColName>) references <kTableName>(<kColName>) //后添加foreign key
alter table <tableName> drop constraint <constraintName>   //删除foreign key

//check
alter table <tableName> add constraint <constraintName> check(<comdition> and <condition>)
alter table <tableName> drop constraint <constraintName>

//default
alter table <tableName> add constraint <constraintName> default <value> for <comdition>
alter table <tableName> drop constraint <constraintName>


___________________________________________________________________________________________________________________________________________________________________

//训练代码
create table test( 
  col1 smallint not null unique,
  col2 int not null,
  col3 char(50) not null,
  col4 numeric(10,3),
  col5 decimal(5,2) not null,
  );


alter table test alter column col4 char not null
alter table test alter column col5 decimal null
alter table test add unique (col2)
alter table test add constraint uq_col2 unique(col2)
alter table test drop constraint uq_col2


create table orders(
id_O int not null primary key,
Id_P smallint FOREIGN KEY REFERENCES test(col1),
City varchar(255) DEFAULT 'Sandnes',
col1 char(10),
col2 varchar(20),


)
 select*from orders


alter table test add constraint ord primary key (id_0)
alter table test drop constraint ord


alter table orders add constraint ss foreign key(Id_P) references test(col1)
alter table orders drop constraint ss
alter table orders add constraint che check(id_O>0 and Id_P<2)
alter table orders drop constraint che


alter table test add col6 timestamp
alter table test drop column col3
select*from test
alter table test add col3 varchar(20)
alter table test add constraint de default 1 for col6
alter table test drop constraint de




————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//引用文章

https://www.cnblogs.com/zhengyunjia/archive/2009/11/12/1601549.html(SqlServer中decimal(numeric )、float 和 real 数据类型的区别[转])
https://www.cnblogs.com/cy920/p/5854005.html(SQL Server数据库(时间戳timestamp)类型)
http://blog.csdn.net/evilcry2012/article/details/46684323(sqlserver 语句中的default 是怎么用的)\
http://blog.csdn.net/csdn9_14/article/details/52612466(SQL Server 创建表及其约束条件)











  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值