1、创建学生表:create table student(sno varchar(4) primary key,sname varchar(10) not null,sage decimal(3,0),ssex char(2),email varchar(20) unique,constraint ck_student_ssex_sage check(ssex in(男,女)and sage between 10 and 50)创建课程表:create table course(cno varchar(4),cname varchar(40),constraint pk_course primar
2、y key(cno)创建成绩表:create table sc ( snum varchar(4), cnum varchar(4), score decimal(4,1), constraint pk_sc primary key(snum,cnum), constraint fk_sc_snum_student foreign key(snum)references student, constraint fk_sc_cnum_cource foreign key(cnum)references course, constraint ck_sc_score check (score bet
3、ween 0 and 100) )一、 简单查询 简单的 Transact-SQL 查询只包括选择列表、 FROM 子句和WHERE 子句。它们分别说明所查询列、查询的表或视图、以及搜索条件等。 例如,下面的语句查询 testtable 表中姓名为 “张三”的nickname 字段和 email 字段。 SELECT nickname,email FROM testtable WHERE name= 张三 (一 ) 选择列表 例如,下面语句显示 testtable 表中所有列的数据 : SELECT * FROM testtable一、说明:几个简单的基本的 sql 语句 选择:select
4、* from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like %value1% -like 的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 desc 总数:select count * as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1