--创建数据库create database 学生练习on(name = srcsharedb,filename = 'e:\stuexcise.mdf',size = 10,maxsize = unlimited,filegrowth = 10%)--日志文件log on(name = srcsharelg,filename = 'e:\stuexcise.ldf',size = 3,maxsize = unlimited,filegrowth
--创建数据库create database 学生练习on(name = srcsharedb,filename = 'e:\stuexcise.mdf',size = 10,maxsize = unlimited,filegrowth = 10%)--日志文件log on(name = srcsharelg,filename = 'e:\stuexcise.ldf',size = 3,maxsize = unlimited,filegrowth = 10%)--学生表create table 学生(stuno varchar(10) primary key,stuage int check (stuage>0 and stuage=0 and lsnmark0 and lsnhour=0 and stumark
约束规则:
1、实体完整性规则
主要是针对主键(列级和表级)的,主键约束用于唯一性表示表的记录,并且主键约束要求
该列不为空,切记是不为null而不是不为,并且要求该列不能有相同项,否则不能执行sql
语句
2、参照完整性约束,即为外键约束,主要是表示表中列和表中列的关系
语法是:foreign key(列名) references 表名(列名),
其中(列名)需加括号,表名(列名)中列名需为所引用表的主键
3、用户自定义完整性约束
包括列值非空(not null),列值唯一(unique),检查列值是否满足一个布尔表达式
(check)
需要注意的是check约束需要括号,即check(布尔值)
连接的种类:
内联接:分为等值连接和自然连接,通过比较运算符来连接
语法:select * from table1 join table2 on table1.id=table2.id
交叉连接:即产生笛卡儿积的连接
语法:select * from table1 cross join table2
外连接:
左外连接:返回左表的所有行,右表不匹配的用null表示
语法:select * from table1 left join table2 on table1.id=table2.id
右外连接:返回右表的所有行,左表不匹配的用null表示
语法:select * from table1 right join table2 on table1.id=table2.id
全连接:返回连接表的所有行
语法:select * from table1 full join table2 on table1.id=table2.id