create database mmm create table xxx ( code int primary key identity(1,1), name varchar(50) not null, sex varchar(50) not null, age int not null, hight decimal(18,2) not null, wight int not null, ) go insert into xxx values('佐助','男',20,175,100); insert into xxx values('鸣人','男',21,177,110); insert into xxx values('小樱','女',19,165,80); insert into xxx values('宁次','男',22,179,200); insert into xxx values('迪达拉','男',23,180,130); select *from xxx update xxx set name='我爱罗' where name='鸣人'--吧鸣人换成我爱罗 delete from xxx where code=4--删除掉第四行 select*from xxx where name like '小%'--找寻名字表里面有小的 select top 3*from xxx--只显示前三行 select top 2 code from xxx where age>19--显示表里面年龄大于19岁的两行 select *from xxx order by age--按照年龄开始排序 select distinct name from xxx--只显示名字 select *from xxx order by age,wight desc--按照年龄体重排序 select wight from xxx group by wight --按照体重排序 select age from xxx group by age --按照年龄排序 select name from xxx group by name --按照名字排序 select *from xxx where age-3<20--找寻年龄减3小于20岁的 select *from xxx where age in(19,20)--寻表里面年龄是19和20岁的 select *from xxx where name in('佐助')
--外键 :受约束的表叫外键表,约束的数据源叫主键表 --要想加外键,首先得有主键表 --要想删主键表数据,必须先删除外键表数据 --作为外键的数据源的列,必须要是一个唯一键(这一列必须是主键或者是unique) create table teacher ( tcode int primary key identity(1,1) , tname varchar(50) ) insert into teacher(tname) values('张三') select *from teacher create table student ( scode int primary key identity(1,1) , sname varchar(50), tno int references teacher(tcode) , --student表的tno项参考teacher表的tcode项,tcode项必须是主键项 cid varchar(20) unique --唯一列,不能重 unique ) insert into student values ('学生1',null,'32134124') --tno项只能输入null或张三的编号 insert into student values ('学生2',null,'321434124') insert into student values ('学生3',null,'32153124') insert into student values ( ,1,'3215g124') select *from student