MySQL(一)——MySQL概述
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
/* 查询所有数据库*/
SHOW DATABASES;
/* 查询当前数据库*/
SELECT DATABASE();
CREATE DATABASE [IF NOT EXISTS] 数据库名 [DEFAULT CHARSET 字符集][COLLATE 排序规则];
DROP DATABASE[IF EXISTS]数据库名;
USE 数据库名;
/*查询当前数据库所有表*/
SHOW TABLES;
/*查询表结构*/
DESC 表名;
/*查询指定表的建表语句*/
SHOW CREATE TABLE 表名;
CREATE TABLE 表名(
字段1 字段1类型[COMMENT 字段1注释],
字段2 字段2类型[COMMENT 字段2注释],
字段3 字段3类型[COMMENT 字段3注释],
......
字段n 字段n类型[COMMENT 字段n注释]
)[COMMENT 表注释];
123.45 精度M是5,小数点前后总共的位数;标度D是2,小数点后面的位数
char:定长字符串,未占用的用空格形式占用空间
varchar:占用空间随实际需要占用的空间大小而变化,需要根据内容计算占用的空间
create table staff(
-> id int,
-> number char(10),
-> name varchar(10),
-> gender char(1),
-> age tinyint UNSIGNED,
-> card char(18),
-> year date
-> );
/* 添加字段 */
ALTER TABLE 表名 ADD 字段名 类型(长度)[COMMENT 注释][约束];
/* 修改数据类型 */
ALTER TABLE 表名 MODIFY 字符名 新数据类型(长度);
/* 修改字段名和字段类型 */
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度)[COMMENT 注释][约束];
/* 删除字段 */
ALTER TABLE 表名 DROP 字段名;
/* 修改表名 */
ALTER TABLE 表名 RENAME TO 新表名;
/* 删除表 */
DROP TABLE [IF EXISTS] 表名;
/* 删除指定表,并重新创建该表 */
TRUNCATE TABLE 表名;
SHOW DATABASES;
CREATE DATABASE 数据库名;
USE 数据库名;
SELECT DATABASE();
DROP DATABASE 数据库名;
SHOW TABLES:
CREATE TABLE 表名(字段 字段类型,字段 字段类型);
DESC 表名;
SHOW CREATE TABLE 表名;
ALTER TABLE 表名 ADD/MODIFY/CHANGE/DROP/RENAME TO...;
DROP TABLE 表名;
/* 给指定字段添加数据 */
INSERT INTO 表名(字段名1, 字段名2, ...) VALUES(值1,值2,...);
/* 给全部字段添加数据 */
INSERT INTO 表名 VALUES(值1, 值2, ...);
/* 批量添加数据 */
INSERT INTO 表名(字段名1, 字段名2, ...) VALUES(值1, 值2, ...)(值1, 值2, ...)(值1, 值2, ...)
INSERT INTO 表名 VALUES(值1, 值2, ...),(值1, 值2, ...),(值1, 值2, ...);
注意:
insert into employee(id, number, name, gender, age, card, year) values (1,'1','itcast','男',10,'123456789101234567','2000-01-01');
select * from employee;
insert into employee values (2, '2', '张无忌', '男', 18, '123456789101234566', '2005-01-01');
insert into employee values (3, '3', '韦一笑', '男', 38, '123456789101234565', '2005-01-01'),(4, '4', '赵敏', '女', 18, '123456789101234564', '2005-01-01');
UPDATE 表名 SET 字段名1=值1, 字段名2=值2,...[WHERE 条件];
注意:修改语句的条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据
update employee set name = 'itheima' where id =1;
update employee set name = '小昭',gender='女' where id = 1;
update employee set year = '2008-01-01' ;
DELETE FROM 表名 [WHERE 条件];
注意:
INSERT INTO 表名(字段1, 字段2, ...) VALUES (值1, 值2,...)[,(值1, 值2,...)];
UPDATE 表名 SET 字段1=值1, 字段2=值2 [WHERE 条件];
DELETE FROM 表名 [WHERE 条件];
查询关键字:SELECT
SELECT
字段列表
FROM
表名列表
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后条件列表
ORDER BY
排序字段列表
LIMIT
分页参数
/* 查询多个字段 */
SELECT 字段1, 字段2, 字段3... FROM 表名;
SELECT * FROM 表名;
/* 设置别名 */
SELECT 字段1 [AS 别名1], 字段2 [AS 别名2] ...FROM 表名;
/* 去除重复记录 */
SELECT DISTINCT 字段列表 FROM 表名;
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment '入职时间'
)comment '员工表';
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');
select name,workno,age from emp;
select * from emp;
select workaddress as '工作地址' from emp;
select distinct workaddress as '工作地址' from emp;
SELECT 字段列表 FROM 表名 WHERE 条件列表;
select name,age from emp where age = 88;
select * from emp where age < 20;
select * from emp where age <= 20;
select * from emp where idcard is null ;
select * from emp where idcard is not null ;
select * from emp where age != 88 ;
select * from emp where age <> 88 ;
select * from emp where age between 15 and 20;
select * from emp where age >= 15 and age <= 20;
select * from emp where gender = '女' and age < 25;
select * from emp where age = 18 or age = 20 or age = 40;
select * from emp where age in (18, 20, 40);
select * from emp where name like '__';
select * from emp where idcard like '%x';
select * from emp where idcard like '_________________x';
函数 | 功能 |
---|---|
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
注意:null值不参与所有聚合函数运算
SELECT 聚合函数(字段列表) FROM 表名;
select count(*) as '员工数量' from emp;
select avg(age) as '平均年龄' from emp;
select max(age) as '最大年龄' from emp;
select min(age) as '最小年龄' from emp;
select sum(age) as '西安地区年龄总和' from emp where workaddress = '西安';
SELECT 字段列表 FROM 表名 [WHERE 条件] GROUP BY 分组字段名 [HAVING 分组后过滤条件];
where与having区别:
select gender,count(*) from emp group by gender;
select gender,avg(age) from emp group by gender;
/* 查询年龄小于45的员工,并根据工作地址分组,获取员工数量大于等于3的工作地址 */
select workaddress,count(*) as address_count from emp where age < 45 group by workaddress having address_count >= 3;
注意:
SELECT 字段名称 FROM 表名 ORDER BY 字段1 排序方式1, 字段2 排序方式2;
排序方式:
注意:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序
select * from emp order by age asc ;
select * from emp order by entrydate desc ;
/*根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序*/
select * from emp order by age asc, entrydate desc ;
SELECT 字段列表 FROM 表名 LIMIT 起始索引,查询记录数;
注意:
/*查询第1页员工数据,每页展示10条记录*/
select * from emp limit 0,10;
/*查询第2页员工数据,每页展示10条记录 -->(页码-1)*页展示记录素*/
select * from emp limit 10,10;
select * from emp where gender = '女' and age in (20,21,22,23);
select * from emp where gender = '男' and (age between 20 and 40) and name like '___';
select gender,count(*) as '人数' from emp where age < 60 group by gender;
select name,age from emp where age <= 35 order by age asc ,entrydate desc ;
select * from emp where gender = '男' and (age between 20 and 40) order by age asc ,entrydate desc limit 0,5;
/*查询用户*/
USE mysql;
SELECT * FROM user;
/*创建用户*/
CREATE USER '用户名'@'主机名' IDENTIFIED BY '密码';
/*修改用户密码*/
ALTER USER '用户名'@'主机名' IDENTIFIED WITH mysql_native_password BY '新密码';
/*删除用户*/
DROP USER '用户名'@'主机名';
注意:
/*创建用户 itcast,只能够在当前主机localhost访问,密码为12345*/
create user 'itcast'@'localhost' identified by '12345';
/*创建用户 heima ,可以在任意主机访问该数据库,密码为123456*/
create user 'heima'@'%' identified by '123456';
/*修改用户heima的访问密码为1234*/
alter user 'heima'@'%' identified with mysql_native_password by '1234';
/*删除itacst@localhost*/
drop user 'itcast'@'localhost';
权限 | 说明 |
---|---|
ALL, ALL PRIVILEGES | 所有权限 |
SELECT | 查询数据 |
INSERT | 插入数据 |
UPDATE | 修改数据 |
DELETE | 删除数据 |
ALTER | 修改表/字段 |
DROP | 删除数据库/表/视图 |
CREATE | 创建数据库/表 |
/*查询权限*/
SHOW CRANTS FOR '用户名'@'主机名';
/*授予权限*/
GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'主机名';
/*查询权限*/
REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';
show grants for 'heima'@'%';
grant all on itcast.* to 'heima'@'%';
revoke all on itcast.* from 'heima'@'%';
注意: