MySQL Flashcards

1
Q

从user表查询score为100并且country为China的username,password

从user表查询score为100或者country为China的username,password

A

select username,password from user where score=100 and country=’China;

select username,password from user where score=100 or country=’China;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

说说having的作用和用法

A

having用作筛选
SELECT cust_id, COUNT() AS orders FROM orders GROUP BY cust_id HAVING COUNT() > 2

where在分组前过滤,having在分组后过滤

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

查看所有数据库

A

show databases;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

创建一个名为MYSQLDATA的数据库

A

create database MYSQLDATA;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
从products表中查询price的平均值
从products表中查询price的最大值
从products表中查询price的最小值
从products表中查询price的综合
查询price共有多少行(空也算)
查询price共有多少行(空的不算)
A
select avg(price) as avgprice from products;
select max(price) as avgprice from products;
select min(price) as avgprice from products;
select sum(price) as avgprice from products;
select count(*) from products;空的也算
select count(price) from products;空的不算
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

在user表中插入一条数据:
username:hijack
passwd:123456

在user表中插入6条数据:
username:hijack1
passwd:1
username:hijack2
passwd:2
username:hijack3
passwd:3
username:hijack4
passwd:4
username:hijack5
passwd:5
username:hijack6
passwd:6
A

insert into user (username,passwd) values (‘hijack’,’123456’);

insert into user (username,passwd) values (‘hijack1’,’1’), (‘hijack2’,’2’), (‘hijack3’,’3’), (‘hijack4’,’4’), (‘hijack5’,’5’), (‘hijack6’,’6’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

从user表中检索name,password列,按照列id降序排列

A

select name,password from user order by id desc;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

从score这个表中检索class列

A

select class from score;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

union干什么用的

A
用于合并两个或多个select语句的结果集,并消除重复行
如果要保留重复行,用union all
select column_name from table1
union
select column_name from table2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

从user表中查询id=8的username,passwd

从user表中查询score不等于100的username,passwd

A

select username,passwd from user where id=8;

select username,passwd from score where score <>100;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

从user表中查询username和passwd,结果用username—-passwd形式表示

A

select concat(username,’—-‘,passwd) from user

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

切换数据库:MYSQLDATA

A

use MYSQLDATA;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

登陆mysql
用户名:hijack
主机名:myserver
端口:9999

A

mysql -u hijack -p -h myserver -P 9999

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

从score这个表中检索所有列

A

select * from score;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

select子句的顺序是什么

A
select
from
where
group by
having
order by
limit
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

列出表score表中所有的列信息

A

show columns from score;

desc score;

17
Q

列出数据库mysqldata的所有表

A

use mysqldata;

show tables;

18
Q

从score表中检索class列,并按照列age排序

从product表中检索name,price列,并按照country,date排序

A
select class from scroe order by age;
select name,price from product order by country,date;
19
Q

从score这个表中检索class列和id列

A

select class,id from score;

20
Q

从products表中查询count和price的乘积

A

select count*price as total_price from products;

21
Q

取消LILI的存取students表的特权

A

revoke all on students from LILI CASCADE;

22
Q

使用group by时应注意哪些?

A

select id,count(*) as num_prods from tb_name group by id;
group by 可以包含任意数目的列
group by 中每个列都必须是检索列或有效的表达式(但不能使聚集函数)
group by 子句必须出现在where子句之后,order by之前。

23
Q

从user表中查询id,条件是name是han开头的

A

select id from user where name like ‘han%’
%匹配0个或多个任意字符
如果是单个字符,用_,如:
select id from user where name like ‘_an’

24
Q

用正则表达式从products表中查询prod_name,要求prod_id为’1000’或’2000’或‘3000’或’4000’或’5000’

A

select prod_name from products where prod_id regexp ‘[1-5]000’;

25
Q

从user表中查询username并去除两边的空白字符,然后显示

A

select trim(username) from user;

26
Q

删除user表中的id为10006的用户数据

A

delete from user where id=10006;

27
Q

从user表中查询score为空的username,passwd

A

select username,passwd from user where score is NULL;

28
Q

把user表中的id为10005的用户的passwd改为’123456’

A

update user set passwd=’123456’ where id=10005;

29
Q

从score表中检索class列,并且去重复

A

select distinct class from score;

30
Q

从user表中查询score在60和80之间的username,passwd

A

select username,passwd from user where score between 60 and 80;

31
Q

授予LILI有对表STUDENTS的查询权

A

grant select on students to LILI with grant option

32
Q

从表score中检索class列,限制返回不多于5行

从表score中检索class列,从第二行开始返回,返回5行数据

A
select class from score limit 5;
select class from score limit 1,5;
limit初始为0