数据库练习题汇总2

1、修改视图v_stu,查询student表中,分数不在90~100分之间的,学员的编号(sid),学员姓名(sname),分数(score),地址(address)

alter view v_stu

as

select sid,sname,score,address

from student

where not score between 90 and 100;

select * from v_stu;


2、删除视图v_stu

drop view v_stu;

select * from student;


3、创建表(复合主键)

create table stu(

    id int(4),

    name varchar(20),

    age int(3),

    primary key(id,name)

);


4、查看表结构  

desc 表名;


5、查看数据库中的全部表

show tables;


6、插入语句

insert into stu values(101,'rise',20);

insert into stu(id,name) values(102,'tom');

insert into stu values(103,'sdf',20),(104,'sff',23);

insert into stu(id,name) values(105,'gfg'),(106,'45');


7、更新语句

update stu set name='tose',age=100 where id=102;

update stu set name='trt';


8、删除表中数据

delete from stu where id=102;

delete from stu where id in(102,103,105);

delete from stu;

9、删除表

drop table 表名;


10、查询语句

(1)查询全部列数据

            select * from stu;

(2)查询指定列数据

            select id,name,age from stu;

(3)给列起别名

            select id 编号,name 姓名 from stu;

            select count(*) 数量,sum(sal) 求和 from stu;

            select id,name,(sal+comm) 总和 from emp;

(4)去掉重复的列值

            select distinct 列名 from 表名;

            select count(distinct job) from 表名;

(5)限制查询

            select * from emp limit 初始位置,行数;

            初始位置,默认值为0,表示第一条记录

            select * from emp limit 2,7;

            select * from emp order by desc limit 2,5;

(6)排序(order by)

            select id,name,sal from emp order by sal desc;

            select id,name,sal,deptno from emp order by sal desc,deptno asc;

            多列排序时,先排第一列,如果第一列,中有相同的列值时,才会进行第二列排序

(7)条件查询

            select id,name,sal from emp where sal>1000;

            select id,name from emp where sal<>1250;

            select id,name,sal from emp where sal>1000 and sal<5000;

            select id,name,sal from emp where sal>1000 or loc='北京';

            select id,name,sal from emp where sal>1000 and (sal<5000 or deptno=30);


            select id,name,sal from emp where sal<>1250;

            select id,name,sal from emp where not sal=1250;


            select * from emp where comm is null;

            select * from emp where mgr is not null;


            select id,name,sal from emp where sal between 1000 and 5000;

            select id,name,sal from emp where sal >=1000 and sal<=5000;

            select id,name,sal from emp where not sal between 1000 and 5000;


            select id,name,sal from emp where deptno in(101,202,231);

            select id,name,sal from emp where deptno=101 or deptno=202 or deptno=231;

            select id,name,sal from emp where not deptno in(101,202,231);


            select id,name from emp where name like '李%';

            select id,name from emp where name like '%李%';

            select id,name from emp where name like '%李';

            select id,name from emp where name like '_李%';

            select id,name from emp where name like '%李__';

            select id,name from emp where not name like '%李%';

(8)聚合函数

            count(*/列名)统计数量

            sum(列名)      求和

            avg(列名)       求平均数  

            min(列名)       求最小值

            max(列名)      求最大值

(9)MySQL函数

round(数值,位数)  四舍五入函数

truncate(数值,位数) 截取函数

rand(n) 随机数函数

sqrt(n)  平方根函数

mod(n,m) 取余函数

length(字符串/列名)  统计个数

select * from emp where length(ename)>5;

trim(字符串/列名) 去掉字符串/列值2端的空格

substring(参数1,参数2,参数3) 字符串截取函数

参数1:被截取的字符串/列名

参数2:从哪里开始截取

参数3:截取的个数

reverse(字符串/列名) 字符串逆序函数

concat(字符串/列名,字符串/列名,... 字符串/列名)  字符串拼接函数

select id,name,sal,comm,concat(sal,comm) sal from emp;

curdate()  获取当前系统日期

curtime()  获取当前系统时间

sysdate()  获取当前系统日期时间

year(date)  获取年份

where year(hiredate)='1981';

month(date) 获取月份

where month(hiredate)='10';


(10)分组查询

select deptno,sum(sal),count(*)

from emp

where sal>1000

group by deptno

order by count(*) desc,deptno asc;

根据某一列,把相同的列值分成一组,然后对

每一组数据使用聚合函数,聚合函数经常和分组查询一起使用。


(11)having语句

在分组查询得到结果后,再次对数据进行筛选,使用having,必须和group by一起使用

select deptno,sum(sal),avg(sal)

from emp

where not ename like '%K%'

group by deptno

having avg(sal)>2000

order by deptno desc;


11、约束

主键约束(primary key)

唯一约束(unique)

[if !supportLists]l [endif]删除唯一约束

alter table 表名 drop index key_name;

show keys from 表名\G;

默认值约束(default)

非空约束(not null)


create table stu1(

id int(4) primary key,

name varchar(20) unique,

age int(3) default 18,

address varchar(30) not null,

email varchar(50)

);

12、视图

视图就是一张虚拟表,可以通过视图来查询数据

create view v_emp

as

select * from emp;

select * from v_emp;

show tables;

select id,name,sal from v_emp

where sal>1000;


alter view v_emp

as

select * from emp where sal>1000;


insert into v_emp values(101,'rose',20);

select * from emp;


update v_emp set name='rose';

select * from emp;


delete from v_emp where name='rose';

select * from emp;


drop view视图名称;


13、查询emp表中,员工的编号,姓名,工资,奖金,工资+奖金之和

select empno,ename,sal,comm,sal+comm from emp;

select empno,ename,sal,comm,sal+ifnull(comm,0) from emp;

如果comm列中列值为空使用0代替,如果不为空,就使用列值本身。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容