常用的数据库脚本

在平时的测试工作中,经常会遇到如何检查数据正确性的问题。常用的方法时,取界面上的数据和数据库中的数据进行比较,若是一致,则功能基本上是没有问题的。
—-更新一条记录

 update custom_info set custom_name=‘test’ where custom_id=1

—更新多条记录

 update custom_info set custom_name=‘test’ ,custom_gender=‘female’ where custom_id=1

—删除记录

delete from custom_info where custom_name=‘test’ or custom_name=‘lucy’

—-删除多条记录

delete from custom_info where custom_name=‘test’ and  custom_name=‘lucy’

—-一次插入多条记录的三种方法

insert into 表名 values(‘’values1,’values2’,’values3’)

—-将数据插入到另一个表中

insert into  表名(列名) select 列名 from 表名
insert into test(custom_name) select custom_name from custom_info

注意:这种插入方式,新表要是已经存在的。
—第三种方法,将现有表中的数据插入到新表中,注意新表会自动创建
select 列名 into 新表名 from 旧表名

select custom_name into test1 from custom_info 

—查询
—查询yongh用户的年龄总和

select sum(cstomer_age) as 总年龄  from customer_infor

—查询最小的年龄

 select min(customer_age) as 最小年龄 from customer_info

—–查询最大年龄

 select max(customer_age) as 最大年龄   from  customer_info

—查询表中所有用户的平均年龄

 select avg(customer_age) as 平均年龄 from customer_info

—查询表中一共有多少条数据

select count(*) as 记录数 from customer_info

—查询含有特定字符,如abc的用户信息

select * from customer_info where name like ‘%abc%’

—查询姓赵的名字是两个字的记录

select * from customer_info where name like ‘赵_’

—添加序号列

select ROW_NUMBER() over(order by customer_id) as 序号, customer_name, customer_gender, customer_age from customer_info

—查询每个课程有多少人选择

select count(*) as totalNum from customer_info group by CourseID

—查询男生女生的平均年龄

select AVG(customer_age) as 平均年龄, student_gender as 性别 from student_info group by student_gender

—-内连接查询即多张表(比如两张表)一起查询
如 course_info 表中存储的数据格式如下:

Course_id cousrse_name
1                English
2.              Chinese
3               Math
4               Physical
5               biography

student_info 表中存储的是学生的信息,如

course_id  student_no student_name. student_gender 
1               1001             Amy               Female
2              1002             Bob             Male
3            1003             Carl             Male
4               1004            David           Male
5              1005             Evan            Female
6              1006             Fanny          Female

方法一:

select student_no student_name, student_gender, course_name course_info.Course_id from student_info inner join course_info on student_info.course_id=course_info.Course_id

方法二:

select student_no student_name, student_gender, course_name course_info.Course_id from student_info, course_info where student_info.course_id=course_info.Course_id

个人更喜欢第二种方式

—外连接
—一张表里的所有数据都被查询出来,另一张表里和它匹配的数据被查询出来,不匹配的不查询出来

select course_name, student_name from course_info left join student_info on student_info.course_id=course_info.Course_id
select course_name, student_name from student_info right join course_info on student_info.course_id=course_info.Course_id

—创建表

create database DB_Book

Use DB_Book

create table book type
{
    TypeID int primary key identity, —自增列
    TypeName varchar(50) not null,
}

Create table book
{
    BOOKID int primary key identity,  —自增列
    BOOKName varchar(50) not null,
    BOOKAuthor varchar(30) not null,
    BOOKDate varchar(50) not null,
    TypeID int foreign key references booktype(TypeID)
}

insert into  book type values(‘古典’), (‘科幻’), (‘现代’), (‘言情’) —添加信息
insert into book values (‘三国演义’, ‘赵一’, ’2010-1-1’, 1), (‘水浒传’, ‘钱二’, ’2011-1-1’, 1), (‘西游记’, ‘孙三’, ’2012-1-1’, 1),(‘红楼梦’, ‘李四’, ’2014-1-1’, 1)

—分别查询每类图书的数量

select count (*) as 数量, typeName as 类型 from book, booktype where book.typeid = booktype.typeid group by typeName
—同一张表中查询同名用户的信息
select * from customer_info a, customer_info b where a.Customer_Name = b.Customer_Name and a.customer_id <> b.customer_id 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
交互式SQL的使用 环境:WINDOWS,Microsoft SQL Server 实验要求: 1,创建Student数据库,包括Students,Courses,SC表,表结构如下: Students(SNO,SNAME,SEX,BDATE,HEIGHT,DEPARTMENT) Courses(CNO,CNAME,LHOUR,CREDIT,SEMESTER) SC(SNO,CNO,GRADE) (注:下划线表示主键,斜体表示外键),并插入一定数据。 2.完成如下的查询要求及更新的要求。 (1)查询身高大于1.80m的男生的学号和姓名; (2)查询计算机系秋季所开课程的课程号和学分数; (3)查询选修计算机系秋季所开课程的男生的姓名、课程号、学分数、成绩; (4)查询至少选修一门电机系课程的女生的姓名(假设电机系课程的课程号以EE开头); (5)查询每位学生已选修课程的门数和总平均成绩; (6)查询每门课程选课的学生人数,最高成绩,最低成绩和平均成绩; (7)查询所有课程的成绩都在80分以上的学生的姓名、学号、且按学号升序排列; (8)查询缺成绩的学生的姓名,缺成绩的课程号及其学分数; (9)查询有一门以上(含一门)三个学分以上课程的成绩低于70分的学生的姓名; (10)查询1984年~1986年出生的学生的姓名,总平均成绩及已修学分数。 (11) 在STUDENT和SC关系中,删去SNO以’01’开关的所有记录。 (12)在STUDENT关系中增加以下记录: (13)将课程CS-221的学分数增为3,讲课时数增为60 3.补充题: (1) 统计各系的男生和女生的人数。 (2) 列出学习过‘编译原理’,‘数据库’或‘体系结构’课程,且这些课程的成绩之一在90分以上的学生的名字。 (3) 列出未修选‘电子技术’课程,但选修了‘数字电路’或‘数字逻辑’课程的学生数。 (4) 按课程排序列出所有学生的成绩,尚无学生选修的课程,也需要列出,相关的学生成绩用NULL表示。 (5) 列出平均成绩最高的学生名字和成绩。(SELECT句中不得使用TOP n子句) 4.选做题:对每门课增加“先修课程”的属性,用来表示某一门课程的先修课程,每门课程应可记录多于一门的先修课程。要求: 1) 修改表结构的定义,应尽量避免数据冗余,建立必要的主键,外键。 2) 设计并插入必要的测试数据,完成以下查询: 列出有资格选修数据库课程的所有学生。(该学生已经选修过数据库课程的所有先修课,并达到合格成绩。) 注意:须设计每个查询的测试数据,并在查询之前用INSERT语句插入表中。 提交作业形式: 1) 建立Student数据库的SQL脚本,插入所有数据项的SQL脚本(包括所有的测试数据)。 2) 完成查询要求的SQL语句脚本。 3) 选做题:须提交修改数据库表定义的SQL脚本,插入测试数据的SQL脚本以及用于查询的SQL语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值