mysql上机作业2

create database  test3;
use test3;


create table student(
sno int not null primary key auto_increment,
sname varchar(8)  not null,
ssex char(1) default 0,
sage smallint,
sdept varchar(20))character set gbk;


create table course(
cid int not null primary key auto_increment,
cname varchar(16) not null
)character set gbk;


create table sc(
grade int not null 
)character set gbk;


set names gbk;
insert into student (sname,ssex,sage,sdept) values('张三丰','1',22,'云计算系');
insert into student (sname,ssex,sage,sdept) values('hehe','1',19,'电子工程系');
insert into student (sname,ssex,sage,sdept) values('张三丰2','0',24,'图艺系');
insert into student (sname,ssex,sage,sdept) values('张三丰3','0',18,'图艺系');
insert into student (sname,ssex,sage,sdept) values('张三丰4','0',18,'计算机系');
insert into student (sname,ssex,sage,sdept) values('张三丰5','1',21,'通信系');
insert into student (sname,ssex,sage,sdept) values('张三丰6','0',20,'计算机系');
insert into student (sname,ssex,sage,sdept) values('Alex','0',20,'计算机系');
insert into sc values(20);
insert into sc values(90);
insert into sc values(99);
insert into sc values(77);
insert into sc values(56);
insert into sc values(54);
insert into sc values(89);
insert into sc values(66);






/*以下全部都是作业二的查询语句*/
/*1创建表*/
/*2查询全体学生的学号与姓名,用中文显示列名。*/
select sno as '学号',sname as '姓名'  from student;


/*3给学生表设置别名s,查询所有记录。*/
select * from student s;


/*4查询年龄在20以下的学生的姓名.*/
select s.sname from student s where s.sage<20;


/*5查询全体学生的姓名、年龄,要求按照年龄降序排序。*/
select s.sname,s.sage from student s order by  s.sage desc;


/*6查询年龄最大的前3个学生的姓名和年龄,或第4、5个学生*/
select sname ,sage from student order by sage desc limit 3;
select sname ,sage from student order by  sage desc limit 3,2;
 
/*7student表中的所有学生名称为"Alex"的改为"Tom"*/
update student set sname='Tom'  where sname='Alex';
  
/*8将student表中的“计算机系”修改为“云计算系”*/
update student set sdept='云计算系'  where sdept='计算机系';


/*9将成绩表中50分以下的成绩均加5分.*/
update sc s set s.grade=s.grade+5 where s.grade<50;


/*10删除student表中学号为1的的记录。*/
delete from student where sno=1;


/*11学生表按性别分组,统计人数。*/


select ssex,count(*) from student where ssex in('1','0') group by ssex;

你可能感兴趣的:(mysql)