標籤:
-- Mysql 數 據 庫 語 法
建立資料庫 create database 資料庫名;
建立表 create table 表名(id int,name varchar(20) );
複製表和資料 create table 複製後新的表名 select * from 舊錶名;
複製表的結構 create table 複製後新的表名 select * from where 1=0 ;
建立索引 create index 索引名 on 表名(列名);
建立視圖 create view 視圖名 as select 列名1,列名2, ..... ;
刪除表 drop table 表名;
顯示表的結構 describe 表名 ;
修改表名 alter table 新表名 rename 要修改的表名;
建立列 alter table 表名 add 列名 資料類型;
刪除列 alter table 表名 drop 列名;
修改列名或者資料類型 alter table 表名 change 列名 新列名 舊資料類型;
修改資料類型 alter table 表名 modify 列名 新資料類型;
刪除索引 drop index 索引名 on 表名 ; alter table 表名 drop index 索引名 ;
主鍵約束 例如: 第一種方法 id int primary key ;
第二種方法 表裡面內容最後寫入 primary key (id) ;
唯一約束 例如: id int unique ;
與主鍵相同 id int unique not null ;
預設約束 例如: gender char(3) default ‘男‘ ;
非空約束 id int not null ;
外鍵與外鍵約束 首先:建立一個外鍵 與另一個表產生關係
然後:constraint 外鍵約束名 foreign key(建立的外鍵名-》列名) references 另一個表名(列名-必須有主鍵約束)
區別:有外鍵表示兩表有關係, 有外鍵不一定有外鍵約束,有外鍵約束一定有外鍵。
刪除資料 先刪除從表資料 delete from 表名 where 列名 = 列名下的編號 ;
再刪除主表資料 delete from 表名 where 列名 = 列名下的編號 ;
-- 外部寫入主鍵、外鍵、外鍵約束
添加自動成長(只能主鍵) alter table 表名 列名1 列名1 資料類型 auto_increment ;
改變自動成長初始值 alter table 表名 auto_increment=1000 ;
刪除自動成長 alter table 表名 change 列名1 列名1 資料類型 ;
添加主鍵約束 第一種 alter table 表名 add constraint primary key(列名) ;
第二種 alter table 表名 change 列名1 列名1 資料類型 primary key ;
刪除主鍵約束(必須先刪自動成長) alter table 表名 drop primary key ;
添加唯一約束 alter table 表名 add unique(列名2);
刪除唯一約束 alter table 表名 drop index 列名2 ;
添加預設約束 alter table 表名 change 列名2 列名2 資料類型 default ‘男‘ ;
刪除預設約束 alter table 表名 change 列名2 列名2 資料類型 ;
添加非空約束 alter table 表名 change 列名2 列名2 資料類型 not null ;
刪除非空約束 alter table 表名 change 列名2 列名2 資料類型 ;
添加外鍵約束 alter table 表名 add constraint 外鍵約束名 foreign key(外鍵列名)references 另一個表名(列名) ;
刪除外鍵約束 alter table 表名 drop foreign key 外鍵約束名 ;
添加資料 insert into 表名(對應的列:逗號隔開)values(資料1,資料2,*******);
簡寫: insert into 表名 values(資料1,資料2,****);
修改資料 update 表名 set 列名 = 新資料where pk_id=2(加入的條件);
處理空值時: update 表名 set 列名 = 新資料 where 列名 is null ;
只刪除資料: update 表名 set 列名 = null where pk_id=2(加入的條件);
刪除資料 刪除後可以恢複 delete from 表名 where pk_id=2(刪除的條件);
truncate table 表名;
-- 查詢資料 文法
查詢表中所有資訊 select 列名1,列名2 from 表名;
select * from 表名;
表別名 select 表別名.id as ‘編號‘,表別名.name as ‘名字‘ from 表名 as ‘表別名‘ ;
列別名 select id as ‘編號‘,name as ‘名字‘ from 表名 ;
計算資料行 select age as ‘當前年齡‘,age+10 as ‘10年後的年齡‘ from 表名;
在一列顯示資料(字串拼接函數) select concat(name," ",age) from 表名;
計算長度函數 select length("內容");
多列去重複 select distinct 列名 1,列名2 from 表名;
分頁(limit) select * from 表名 limit 2; 一個參數,表示從第一行開始返回指定行數的結果。
select * from 表名 limit 2,5; 兩個參數,表示從指定行開始返回指定行數的結果;
分頁公式 (頁數-1)* 每頁顯示資料的條數
between and(在什麼之間包括上限和下限) select * from 表名 where 列名 between 18 and 20 ; 查詢年齡在18--20歲的人(包括18和20)
in (在什麼裡面) select * from 表名 where 列名 in(17,20,25); 查詢17,20,25 歲的人
select * from 表名 where age=17 or age=20 or age=20 ;
not in (不在什麼裡面) select * from 表名 where age!=15 and age!=30 ; 查詢年齡不是15和30歲的人;
select * from 表名 where age not in(15,30) ;
like 模糊查詢 select * from 表名 where 列名 like ‘張_‘ ; 查詢姓張的(兩個字)同學的資訊
select * from 表名 where 列名 like ‘__‘ ; 查詢姓名為兩個字的同學資訊
select * from 表名 where 列名 like ‘李%‘ ; 查詢以李開頭姓名的同學資訊
select * from 表名 where 列名 like ‘%趙%‘; 查詢姓名包含趙字的同學的資訊
查詢空值 is select * from 表名 where 列名 is null ; 查詢姓名為空白值的同學的資訊
排序 order by select * from 表名 order by age asc; 按年齡從小到大排列同學的資訊
select * from 表名 order by age desc; 年齡從大到小排列同學的資訊
多列排序 select * from 表名 order by age desc,chengji desc; 年齡從大到小排列,成績從高到低排列;
執行步驟:1:from 2:where 3:select 4:order by
-- 函數與分組
count(*)統計包括null值得行 select count(*) from 表名;
count(all 列名)統計不包括null的行 select count(all 列名) from 表名;
count(distinct 列名)統計不包括null的行,並且去重複 select count(distinct 列名) from 表名;
sum(列名) 對單個列求和 select sum(列名) from 表名;
sum(distinct 列名)對單個列求和(相同只求一次) select sum(distinct 列名) from 表名;
avg(列名)求某個列的平均值 select avg(列名) from 表名;
max(列名)求某個列的最大值 select max(列名) from 表名;
min(列名)求某個列的最小值 select min(列名) from 表名;
round(,2) 保留幾位小數 select round(avg(列名1),2) from 表名; 表示對列名1取平均值後保留兩位小數
分組 group by select 列名1,列名2, 彙總函式 from 表名 where 過濾條件 group by 列名1,列名2;
having與where區別 where運行在分組前,having運行在分組後
SQL語句 執行順序 1 from 2 where 3 group by(分組) 4 select(投影) 5 having 6 order by(排序) 。
字串轉整型: atoi( 要修改的名字 ) ;
整型轉字串: itoa(新字串名,要修改的名字,10);
浮點型轉字串: gcvt( 新字串名,保留的位元,要改的名字);
Mysql學習基礎文法