目錄
MySQL數據類型
int
- double
- char(n) 定長字符串,不管存幾個字符,長度就設定爲n,不夠長的話,後面用空格補全(0-255)
適合存儲數據長度固定的數據(身份證號),不會浪費空間,效率略高
- varchar(n) 不定長字符串,長度也是n,只不過不夠長的話,後面剩餘的還給數據庫,留給別的數據(0-65535)
效率略低,也不會浪費空間
- text 65535個字符
- bigtext 4G
- 日期類型
date 年月日
time 時分秒
datetime 年月日時分秒
timestamp 時間戳 1970年到指定日期之間的毫秒值,可以自動插入和更新
查詢當前所有庫
show databases;
查詢當前數據庫中所有表
第一步 use mysql //這一步不用分號
第二步 show tables;
創建數據庫
create database 數據庫名字 character utf8; //創建utf-8編碼格式的數據庫
create database if not exists 數據庫名字 character utf8; //如果該數據庫不存在,則創建utf-8編碼格式的數據庫
刪除數據庫
drop database if exists 數據庫名字 //如果數據庫存在就刪除
drop database if not exists 數據庫名字 //則創建該數據庫
創建表
use 數據庫名;
id name age 1 小明 14 2 小華 16 create table 表名(
id int,
name varchar(20),
age int
);
表建好之後,添加新的列alter,刪除列
在表建好之後,插入新的列 alter table 表名 add 列名 varchar(20); 在表建好之後,刪除列 alter table 表名 drop 列名;
查看錶結構
desc 表名;
設置約束
主鍵約束
把某一列設置爲主健,該列不能重複且不能爲空,作爲表記錄的唯一標識
create table 表名(
id int primary key, //設置主鍵約束
name varchar(20),
age int
);
唯一約束
如果爲一個列添加唯一約束,該列的值就不能重複,可以爲空
create table 表名(
id int unique, //設置唯一約束
name varchar(20),
age int
);
非空約束
如果爲一個列添加非空約束,該列的值不能爲空,可以重複
create table 表名(
id int not null, //設置非空約束
name varchar(20),
age int
);
外鍵約束
指定主鍵自增 auto_increment
create table student(id int primary key auto_increment,name varchar(20),age int);
auto_increment自增,如果我們在插入數據的時候,傳null,則從1開始,前提是數據類型是int
例如 insert into student(id,name,age)values(null,'小明',24);
插入數據
insert into 表名(id,name,age)values(12033101,'小明',24);
修改數據
update 表名 set age=26 where name='小明';
- where條件語句,如果不加where,那麼所有的數據的age都會被設置爲26
如果某一列是數值類型的,比如int,double,這時候可以對該列全部加或減操作
update 表名 set 列名=列名+數值; 例如update people set sal=sal+8000;
刪除所有數據
delete from 表名;
- 刪除所有數據之後,那設置的自增的主鍵是不會重製的,假如原來主鍵從1到3,刪除了所有數據之後,主鍵會從4開始;
- 如果想清空表,並且把主鍵也清除,要這樣寫 truncate 表名;
查詢表
基礎查詢
select * from 表名;
或者寫成
select id,name,age from student;
注意:*也是轉換成所有列,相對來說,下面的寫法效率更高
查詢表中所有數據,所有列 select name,age from student;
查詢表中所有數據,指定列 select name,age from student where gender='男'; 根據條件,查詢表中所有數據,指定列 select name,gender from people where sal>=20000 and sal<=38000;
上面的語句等價於
select name,gender from people where between 20000 and 38000;
根據區間條件,查詢表中所有數據,指定列 select name,gender from people where sal>20000 or sal<38000;
根據區間條件,查詢表中所有數據,指定列 select * from people where salary in(3000,2000);
select * from people where salary not in(3000,2000);
查詢特定數值的數據
查詢除了特定值以外的數據
select * from people where bonus is null;
select * from people where bonus is not null;
查詢某一列的值爲null的數據
查詢某一列的值不爲null的數據
select distinct *from student;
distinct去除重複數據 關於null值
假如我查詢的時候,發現某條數據有null值,這時候是查不到他的
例如:select name,salary+bonus from people where salary+bonus>0;這時候查詢到的數據
這時候就要通過 ifnull進行列判斷 ifnull(標值,替換值)
select name,salary+ifnull(bonus,0) from people where salary+ifnull(bonus,0)>0;
as起別名
select name,salary+ifnull(bonus,0) as 總薪資 from people where salary ull(bonus,0)>0;
模糊查詢 like
%表示0或多個任意字符
_下劃線表示一個任意的字符,查詢多個的話,要用多個下劃線
select * from people where name like "%喬";//查詢第二個字爲喬的數據
select * from people where name like "%喬%";//查詢名字包含喬的數據
select * from people where name like "張_";//查詢張某
select * from people where name like "張__";//查詢張某某
select * from people where name like "張___";//查詢張某某某 依次類推
聚合查詢
會自動剔除null值的數據
目前全部數據如下:
group by 只能看到每組的第一個人
select * from people group by home;
count(*)統計每組有多少行數,通過這個就可以統計每組的數量
select *,count(*) from people group by home;
max(salary) 查詢該分組中該列最大的值
min(salary) 查詢該分組中該列最小的值
select home,max(salary) from people group by home;
select home,min(salary) from people group by home;
sum(salary)求和
select sum(salary) from people;
avg(salary)求平均值
select avg(salary) from people; 或者自己計算 select sum(salary)/count(*) from people;
year(birthday) month(birthday) day(birthday) 查詢出生日期的數據
select * from people where year(birthday)>1992;
curdate()查詢當前年月日 curtime() 當前時間時分秒 sysdate()當前年月日是分秒
查詢出生年份是1992年的數據
select name,birthday from people where year(birthday)=year(curdate())-28;
having數據分組之後,再篩選
比如我要查詢每個省最低薪資中大於3000的,就要先分組,再篩選
select name,min(salary) from people group by home having min(salary)>3000;
- where:分組之前篩選,不能使用別名和聚合函數
- having:分組之後篩選,可以使用別名和聚合函數
排序
order by asc 升序排序
order by desc 降序排序
select * from people order by salary asc;
select * from people order by salary desc;
子查詢
比如我要查詢薪資比張飛高的數據,
後面加粗部分的查詢語句,用小括號括起來,作爲前面查詢語句的條件
select * from people where salary>(select salary from people where name="張飛");
首先查詢張飛的工資,然後結果作爲子條件
分頁查詢
limit後面的兩個參數 (頁碼-1)*每頁數據數量, 每頁數據數量
select * from people limit 0,2;
select * from people limit 2,2;
select * from people limit 4,2;
查詢第一頁,每頁顯示2條數據;
查詢第二頁數據,每頁顯示2條數據;
查詢第三頁數據,每頁顯示2條數據;
刪除表
use 數據庫名;
drop table if exists 表名;
查看當前在哪個庫中操作
select database();
取消SQL語句執行
在sql語句之後加 \c 這個一定要在分號前面