表操作
- DROP TABLE [IF EXISTS] 'tbl_name';
- ALTER TABLE 'tbl_name' ###增加字段
字段:
- 添加字段: add
- ADD col1 data_type [FIRST|AFTER col_name]
删除字段: drop
修改字段:
- alter(默认值) , change(字段名) , modify(字段属性)
索引:
- 添加索引: add index
- 删除索引: drop index
表选项
修改:
- 查看表上的索引: SHOW INDEXES FROM [db_name.]tbl_name;
- 查看帮助: Help ALTER TABLE
例子: 原表
MariaDB [db1]> desc students;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| sex | enum('f','m') | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 20 | |
+-------+---------------------+------+-----+---------+----------------+
增加字段后的表:
MariaDB [db1]> alter table students add mobile char(11) after name;
MariaDB [db1]> desc students;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| mobile | char(11) | YES | | NULL | |
| sex | enum('f','m') | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 20 | |
+--------+---------------------+------+-----+---------+----------------+
MariaDB [db1]> show indexes from students\G ###查看表上的索引信息
MariaDB [db1]> alter table students add index(name); ###添加索引信息
DML语句 数据表的操作
DML: INSERT, DELETE, UPDATE
insert 增加数据库
delete 删除数据库
update 修改数据库
INSERT:
一次插入一行或多行数据
语法
INSERT [L OW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] 优先级
[INTO] tbl_name [(col_name,...)] 赋值字段的顺序
{VALUES | VALUE} ({expr | DEFAULT},...),(...),... 值的顺序
[ ON DUPLICATE KEY UPDATE 如果重复更新之
col_name=expr
[, col_name=expr] ... ]
简化写法:
INSERT tbl_name [(col1,...)] VALUES (val1,...), (val21,...)
例子:
MariaDB [db1]> insert into students(name,mobile,sex,class,age)values('mage','10010','m',18,25);
MariaDB [db1]> insert into students (name,mobile,sex,class,age)values('马哥','120','f',18,45);
+----+--------+---------+------+-------+------+
| id | name | mobile | sex | class | age |
+----+--------+---------+------+-------+------+
| 1 | mage | 10010 | m | 127 | 25 |
| 2 | mage | 10010 | m | 18 | 25
6 | wang | 1380000 | m | 127 | 48 |
| 7 | 马哥 | 120 | f | 18 | 45 |
+----+--------+---------+------+-------+------+
跨数据库表复制数据方法:
MariaDB [db1]> insert into students(name,mobile,sex,age,)select name,mobile,sex,age from hellodb.students;
增加数据的另一中写法:
MariaDB [db1]> insert into students set name='han',sex='m';
MariaDB [db1]>select * from students;
UPDATE:修改语句
UPDATE:修改数据库
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
注意:一定要有限制条件,否则将修改所有行的指定字段
限制条件:WHERE LIMIT
Mysql 选项: -U|--safe-updates| --i-am-a-dummy
MariaDB [db1]> update students set sex='f' where id=9;
| 9 | han | NULL | f | NULL | 20 |
+----+--------+---------+------+-------+------+
注意:如果不指定 where 条件 则所有字段内容全部修改
修改多个字段
MariaDB [db1]> update students set name='li',sex='m',class='16' where id=3;
****为了避免用户修改文件是忘记加where条件导致数据全部修改 我们可以修改配置文件;***
[root@centos7-5 my.cnf.d]# vim mysql-clients.cnf
[mysql]
safe-updates
delete 删除语句
DELETE:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...] 排序prder by
[LIMIT row_count] 删除的指定条数;
可先排序再指定删除的行数
注意:一定要有限制条件,否则将清空表中的所有数据
限制条件:WHERE LIMIT
TRUNCATE TABLE tbl_name; 清空表
rruncate table students;
例子:
MariaDB [db1]> delete from students where id=9;
limit的用法
MariaDB [db1]> delete from students limit 3 ;
order by 的用法 先排序后删除
MariaDB [db1]> delete from students order by age limit 1 ;
先按照年龄排序,排完序,排完之后在删除。
SELECT查询语句
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[SQL_CACHE | SQL_NO_CACHE]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE | LOCK IN SHARE MODE]
- 字段显示可以使用别名:
col1 AS alias1, col2 AS alias2, ...
例子:
MariaDB [hellodb]> select stuid as 学员编号,name 姓名,age 年龄 from students;
+--------------+---------------+--------+
| 学员编号 | 姓名 | 年龄 |
+--------------+---------------+--------+
| 1 | Shi Zhongyu | 22 |
| 2 | Shi Potian | 22 |
| 3 | Xie Yanke | 53 |
| 4 | Ding Dian | 32 |
| 5 | Yu Yutong | 26 |
| 6 | Shi Qing | 46 |
| 7 | Xi Ren | 19 |
- WHERE子句:指明过滤条件以实现“选择”的功能:
过滤条件:布尔型表达式
算术操作符: +, -, *, /, %
比较操作符: =,<=>(相等或都为空) , <>, !=(非标准SQL), >, >=, <, <=
BETWEEN min_num AND max_num
IN (element1, element2, ...)
IS NULL
IS NOT NULL
例子:
where 用法
MariaDB [hellodb]> select * from students where classid=1;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
+-------+--------------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students where age > 30;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+--------------+-----+--------+---------+-----------+
> < 大于小于号用法
MariaDB [hellodb]> select * from students where age > 30 and age <50;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
+-------+--------------+-----+--------+---------+-----------+
between
MariaDB [hellodb]> select * from students where age between 30 and 50;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
+-------+--------------+-----+--------+---------+-----------+
in
MariaDB [hellodb]> select * from students where classid in (1,2,3);
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
is null
MariaDB [hellodb]> select * from students where teacherid is null ;
MariaDB [hellodb]> select * from students where teacherid is not null ;
2、DISTINCT 去除重复列
SELECT DISTINCT gender FROM students;
MariaDB [hellodb]> select distinct classid from students; ##去重
+---------+
| classid |
+---------+
| 2 |
| 1 |
| NULL |
+---------+
3、LIKE:
- %: 任意长度的任意字符
- _:任意单个字符
例子:
MariaDB [hellodb]> select * from students where name like 's%';
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+-------------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students where name like '%o';
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+-------+-----------+-----+--------+---------+-----------+
4、RLIKE:正则表达式,索引失效,不建议使用
MariaDB [hellodb]> select * from students where name rlike '^s';
5、REGEXP:匹配字符串可用正则表达式书写模式,同上
6、逻辑操作符:
NOT
AND
OR
XOR
order by 排序
ORDER BY: 根据指定的字段对查询结果进行排序
升序: ASC
降序: DESC
例子:
MariaDB [hellodb]> select * from students order by age;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
6 | Shi Qing | 46 | M | 5 | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students order by age desc; ##倒序排序
LIMIT [[offset,]row_count]:对查询的结果进行输出行数数量限制
MariaDB [hellodb]> select * from students order by name limit 3;
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 15 | Duan Yu | 19 | M | 4 | NULL |
+-------+-----------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students order by age asc limit 2,3;
****** 跳过前2个只显示后三个****
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
+-------+--------------+-----+--------+---------+-----------+
group的用法
GROUP:根据指定的条件把查询结果进行“分组”以用于做“聚合”运算
avg(), max(), min(), count(), sum()
HAVING: 对分组聚合运算后的结果指定过滤条件
count(*) ##检索到行中非空值的数目。
MariaDB [hellodb]> select classid,count(*) from students group by classid ;
+---------+----------+
| classid | count(*) |
+---------+----------+
| NULL | 2 |
| 1 | 4 |
| 2 | 3 |
| 3 | 4 |
| 4 | 4 |
| 5 | 1 |
| 6 | 4 |
| 7 | 3 |
+---------+----------+
例子:
MariaDB [hellodb]> select count(*) from students; 统计students表中的行数
MariaDB [hellodb]> select max(age) from students; 统计最大年龄
MariaDB [hellodb]> select min(age) from students; 统计最小年龄
MariaDB [hellodb]> select courseid,avg(score) from scores where courseid=2 ;
+----------+------------+
| courseid | avg(score) |
+----------+------------+
| 2 | 75.2500 |
+----------+------------+
求和
MariaDB [hellodb]> select courseid,sum(score) from scores where courseid=2 ;
+----------+------------+
| courseid | sum(score) |
+----------+------------+
| 2 | 301 |
+----------+------------+
MariaDB [hellodb]> select classid,age,avg(age) from students group by classid ;
先对班级进行分组,分完组之后在在对年龄进行求平均值 分组可以理解成去重
+---------+-----+----------+
| classid | age | avg(age) |
+---------+-----+----------+
| NULL | 27 | 63.5000 |
| 1 | 22 | 20.5000 |
| 2 | 22 | 36.0000 |
| 3 | 26 | 20.2500 |
| 4 | 32 | 24.7500 |
| 5 | 46 | 46.0000 |
| 6 | 20 | 20.7500 |
| 7 | 17 | 19.6667 |
+---------+-----+----------+
having的用法;适合在查询表中做了group by 分组后使用
MariaDB [hellodb]> select classid,avg(age) from students group by classid having classid in (1,2) ;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 1 | 20.5000 |
| 2 | 36.0000 |
+---------+----------+
查看students表中classid和年龄 ,并对年龄进行平均计算,并对classid进行分组,分组后对平均年龄进行别名的明名,最后显示平均年龄大于30的班级。
MariaDB [hellodb]> select classid,age,avg(age) as avg from students group by classid having avg > 30 ;
+---------+-----+---------+
| classid | age | avg |
+---------+-----+---------+
| NULL | 27 | 63.5000 |
| 2 | 22 | 36.0000 |
| 5 | 46 | 46.0000 |
+---------+-----+---------+
(5) 显示TeacherID非空的同学的相关信息
(6) 以年龄排序后,显示年龄最大的前10位同学的信息
(7) 查询年龄大于等于20岁,小于等于25岁的同学的信息
第五题:
MariaDB [hellodb]> select stuid,name,teacherid from students where teacherid is not null ;
第六题:
MariaDB [hellodb]> select stuid,name,age from students order by age desc limit 10 ;
+-------+--------------+-----+
| stuid | name | age |
+-------+--------------+-----+
| 25 | Sun Dasheng | 100 |
| 3 | Xie Yanke | 53 |
| 6 | Shi Qing | 46 |
| 13 | Tian Boguang | 33 |
| 4 | Ding Dian | 32 |
| 24 | Xu Xian | 27 |
| 5 | Yu Yutong | 26 |
| 17 | Lin Chong | 25 |
| 23 | Ma Chao | 23 |
| 18 | Hua Rong | 23 |
+-------+--------------+-----+
第七题;
MariaDB [hellodb]> select stuid,name,age,avg(age) from students where age >= 20 and age <=25 ;
+-------+-------------+------+----------+
| stuid | name | age | avg(age) |
+-------+-------------+------+----------+
| 1 | Shi Zhongyu | 22 | 22.1000 |
+-------+-------------+------+----------+
MariaDB [hellodb]> select classid,gender,count(*) from students group by classid,gender;
+---------+--------+----------+
| classid | gender | count(*) |
+---------+--------+----------+
| NULL | M | 2 |
| 1 | F | 2 |
| 1 | M | 2 |
| 2 | M | 3 |
| 3 | F | 3 |
| 3 | M | 1 |
| 4 | M | 4 |
| 5 | M | 1 |
| 6 | F | 3 |
| 6 | M | 1 |
| 7 | F | 2 |
| 7 | M | 1 |
+---------+--------+----------+