SQL考试卷与答案2(附加对dateadd(MM,1,列名),dateadd(YY,1,列名)的区分)

笔试题
1、数据库的完整性有哪些?域完整性:not null、check、default
参照完整性:外键
实体完整性:主键、唯一、自增
2、什么是主键?信息不能重复,唯一的,不能为null,一个表中只有一个主键,用于识别每一条信息
3、写出在数据库中我们常用的3个逻辑运算符?Or and not
|| && !
4、什么是外键?通过外键可以连接另一张表,表示他们之间用于主外键关系,可以将两张表通过sql语句作为一张表进行操作
5、写出5个常见的数据库软件?Mysql sqlserver db2 sqllite oracle
6、在SQLSERVER中需要在学生表中的分数这一列添加检查约束,限制值在0到100之间,写出关键代码?sore check(sore>=0 and sore <=100)
7、内连接的语法是什么?Select * from a inner join b on a.id=b.id
8、分组查询后的数据要进行筛选可以使用哪个关键字?having
9、常见的聚合函数有哪些?(请写出5个)Sum():求总和
Avg():平均值
Max():最大值
Min():最小值
Count():求条数
10、请写出SQLSERVER中的日期函数?
时间函数: year():取年 month():取月 day():取日 dateadd(MM,1,列名):增加时间
MM为加月份,YY为加年份
机试题


1、 使用命令创建一个名字为stud的数据库
create database stud;
2、 使用建表语句创建一个student表,字段如下:
create database stud
create table student(
Id nvarchar(50),
Name nvarchar(20),
Gender nvarchar(10),
Height float,
Weight float,
Age int,
Class nvarchar(50)
)
2、使用sql在表中插入数据如下(需要上交插入数据的sql语句):
insert into student values('10001','刘备','男',175,160,25,'N0001')
insert into student values('10002','关羽','男',160,150,27,'N0001')
insert into student values('10003','孙权','男',170,150,30,'N0001')
insert into student values('10004','鲁肃','男',175,130,18,'N0002')
insert into student values('10005','庞统','男',180,140,21,'N0002')
insert into student values('10006','貂蝉','女',178,120,18,'J0001')
insert into student values('10006','孙尚香','女',175,110,20,'J0001')
insert into student values('10006','吕布','女',185,150,22,'J0001')
--3请查询出J0001班的所有学生信息?(写出sql语句)
select * from student where Class='J0001'
--4请查询出所有男学生信息?(写出sql语句)
select * from student where Gender='男'
--5请将吕布的性别修改为男?
update student set Gender='男' where Name='吕布'
--6请统计出每个班级年龄大于18岁的有多少人?(需要显示人数和班级)(写出sql语句)
select Class as '班级' ,count(Name)as '人数'from student where Age>18 group by Class