四久又、表操作

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
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市伏伯,隨后出現(xiàn)的幾起案子橘洞,更是在濱河造成了極大的恐慌,老刑警劉巖说搅,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炸枣,死亡現(xiàn)場離奇詭異,居然都是意外死亡弄唧,警方通過查閱死者的電腦和手機适肠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來套才,“玉大人迂猴,你說我怎么就攤上這事”嘲椋” “怎么了沸毁?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長傻寂。 經(jīng)常有香客問我息尺,道長,這世上最難降的妖魔是什么疾掰? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任搂誉,我火速辦了婚禮,結(jié)果婚禮上静檬,老公的妹妹穿的比我還像新娘炭懊。我一直安慰自己,他們只是感情好拂檩,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布侮腹。 她就那樣靜靜地躺著,像睡著了一般稻励。 火紅的嫁衣襯著肌膚如雪父阻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音加矛,去河邊找鬼履婉。 笑死,一個胖子當著我的面吹牛斟览,可吹牛的內(nèi)容都是我干的毁腿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼趣惠,長吁一口氣:“原來是場噩夢啊……” “哼狸棍!你這毒婦竟也來了身害?” 一聲冷哼從身側(cè)響起味悄,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎塌鸯,沒想到半個月后侍瑟,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡丙猬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年涨颜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茧球。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡庭瑰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出抢埋,到底是詐尸還是另有隱情弹灭,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布揪垄,位于F島的核電站穷吮,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏饥努。R本人自食惡果不足惜捡鱼,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望酷愧。 院中可真熱鬧驾诈,春花似錦、人聲如沸溶浴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽戳葵。三九已至就乓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背生蚁。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工噩翠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人邦投。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓伤锚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親志衣。 傳聞我的和親對象是個殘疾皇子屯援,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 一、基本增刪改查二念脯、創(chuàng)建索引的幾種方式三狞洋、單表查詢四、連表查詢 一绿店、基本增刪改查 1.數(shù)據(jù)庫的增刪改查 show ...
    清風徐來_簡閱讀 429評論 0 1
  • MySQL5.6從零開始學 第一章 初始mysql 1.1數(shù)據(jù)庫基礎 數(shù)據(jù)庫是由一批數(shù)據(jù)構(gòu)成的有序的集合吉懊,這些數(shù)據(jù)...
    星期四晚八點閱讀 1,153評論 0 4
  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,811評論 0 11
  • 關系型數(shù)據(jù)庫和SQL SQL語言的三個部分DML:Data Manipulation Language,數(shù)據(jù)操縱語...
    Awey閱讀 1,947評論 0 13
  • 一假勿、SQL速成 結(jié)構(gòu)查詢語言(SQL)是用于查詢關系數(shù)據(jù)庫的標準語言借嗽,它包括若干關鍵字和一致的語法,便于數(shù)據(jù)庫元件...
    shadow雨軒閱讀 514評論 0 3