1 数据库操作
一:创建数据库
用法:create database 库名;
例子:
create database feng; #创建一个风数据库
二:查看数据库
show tables; 显示表名称
show database; -- 显示所有数据库
例子:
show create database feng;#查看数据库信息
show DATABASEs;#查看所有数据库信息
select database();#查询当前操作数据库名称
三:选择数据库
use 库名;
例子:
use feng; #选择数据库
四: 删除数据库
drop database 库名;
前面说了那么多,总结总结就是这一段代码
不明白直接上这个
2 表格操作
一:新建表
语法:
create table 表名(
字段1 数据类型 约束条件,
字段1 数据类型 约束条件,
)
例子:
create table student(name varchar(250) not null);
二:删除表格:
drop table student
三: 对表格字段的修改
ALTER table student add id int not null; 对student 表格添加id字段 数据类型不能为空
ALTER TABLE t1 MODIFY salary double(10,3) not null DEFAULT 10; -- 修改字段类型与约束条件
ALTER TABLE t1 CHANGE salary sal double(10,3) not null DEFAULT 10; --修改字段名称和类型与约束条件
ALTER TABLE t1 DROP COLUMN sal; -- 删除指定字段
四:查询
1 * select * from t1; -- 查询表中所有数据
2 desc student -- 显示表结构 student是标的名字
3 show create table t1; -- 显示表的创建信息
五:复制表
create table t1 select * from student; 复制student表结构和表数据
create table t3 like student 只复制表结构
六:数据库操作:
增: insert into 表明(属性) 值()
insert into t3 (name,age) values ('小三',18); 指定字段插入
insert into t3 vlaues('化工',28,2.5);整表插入字段
insert into t3 vlaues('化工',28,2.5),('化工',28,2.5),('化工',28,2.5); 插入多条字段
insert into t1 SELECT id,name from t2; -- 复制表数据
删:
delete from t2 where age = 1113;
改
update t2 set name ='祝小凤' , salary = 100 where age =13;
查:
select * from T1;
1.简单查询
-- 查询所有
select * from person;
--查询指定字段
select name,age FROM person;
-- 别名+字段运算
select p.name,p.salary,p.salary+p.salary*0.1 as 'sum' from person as p
-- 去重复查询
select DISTINCT salary,name from person;
2.条件查询
-- 逻辑运算符 < > <= >= != <> =
-- is null ,is not null
-- and OR ()
SELECT * from person where salary >5000 or ( age <=30 AND NAME ='');
3.区间查询
SELECT * FROM person where salary >=5000 and salary<=10000;
-- 推荐使用 :
--ps:前后包含
SELECT * FROM person where salary between 5000 and 10000;
4.集合查询 in not in
SELECT * FROM person where age = 20 or age = 23 or age =30 ;
SELECT * FROM person where age not in(20,23,30);
5.模糊查询 like
SELECT * FROM person where name LIKE '%月'; -- 以什么结尾
SELECT * FROM person where name LIKE '月%'; -- 以什么开头
SELECT * FROM person where name LIKE '%月%'; -- 包含
SELECT * FROM person where name LIKE '_l%'; -- "_"表示占位符
6.排序
select * from person ORDER BY salary ASC,age desc;
-- 强制中文[排序
select * FROM person ORDER BY CONVERT(name USING GBK) ;
7.聚合函数
select MAX(salary) from person;
select MIN(salary) from person;
select AVG(salary) from person;
select SUM(salary) from person;
select COUNT(*) from person;
8.分组查询 GROUP BY HAVING
select count(id),dept_id,avg(salary) from person GROUP BY dept_id HAVING avg(salary) >=5000 ;
where 与 having区别:
#执行优先级从高到低:where > group by > having
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
#2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
9.分页查询 LIMIT
SELECT * FROM person LIMIT 2,2
10.正则表达式
SELECT * FROM person where name REGEXP '^a';
SELECT * FROM person where name REGEXP 'n$';
SELECT * FROM person where name REGEXP '.a';
SELECT * FROM person where name REGEXP '[a,e,n]';
SELECT * FROM person where name REGEXP '[^alex]';
SELECT * FROM person where name REGEXP 'a|e';
SELECT * FROM person where name REGEXP '^w.*i$';
11. SQL 语句关键字的执行顺序
执行顺序: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY ->limit
4.权限
create user 'alex'@'127.0.0.1' IDENTIFIED by '123'; -- 创建用户
grant SELECT,UPDATE,DELETE ON db1.* to 'alex'@'127.0.0.1';
GRANT all PRIVILEGES ON db1.* to 'alex'@'127.0.0.1'; -- 所有权限
FLUSH PRIVILEGES; -- 刷新权限
update mysql.user set password=password('123456') where user='root';