1 數據庫操做mysql
一:建立數據庫正則表達式
用法:create database 庫名;sql
例子:數據庫
create database feng; #建立一個風數據庫函數
二:查看數據庫排序
show tables; 顯示錶名稱it
show database; -- 顯示全部數據庫table
例子:date
show create database feng;#查看數據庫信息
show DATABASEs;#查看全部數據庫信息
select database();#查詢當前操做數據庫名稱select
三:選擇數據庫
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';