/*表操作*/
/*创建表*/
create table emp
(
eid int primary key,--主键
ename sql_variant, --通用数据类型
flag timestamp --时间戳
)
/*
插入列*/
alter table emp add esex varchar(10) default 'nan'
/*插入数据*/
insert into emp(eid,ename) values(1004,'accp')
insert into emp(eid,ename) values(1005,getdate())
insert into emp(eid,ename) values(1006,3.1415926)
/*更新数据*/
update emp set ename='app' where eid=1004
/*
约束*/
create table emp
(
eid int constraint pk_eid primary key,--主键约束
ename varchar(10),
sal numeric(7,2) constraint ck_sal check (sal>0),--检查约束
selfid varchar(18) constraint uq_id unique, --唯一约束
idate datetime constraint def_data default getdate()--默认约束
)
sp_pkeys emp--查看主键
sp_help emp--查看约束
alter table emp drop CK__emp__sal__78B3EFCA--删除约束
alter table emp add constraint pk_id primary key(eid)--增加约束
/*
外键操作*/
create table dept--主表
(
did int primary key,
dname varchar(10),
)
create table emp
(
eid int primary key,
ename varchar(10),
dno int constraint fk_did foreign key references dept(did) on update cascade
/*on delete no action 无级连更新,删除时外键冲突报错并回滚delete*/
/*on update no action 无级连更新,更新时外键冲突报错并回滚update*/
/*on delete cascade 删除时允许级连更新*/
/*on update cascade 更新时允许级连更新*/
)
alter table emp add constraint fk_did foreign key (dno) references dept(did)--增加外键