--创建数据库
@create database StudentDB
--创建表--
--ddl create -- create table Info (
--字段 --列,列和列之间用都好隔开
---snumb int primary key not null , ---学号 类型 (int)--主键约束 snumb int identity(1,1) primary key not null, ---唯一约束 ---varchar 代表可变长度字符 ---char 代表固定长度字符 sname varchar(8), --学生姓名
sex char(2) check (sex in ('男','女')), --性别 ----检查字段
birthday datetime check(birthday address varchar(50) not null default'昆明市倘甸产业园区轿子雪山旅游开发区倘甸镇', --家庭住址 email varchar(20)check(email like '%@%.%'), -----检查字段 tel varchar(30), ID int not null unique --联系电话 -----唯一值约束 ---check (age>30 and birthday ---查看表 *代表所有列 ---- select * from Info -----删除表 drop table Info ---创建books----- create table Books ( BNO varchar(16) primary key not null check(BNO like 'B%'), ---主键 书号 Bname varchar(100) not null, ----书名 Author varchar(50) not null, ----作者 Price money not null check(Price >0), ----单价 Quantity int not null check(Quantity>0) ----库存册数 ) ------------------------------------------------- ----------------------------------引用完整性约束 ------------------------------------------------- ---------主表 学生信息表 create table student ( SId int primary key, Sname varchar (8), address varchar (20) default '昆明理工大学' ) ----------从表(外键所在的表) 成绩表 create table score ( subject varchar (20), sid int foreign key references student (SId), --与主表关联的字段必须数据类型一样,长度必须一致,名字可以不一样 score int check(score between 0 and 100) ) ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ -------------------------------------------------------------------2013.10.16- ------------------------------------------------------------------------------ -------------------------通过代码选择数据库 use StudentDB go create table Infom -----主表 ( id int not null, name varchar (10) not null, age int not null ----------直接在表中添加约束(有名字) ----constraint pk_id primary key(id) ----------直接在表中添加约束(没有名字的约束,后续不好管理) ----primary key(id) ) ------------------------------从表 create table scores ( subject varchar (20),-----科目 sid int , -----学号外键 score int , -----分数 scoreid varchar(10)--------查学分编号(唯一值约束) ) -------------------------修改表(结构),如果表中的字段漏了,添加列 go alter table Infom add address varchar (10)---------------添加address字段 alter table Infom add guidCol varchar(50) -------------添加字段 alter table infom add score numeric(6,2) --------------精度6位,小数位数两位 -----------------------修改字段 column (字段) alter table Infom alter column address varchar(50) -----------------------删除列 alter table Infom drop column address ------------------------修改表(约束:constraint) alter table Infom add constraint pk_id primary key(id) -----主键约束 ------------------------联合主键约束 alter table info add constraint pk_sno primary key(id,name) alter table infom add constraint ck_age check(age between 18 and 70) ----------check检查约束 alter table infom add constraint df_address default '昆明理工大学' for address ---------默认值约束,df_address为约束名 alter table infom ------------------guid默认值约束 add constraint df_guidCol default (newid())for guidCol--------------全球唯一 标识 alter table scores add constraint uq_scoreid unique (scoreid) ------唯一值约束 -----------------------------------------添加外键约束 alter table scores add constraint FK_scores_infom_sid foreign key (sid) references Infom(id ) -----外键约束 ------------------另一种添加外键的方法,但是不利于后续的修改 --alter table scores --add foreign key (sid) references Infom(id) -----外键约束 ----------------------------------------- -----------------------------------------删除约束 ----------------------------------------- alter table scores drop constraint uq_scoreid --------约束名称 ----------------------------------------删除表 ---------------------------------------drop table 表名 drop table scores ------------------先删从表 drop table Infom ------------------再删主表 ---------------------------------------------- ---------------------------------添加数据 ---------------------------------添加所有列 ----------------方式一 insert into Infom(id,name,age,address)-----自动编号不用写,标识列为只读 values (1,'Mary',18,'england')-------------注意,日期要用单引号引起来, select * from Infom ----------------方式二 insert into Infom values (2,'Tim',19,'China',null) ----------------添加部分列 insert into Infom(id,name,address)------not null必须填 values (3,'陈成','中国') -------------------------------插入多条数据到表中 insert into infom(id,name,age,address,guidCol) select 4,'Jack',20,'昆明理工大学 ',newid() union select 5,'Jeck',20,'昆明理工大学 ',newid() union select 6,'kack',22,'昆明理工大学 ',newid() select * from Infom ------------------------------特殊的数据添加操作- ------------------------------复制表查询(创建新表,把选中数据复制到新表中) select id,name,age,address into newtable from infom ---newtable新表的名称 select * from newtable ------------------------------复制表结构,不带数据 select ID,name,age,address into newtable2 from infom where 1=2---------------条件为假,选择0行数据 ---------------------------复制所有满足where条件的数据到新的表中(age>20) select ID,name,age,address into newtable3 from infom where age>20 -------------------------------表已经存在,只想复制数据 insert into newtable2(id,name,age,address) select ID,name,age,address from infom select * from newtable2 ----------------------------------- ----------------------------------更新数据 ----------------------------------把年龄>20的学生的名字改成“ttt” update infom set name='ttt' ------------修改的内容写在set语句 where age>20 ------------符合条件的被修改 select * from infom ---------------------------------更新多个字段 ---------------------------------把地址包含China的学生名字改为?xxx?,年龄改为?25? update infom set name='xxx' ,age=25 where address='%China%' ----------------------------------把所有学生的年龄都加5 ---------------------没有条件,代表所有行更新 update infom set age=age+5 ----------------------------------把所有年满18岁,地址包含china的学生的名字该成“张三” update infom set name='张三' where age >=18 and address ='%北京%' ------------删除所有学员 delete from infom ------------truncate 删除表中的数据 truncate table infom