SQL Server 添加字段 修改字段 删除字段 语句

SQL 脚本操作

-- 添加新的字段
-- 语法  alter table 表名 add 字段名 字段类型
alter table member
add birthday datetime

alter table member
add fraction float

-- 修改字段的属性  
-- 语法  alter table 表名  alter column  字段名 字段类型
alter table member
alter column fraction int

-- 删除字段
alter table member
drop column birthday

--  添加字段约束
alter table member
add constraint CK__member__age__2C3393D0 check(age<150)

-- 添加约束(使用系统命名)
alter table t_add_money
add foreign key (card_id) references t_card(card_id)

--添加约束(自己命名) 方便删除约束
alter table t_add_money
add constraint t_1 foreign key (card_id) references t_card(card_id)

数据库 字段获取当前时间

	register_date datetime default current_timestamp,
-- 也可写成这种
	register_date datetime default getdate(),

添加约束

-- 主键约束 和自增
card_id		int identity(1,1) primary key,

-- 不为空约束
student_id  int not null ,

-- 外键约束
card_id int foreign key references T_card (card_id),

-- 校验约束
the_money money check(the_money>50 and the_money<200) ,

你可能感兴趣的:(数据库基本操作,笔记,sql,sqlserver,数据库)