1 連接其他人mysql數(shù)據(jù)庫
mysql -p root -h ip地址
2.創(chuàng)建數(shù)據(jù)庫-創(chuàng)建表-更改表結(jié)構(gòu)-更改表數(shù)據(jù)
--create database 數(shù)據(jù)庫名字 # 創(chuàng)建數(shù)據(jù)庫
--use 數(shù)據(jù)庫名 ? # 進入數(shù)據(jù)庫
--create table 表名(字段名 類型 [約束]...) 創(chuàng)建表
-----約束:主鍵:primary key 唯一:unique 非空:not null 默認值:default 檢查:通過枚舉enum 達到限制的作用
--alter table 表名 add 字段 類型 (約束) #增加表中的字段
--alter table 表名 drop 字段 #刪除表中字段
--alter table 表名 change 原 新 數(shù)據(jù)類型 約束 # 修改字段名
--刪除表: drop table 表名? # 表結(jié)構(gòu)已刪除
--增加表數(shù)據(jù):insert 表名(字段名,...) values(值1理卑,...)
--更改表數(shù)據(jù): update 表名 set 字段=新值 [where 條件]
--將手機號為null的學生 年齡-1并性別改成女
如:update student2 set age=age-1,gender='女‘ where phone is null;?
--刪除表中所有數(shù)據(jù)泞遗,并保留表結(jié)構(gòu)
delete from student2;
--刪除女生小坤坤
delete from student2 where gender='女’ and name='小坤坤‘
3.約束:外鍵 foreigh key ?
alter table employee add constraint fk_deptno foreigh key(deptno) references department(id)
添加1個約束,約束名:fk_deptno佩迟,類型foreign key,加到deptno字段上校焦,當加數(shù)據(jù)時阅虫,去department的表里的id字段看下是否有符合的數(shù)據(jù)缩宜。
#fk_deptno 給外鍵取的名字,一般以fk開頭
4. 查詢
(1)--查詢:* 表中所有字段
select * from emplyee;
(2)--查詢指定字段
select * from ename,ebonus,ebsalary from employee;
(3)--根據(jù)指定條件查詢 select 字段名 from 表名 where 條件
1) where 條件之運算符 > ?? < ? ? >= ? <= ? 不等于:<>? !=
2)between ?? and ? 限制區(qū)間
3)in() 區(qū)間? not in()?
4)null有關判斷: 字段名 is null / 字段名 is not null
IFNULL(ebonus,0)? 如果ebonus的值是null廓推,會替換成0
例如:
5)邏輯: and和? or或 not非
and 和between例:
select * from employee where ebsalary>10000 and ebsalary<15000;
select * from employee where ebsalary between 10000 and 15000;? # (包含左右兩邊)
補充-- char_length(str) 獲取字符串的長度
mysql> select * from employee where char_length(ename) = 3;
# 多個and 和or的優(yōu)先級順序:
補充 找出2017年入職的員工
mysql> select * from employee where ehiredate like '2017-%';
mysql> select * from employee where ehiredate regexp '^2017';
mysql> select * from employee where year(ehiredate)=2017;
補充 and 和 or的運算順序
in例:
將5,7,2,9的獎金設置成null:
update employee set ebonus=null where eno=2 or eno=5 or eno=7 or eno=9;
等同于:update employee set ebonus=null where eno in (2,5,7,9)
6)模糊查詢:like? 通配符:_任意1個字符 ? % 任意多個字符
例:查詢名字中有‘飛字的員工
select * from employee where ename like'%飛%’刷袍;
7)排序 order by? 默認升序,也可以降序:order by 字段 desc
降序 例:select * from employee order by eage desc;
降序 例:select * from employee where ebonus+esalary>9000 oder by ebonus+ebsalary desc;
5.LIMIT? 獲取部分記錄
(1)-- limit n : 獲取前n條記錄
例:select * from employee LIMIT 5;?
(2)limit offset,n :獲取偏離量后n個數(shù)據(jù)
OFFSET 表示的是偏移量 n本次查詢要獲取的數(shù)據(jù)
例:select * from employee limit 5,5;
例:-- 共有100樊展,15條一頁呻纹,想得到第3頁?
select * from employee limit 30,15;
例: 第五頁?
select * from employee limit 60,15;
6. distinct 去重
-- 獲取employee表中所有的職位(去除重復的)
select distinct ejob from employee;
7 as 起別名 ?使用as起別名可以輔助查詢結(jié)果得查看,起的別名可以與order by結(jié)合使用但是 在where中使用報錯
例:查詢員工姓名和總工資专缠,并按照總工資排序
select ename as 姓名,ebsalary+IFNULL(ebonus,0) as 總工資 from employee where ebsalary+IFNULL(ebonus,0)>10000 order by 總工資;
8?-- 聚合函數(shù): max()最大值,min()最小值,sum() 求和,avg()平均值,count()計數(shù) # count(*)代表返回的數(shù)據(jù)有多少行(條)默認可以都用*
例:查詢員工表的最大年齡
select eage from employee order by eage desc limit 1;
select max(eage) from employee;
例:-- 查詢3號部門有多少位員工
select count(eno) from employee where deptno=5;
select count(*) from employee where deptno=5;
例:-- 查詢姓王的員工人數(shù)
select count(*) from employee where ename like '王%';
例:-- 查詢女生的薪資總和
SELECT SUM(ebonus+ebsalary) from employee where esex='女'
9 (1)分組查詢:select 字段,... from 表名 where 條件 group by 字段 [having 字段][order by 字段] [limit n]?
-- *** where ...group by ... 先篩選再分組
-- ***? group by ... having ...? 先分組后篩選
-- having 不能獨立存在居暖,必須結(jié)合group by使用
例:-- 查詢employee表中男女的人數(shù)
select esex, count(*) as number from employee group by esex;
-- sex 人數(shù)
-- 男? 5
-- 女? 6
例:-- 查詢4號部門各個職位的最低工資低于3000元的職位
select ejob,min(ebsalary) as 最低底薪 from employee where deptno=4 group by ejob having 最低底薪<3000;
例:-- 查詢各個部門的男女生人數(shù)
select deptno,esex,count(*) from employee group by deptno,esex ORDER BY deptno;
9.(2)子查詢:嵌入到其他查詢語句中查詢語句