Alter table 表名
Add constraint 约束名 unique(列1,列2) 为学生表的(学号,姓名)定义为唯一值约束 Alter table student Add constraint pk1 unique(sno,sname ) 建立表时创建
(1)单个列是唯一值时 Create table 表名
(列名 类型(长度) unique, ??)
(2)多个列是唯一值时 Create table 表名 (列名1 类型(长度), 列名2 类型(长度), Unique (列1,列2) 主键约束:
学生:(学号,姓名,年龄,性别,联系方式,专业)修改表: Alter table 表名
Add constraint 约束名 primary key(列名) 练习:
Alter table student
Add constraint pka primary key(sno) (7)外键约束,外码
含义:多个表之间关系的建立
前提:2个以上的表
表已经定义了主键
举例:学生存在(学生表)--→学生选课(选课表)
Alter table 表名
Alter table 选课表
Add constraint 约束名
Add constraint fk1
Foreign key(列名) references 表名(列名)
Foreign key(sno(选课表.学号)) references student(sno(学生表.学号))
问题:1、删除某个表的主键
Alter table 表名
Drop constraint 约束名
2、删除某个表内的数据
Delete from 表名
3、删除表
Drop table 表名
删除约束 Alter table 表名 Drop constraint 约束名 修改列的属性
Alter table 表名
Alter column 列名 类型 (长度) null Alter table course
Alter column ctime tinyint null Alter table course
Alter column ccredit decimal(3,1)null 删除某个表内所有记录 Delete from 表名 删除某个表内的几条记录 Delete from 表名 Where 选择条件 去掉重复行
(1)count (distinct 列名) (2)Distinct 列1,列2,
(1)验证向学生表录入自己班级学生的信息 (2)向课程表录入上个学期和本学期课程 (3)验证外键对于学生表和选课表之间的关系
向选课表录入存在学生的选课信息Y
向选课表录入不存在学生的选课信息N
120409
1、约束:非空,唯一值,检查,主键,外键
2、录入数据:(单行)
Insert into 表 values('','',??)
Insert into 表名(列一,列二,列三,??) Values ('值1','值2','值3')
3、查询语句
Select 目标列表达式 From 表名 展开目标列表达式 (1)表内所有信息的查询 Select*(列1,列2,列3,??) From 表
(2)查询学生表所有记录、
Select*from student或
Select sno,sname,sage,。。。。。。 1.给每个列定义别名
Select sno(as) 学号,sname(as) 姓名 2012-sage as 出生年份 From student 注:as可不写
2.常量字符串的使用 列。‘字符串内容’,列三 Select sno,sname,‘该学生出生年份为:’, 2012-sage Form student
查找学生表中前五名学生基本信息
数据查询语句 Select 目标列表达式 From 表名 2012411
1常量字符串:列名1,‘字符串内容,列名2’ 2查询结果前几名或前百分比 top 前n行 top n列名1,列名2 例:select top 5* from student() 查询学生表中前五行学生的学号和姓名 Select top 5 sno,sname from student 前百分比 top n percent 列名1,列名2 例查询学生表中前白分之50的记录 Select top50 percent*from student
聚合函数的使用count(列名)统计该列有多少行 例:统计学生表中的人数 Select count(sno) From student
Count(*)统计出符合条件的所有行数 查询学生表中一共有多少行记录 Select Count(*)from student
Max(列名)min(列名)avg(列名)。。。书139页 统计所有学生的平均年龄,查询结果显示为: 学号,姓名,年龄,给每个列定义别名
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库使用sql语言创建数据库的方法(3)在线全文阅读。
相关推荐: