1煞茫、添加字段
1、添加新字段
alter table 表名 add 字段 修飾符
mysql> alter table host add math enum('tom','mike');
mysql> alter table host add (CN int,PV int;------添加多個字段,中 間用逗號隔開噩茄。
alter table 表名 add 添加的字段(和修飾) after math; -------把添加的字段放到name后面
alter table 表名 add 添加的字段(和修飾) first; ----------把添加的字段放在第一個
2下面、修改字段數(shù)據(jù)類型、修改符
1绩聘、修改名稱沥割、數(shù)據(jù)類型、修飾符
alter table host change PV PVS int(15) after math; #change 可以修改字段名字凿菩,類型驯遇,約束,順序
2蓄髓、修改字段類型叉庐、約束、順序
alter table host modify PVS int(20) after CN; #modify 不能修改字段名字
3会喝、刪除字段
mysql>alter table host drop maxs陡叠; #drop 丟棄字段
刪除字段之前必須刪除字段屬性
mysql>alter table host drop primary key
3、插入數(shù)據(jù)(添加記錄)
字符串必須引號引起來
記錄與表頭相對應肢执,表頭與字段用逗號隔開枉阵。
1、添加一條記錄:
mysql insert into 表名(字段1预茄,字段2兴溜,字段3) values(1,‘tom’,90);
注:添加的記錄與表頭要對應
2耻陕、添加多條記錄
mysql>insert into t3(id,name,sex,age) values(2,'jack','m','19'),(3,'tom','f',20);
3拙徽、用set添加記錄
mysql> insert into t3 set id=4,name='zhangsan',sex='m',age=21;
4、更新記錄
update t3 set 修改字段 where 給誰修改诗宣;
mysql>update t3 set id=6 where name='xiaoli';
5膘怕、刪除記錄
1.刪除單條記錄
mysql>delete from t3 where id=6; #刪除那個記錄,where是條件
2.刪除所有記錄
mysql>delete from t3; #刪除表t3召庞,此時增加數(shù)據(jù)時數(shù)據(jù)在原有的基礎之上增加岛心。
trancate t3 #刪除表中所有的數(shù)據(jù),并且自增的值是從1開始篮灼;
4忘古、單表查詢
測試表:company.employee5
mysql>crete database company #創(chuàng)建一個庫
mysql> create table employee(
-> id int primary key AUTO_INCREMENT not null,
-> name varchar(30) not null,
-> sex enum('male','female') default 'male' not null,
-> hire_date date not null,
-> post varchar(50) not null,
-> job_description varchar(100),
-> salary double(15,2) not null,
-> office int,
-> dep_id int);
插入數(shù)據(jù)
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100), ('tom','male','20180203','instructor','teach',5500,501,100), ('robin','male','20180202','instructor','teach',8000,501,100), ('alice','female','20180202','instructor','teach',7200,501,100), ('tianyun','male','20180202','hr','hrcc',600,502,101), ('harry','male','20180202','hr',NULL,6000,502,101), ('emma','female','20180206','sale','salecc',20000,503,102), ('christine','female','20180205','sale','salecc',2200,503,102), ('zhuzhu','male','20180205','sale',NULL,2200,503,102), ('gougou','male','20180205','sale','',2200,503,102);
mysql> use company
語法:
mysql>select 字段1,字段2 from 表名 條件
簡單查詢:
mysql> select * from employee;
多字段查詢:
mysql> select id,name,sex from employee;
條件查詢: where
mysql> select id,name from employee where id<=3诅诱;# 查詢id小于等于3的
mysql> select id,name,salary from employee where salary>2000; #查詢salary大于2000的
設置別名: as
mysql>select id as 'id_name' from employee where id>5; #給id的值起一個別名髓堪,顯示值的表頭會是設置的別名
統(tǒng)計記錄的數(shù)量:count()
mysql>select count(*) from employee;
統(tǒng)計字段的數(shù)量:
mysql>select count(id) from employee; #統(tǒng)計id的數(shù)量
避免重復distinct:表里面的數(shù)據(jù)有相同的
mysql>select distinct post from emplyee;
字段 表名
表的復制:key不會被復制:主鍵,外鍵和索引
復制表
1、復制表結(jié)構(gòu)+記錄(key不會被復制:主鍵旦袋,外鍵和索引)
語法:create table 新表名字 select * from 舊表的名字骤菠;
mysql>create table new1 select * from emplyee;
2它改、復制單個字段:
mysql>create table new2(select id,name from employee);
3.多條件查詢: and ----和
語法: select 字段疤孕,字段2 from 表名 where 條件 and where 條件;
mysql> SELECT name,salary from employee5 where post='hr' AND salary>1000;
mysql> SELECT name,salary from employee5 where post='instructor' AND salary>1000;
4.多條件查詢: or ----或者
語法: select 字段央拖,字段2 from 表名 where 條件 or 條件祭阀;
mysql> select name from employee5 where salary>5000 and salary<10000 or dep_id=102;
mysql> select name from employee5 where salary>2000 and salary<6000 or dep_id=100;
5.關鍵字 BETWEEN AND 什么和什么之間。
mysql> SELECT name,salary FROM employee5 WHERE salary BETWEEN 5000 AND 15000;
mysql> SELECT name,salary FROM employee5 WHERE salary NOT BETWEEN 5000 AND 15000;
mysql> select name,dep_id,salary from employee5 where not salary>5000 ;
注:not 給條件取反
6.關鍵字IS NULL 空的
mysql> SELECT name,job_description FROM employee5 WHERE job_description IS NULL;
mysql> SELECT name,job_description FROM employee5 WHERE job_description IS NOT NULL; #-取反 不是null
mysql> SELECT name,job_description FROM employee5 WHERE job_description=''; #什么 都沒有==空
NULL說明:
1鲜戒、等價于沒有任何值专控、是未知數(shù)。
2遏餐、NULL與0伦腐、空字符串、空格都不同,NULL沒有分配存儲空間失都。
3柏蘑、對空值做加、減粹庞、乘咳焚、除等運算操作,結(jié)果仍為空庞溜。
4革半、比較時使用關鍵字用“is null”和“is not null”。
5流码、排序時比其他數(shù)據(jù)都杏止佟(索引默認是降序排列,小→大)漫试,所以NULL值總是排在最前赏胚。
7.關鍵字IN集合查詢
一般查詢:
mysql> SELECT name,salary FROM employee5 WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000;
IN集合查詢
mysql> SELECT name, salary FROM employee5 WHERE salary IN (4000,5000,6000,9000);
mysql> SELECT name, salary FROM employee5 WHERE salary NOT IN (4000,5000,6000,9000); #取反
8.排序查詢 order by :命令指令,在mysql是排序的意思商虐。
mysql> select name,salary from employee5 order by salary; #-默認從小到大排序觉阅。
mysql> select name,salary from employee5 order by salary desc; #降序,從大到小
9.limit 限制
mysql> select * from employee5 limit 5; #只顯示前5行
mysql> select name,salary from employee5 order by salary desc limit 0,1; #從第幾行開始秘车,打印一行
查找什么內(nèi)容從那張表里面降序排序只打印第二行典勇。
注意:
0-------默認第一行
1------第二行 依次類推...
mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 0,5; #降序,打印5行
mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 4,5; #從第5條開始叮趴,共顯 示5條
mysql> SELECT * FROM employee5 ORDER BY salary LIMIT 4,3; #默認從第5條開始顯示3 條割笙。
10.分組查詢 :group by
mysql> select count(name),post from employee5 group by post;
+-------------+------------+
| count(name) | post |
+-------------+------------+
| 2 | hr |
| 4 | instructor |
| 4 | sale |
+-------------+------------+
count可以計算字段里面有多少條記錄,如果分組會分組做計算
mysql> select count(name),group_concat(name) from employee5 where salary>5000;
查找 統(tǒng)計(條件:工資大于5000)的有幾個人(count(name)),分別是誰(group_concat(name))
+-------------+----------------------------+
| count(name) | group_concat(name) |
+-------------+----------------------------+
| 5 | tom,robin,alice,harry,emma |
+-------------+----------------------------+
11.GROUP BY和GROUP_CONCAT()函數(shù)一起使用
GROUP_CONCAT()-------組連接
mysql> SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id; #以 dep_id分的組伤溉,dep_id這個組里面都有誰
mysql> SELECT dep_id,GROUP_CONCAT(name) as emp_members FROM employee5 GROUP BY dep_id; #給組連接設置了一個別名
12.函數(shù)
max() 最大值
mysql> select max(salary) from employee5;
查詢薪水最高的人的詳細信息:
mysql> select name,sex,hire_date,post,salary,dep_id from employee5 where salary = (SELECT MAX(salary) from employee5);
min()最小值
select min(salary) from employee5;
avg()平均值
select avg(salary) from employee5;
now() 現(xiàn)在的時間
select now();
sum() 計算和
select sum(salary) from employee5 where post='sale';
5.破解密碼
root賬戶沒了或者root密碼丟失:
關閉Mysql使用下面方式進入Mysql直接修改表權限
5.6/5.7版本:
# mysqld --skip-grant-tables --user=mysql &
# mysql -uroot
mysql> UPDATE mysql.user SET authentication_string=password('QianFeng@123') WHERE user='root' AND host='localhsot';
mysql> FLUSH PRIVILEGES;
注般码、多表查詢**
1、連表 企業(yè)通用方法
-- 查詢到 每個班級的所有學員的姓名
select class.name, student.name
from class, student
where
student.class_id=class.id;
-- 查詢到 云計算1901班級的所有學員的姓名
select class.name, student.name
from class, student
where
class.name="云計算1901"
and
student.class_id=class.id;
-- 查詢到 馬邦德 是哪個班級的
select student.name as 學員姓名,class.name as 班級姓名
from student,class
where student.name='馬邦德'
and
student.class_id=class.id;
-- 查詢老師 奇哥 都負責哪些班級
select t.name,c.name
from teacher as t, class as c ,class2teacher c2t
where t.name='奇哥'
and c2t.teacher_id=t.id
and c2t.class_id=c.id;
2乱顾、mysql join語法
/*無對應關系則不顯示*/
select A.class_name, B.name
from class as A, teacher as B
Where A.id = B.class_id
/* 內(nèi)連接 無對應關系則不顯示*/
select A.class_name, B.name
from class as A inner join teacher as B
on A.id = B.class_id
/* 左連接 左邊的表(A)所有顯示板祝,如果右邊的表(B)中無對應關系,則值為null*/
select A.class_name, B.name
from class as A left join teacher as B
on A.id = B.class_id
/* 右連接 右邊的(B)表所有顯示走净,如果左邊的表(A)中無對應關系券时,則值為 NULL*/
select A.name, B.name
from class as A right join teacher as B
on A.id = B.class_id