增
插入数据
INSERT INTO class (name,teacher_name) VALUES ('软件班','张三')
INSERT INTO class (name,teacher_name) VALUES ('软件2班',NULL)
INSERT INTO class VALUES('计算机应用班','李四')
INSERT INTO class VALUES('网络班','王五')
向学生表中插入数据
INSERT INTO student VALUES('张三','男',19,1,'0609190801')
插入多行数据
INSERT INTO class (name,teacher_name)
SELECT '软件1班','张三' UNION
SELECT '软件2班','李四' UNION
SELECT '软件3班','王五'
复制的两种方式
copy1
SELECT * INTO class2 FROM class
copy2
INSERT INTO class2(name,teacher_name) SELECT name,teacher_name FROM class
删
删除学生序号为3的信息
DELETE FROM student WHERE id = 3
删除所有学生信息DELETE
DELETE FROM student
删除所有学生信息TRUNCATE
TRUNCATE TABLE student
DELETE,TRUNCATE,DROP的区别
- DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行回滚操作。
- TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。TRUNCATE 只能对TABLE;DELETE可以是table和view
- DROP则删除整个表(结构和数据)。TRUNCATE 和DELETE只删除数据表和索引所占空间。当表被TRUNCATE 后,这个表和索引所占用的空间会恢复到初始大小,而DELETE操作不会减少表或索引所占用的空间。drop语句将表所占用的空间全释放掉。
改
更新编号为2的老师名称为 王五老师
UPDATE class SET teacher_name = '王五' WHERE id = 2
修改所有人的性别为女
UPDATE student SET sex = '女'
修改所有班级序号为1的所有同学的年龄为20和班级序号为2
UPDATE student SET age =20 ,class_id = 2 WHERE class_id = 1
将年龄大于19的全部修改为18岁
UPDATE student SET age = 18 WHERE age >19
将所有同学的年龄加一岁
UPDATE student SET age = age+1
查
查询班级表中所有的数据
SELECT * FROM class
查询班级表中指定列的记录
SELECT id,name FROM class
SELECT name,id FROM class
为查询结果的列设置别名
SELECT id AS 班级序号, 班级名称 = name FROM class
字段的拼接"班级名称(老师名称)"
SELECT name +'('+ teacher_name +')' FROM class
ALL关键字的使用
SELECT ALL * FROM class
DISTINCT(去重操作)的用法
SELECT DISTINCT teacher_name FROM class
查询前3条记录
SELECT TOP 3 * FROM class
查询前10%的记录
SELECT TOP 10 PERCENT * FROM class
查询学生信息表中所有的女生信息
SELECT * FROM student WHERE sex ='女'
查询学生信息表中除了张三之外的所有同学信息
SELECT * FROM student WHERE name <> '张三'
查询序号在10到50之间的所有同学的信息
SELECT * FROM student WHERE id BETWEEN 10 AND 50
查询序号不在10到50之间的所有同学的信息
SELECT * FROM student WHERE id NOT BETWEEN 10 AND 50
查询序号在指定列表中的学生信息
SELECT * FROM student WHERE id IN(1,3,5,6,7,12,33,35,45,88)
查询班级信息中老师姓名不为空的记录
SELECT * FROM class WHERE teacher_name IS NOT NULL
查询学生表中序号是 1 3 5 6 7 12 33 35 45 88 并且性别为女的学生信息
SELECT * FROM student WHERE id IN(1,3,5,6,7,12,33,35,45,88) AND sex = '女'
查询学生表中学号是 ‘0609’ 开头的学生信息
SELECT * FROM student WHERE stu_num LIKE '0609%'
查询学生表中学号包含 ‘08’ 的学生信息
SELECT * FROM student WHERE stu_num LIKE '%08%'
查询学生表中班级学号是个位数的学生信息
SELECT * FROM student WHERE stu_num LIKE '06090_'
查询学生表中班级学号是个位数并且是奇数的学生信息
SELECT * FROM student WHERE stu_num LIKE '06090[1,3,5,7,9]'
查询学生表中学号不是 ‘0609’ 开头的学生信息
SELECT * FROM student WHERE stu_num NOT LIKE '0609%'
按照序号倒叙输出所有的女同学的信息
SELECT * FROM student WHERE sex ='女' ORDER BY id DESC
按照班级序号升序 姓名降序排列
SELECT * FROM student ORDER BY class_id ASC ,name DESC
按照班级序号升序查询学生信息前10条
SELECT TOP 10 * FROM student ORDER BY class_id ASC
按照班级序号升序查询学生信息前10%
SELECT TOP 10 PERCENT * FROM student ORDER BY class_id ASC
查询Java成绩的总和
SELECT SUM(score) AS Java成绩总和 FROM stu_marks WHERE subject = 'Java'
查询Java成绩的平均分
SELECT AVG(score) AS Java平均成绩 FROM stu_marks WHERE subject ='Java'
查询Java的最高分
SELECT TOP 1 score FROM stu_marks WHERE subject ='Java' ORDER BY score DESC
SELECT MAX(score) AS Java最高成绩 FROM stu_marks WHERE subject ='Java'
查询Java的最低分
SELECT TOP 1 score FROM stu_marks WHERE subject ='Java' ORDER BY score ASC
SELECT MIN(score)AS Java最低成绩 FROM stu_marks WHERE subject ='Java'
查询参加Java的学生数
SELECT COUNT(*) AS 参加Java考试的学生数 FROM stu_marks WHERE subject ='Java'
查询每个科目的平均分
SELECT AVG(score) AS Java平均成绩 FROM stu_marks WHERE subject ='Java'
SELECT AVG(score) AS Java平均成绩 FROM stu_marks WHERE subject ='C语言'
查询每个科目的平均分和总分
SELECT subject,AVG(score) AS 平均分,SUM(score) AS 总成绩 FROM stu_marks GROUP BY subject
查询每个科目的平均分
SELECT subject,AVG(score) AS 平均分 FROM stu_marks GROUP BY subject
查询科目平均分>61的信息
SELECT subject,AVG(score) AS 平均分 FROM stu_marks GROUP BY subject HAVING AVG(score)>61