SQLsrever中的视图操作和对表的操作

1,什么是虚表?有结构,无数据的表
  怎样创建虚表create view a(别名,别名,别名) as select * from student where sdept=‘计算机系’
对视图的增删改转化为基本表的增删改
注意检查插入的条件与视图的条件是否一致在视图加with check option
1,更新视图转化成对基本表的操作 update a set  sage=sage+1转化成 update a set  sage=sage+1where sdept='计算计系'
2,insert into a( sname,sage)values('张三','20')
3,delete删除表的数据,删除表a drop table a
4,alter table 表名 add column 修改表属性
alter table 表名 add 修改表属性
5,truncate a 截断即清空整个表,不能有条件不能回滚 没写入日志中
MySQL 添加列,修改列,删除列
 

ALTER TABLE:添加,修改,删除表的列,约束等表的定义。
 ?查看列:desc 表名;
?修改表名:alter table t_book rename to bbb;
?添加列:alter table 表名 add column 列名 varchar(30);
?删除列:alter table 表名 drop column 列名;
?修改列名MySQL: alter table bbb change nnnnn hh int;
?修改列名SQLServer:exec sp_rename't_student.name','nn','column';
?修改列名Oracle:lter table bbb rename column nnnnn to hh int;
?修改列属性:alter table t_book modify name varchar(22);

sp_rename:SQLServer 内置的存储过程,用与修改表的定义。
 

 

 


MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列

?查看表的字段信息:desc 表名;
?查看表的所有信息:show create table 表名;
?添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);
?添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
?删除主键约束:alter table 表名 drop primary key;
?删除外键约束:alter table 表名 drop foreign key 外键(区分大小写);
 
?修改表名:alter table t_book rename to bbb;
?添加列:alter table 表名 add column 列名 varchar(30);
?删除列:alter table 表名 drop column 列名;
?修改列名MySQL: alter table bbb change nnnnn hh int;
?修改列名SQLServer:exec sp_rename't_student.name','nn','column';


用法exec sp_rename '表名.[字段原名]','字段新名','column'?修改列名Oracle:alter table bbb rename column nnnnn to hh int;
?修改列属性:alter table t_book modify name varchar(22);
 
sp_rename:SQLServer 内置的存储过程,用与修改表的定义。


我的理解我的经验
alter修改一条属性时是改变基本表的值
alter table student drop column 列名
alter table student add  列名 加varchar();
creat view A as select*from student where sdapt="计算系" with check option
--添加主键约束(stuNo作为主键,stuInfo为表名)alter table stuInfoadd constraint PK_stuNo PRIMARY KEY (stuNo)--添加唯一约束alter table stuInfoadd constraint UQ_stuID UNIQUE (stuID)--添加默认约束(地址不填,默认为地址不详)alter table student add sdept varchar(20) DEFAULT('计算机系') --添加检查约束 要求年龄在15-40岁之间alter table stuInfoadd constraint CK_stuAge CHECK(stuAge BETWEEN 15 AND 40)添加外键约束  (表名stuMarks  stuInfo)alter table stuMarksadd constraint FK_stuNo FOREIGN KEY(stuNo) REFERENCES stuInfo(stuNo)


SQLSERVER添加修改删除各种约束
 


–添加约束
 –添加主键
 alter table student –student 表名
 add constraint PK_stuNo primary key (studentNO) –studentNo 列名
 
–添加非空
 alter table student
 add constraint LoginPwd check (LoginPwd is not null)
 
–默认约束
 alter table student
 add constraint DF_address default(‘地址不详’) for address;
 
–检查约束
 alter table Student
 add constraint CK_borndate CHECK(BornDate>= ’1980-01-01′)
 
–唯一约束
 alter table student
 add constraint UQ_identityCard Unique (identityCard)
 
–外键约束
 
alter table result
 add constraint FK_stuNo foreign key(studentNo) –外键列名
 references student(studentNo) –student 主表
 go
 
–删除约束
 –alter table student
 –drop constraint CK_borndate
<!--创建表-->
create table student(
 sno char(10) primary key,
 sname char(10) unique,
 ssex char(4) check(ssex='男'or ssex='女'),
 sage int check(sage between 10 and 100),
 sdept char(20) default ('计算机')
)
create table sc(
 sno char(10),
 cno char(10),
 score int not null,
  primary key(sno,cno),
 foreign key (sno) references student(sno),
 foreign key (cno) references course(cno),
)
create table course(
 cno char(10) primary key,
 cname char(20),
)

 

一,6个约束
1,主键约束
2,外键约束
3,非空约束
4,check约束
5,唯一约束
6,默认约束
中间表的制作
  primray key(sno,sco)
foreign key(sno) references student(sno)
foreign key(cno) references sc(cno)
二,注意在insert into数据时先填上其中的约束后才有效
注意在制作表时,插入约束,和关联主键时 的步骤

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值