MYSQL數據庫2

3.2.8創建數據表及表相關命令

(1)create table 表名

(

       字段1 字段類型,

       字段2 字段類型,

       字段3 字段類型……

)

(2)show tables;展示當前數據庫中所有的數據表

(3)show create table 數據表。展示表的創建過程

(4)desc table;可以查看錶結構

3.2.9select查詢

(1)查詢數據表中全部的行和列

select * from 表名

select col1,col2.... from 表名

(2)查詢部分表的部分列

select col1,col3 from 表名

(3)給查詢出來的數據列設置別名

select col1 as “別名1”,col2 as ‘別名2’…from 表名

      select col1 ‘別名1’,col2 ‘別名2’….from 表名

(4)DISTINCT關鍵字的使用

       作用:消除查詢結果中的重複數據

       語法:select distinct col from 表名

       注意:要求所有的字段都相同纔會去重

(5)LIMIT關鍵字的使用

       作用:指定結果的顯示範圍

       語法:

               select * from 表名 limit m,n

                   m:起始的位置

                   n:顯示的數量

               select * from 表名 limit m

                   m:從第一條開始共顯示m條數據

3.2.10insert插入數據

(1)所有列都插入值

語法:insert into 表名 values(v1,v2,v3….)

特點:列值同數,列值同序

(2)爲指定列插入值

語法:insert into (col1,col2,col3) values(v1,v2,v3)

特點:指定順序,列值對應

(3)一次性插入多條記錄

語法:insert into 表名(co1,col2,col3…)values(v1,v2,v3),(v1,v2,v3),(v1,v3,v3)…..

3.2.11update修改數據

(1)修改指定數據

語法:update 表名 set column=xxx where expression

(2)修改全部數據

語法:update 表名 set column=xxx

3.2.12刪除數據

(1)使用delete命令刪除數據

語法:delete from 表名 where expression

(2)使用truncate命令刪除數據

語法:truncate 表名

(3)邏輯刪除

給要刪除的數據做標記,實際並未刪除

(4)delete和truncate的區別

①Delete語句刪除數據,自動編號沒有恢復到默認值。但是truncate重新設置了自動編號

通過truncate語句刪除數據表數據,不能根據條件刪除,而是一次性刪除,delete語句可以根據條件進行刪除

③truncate在清空表中數據的時候,速度要比delete語句快的多

3.2.13對列進行增、刪、改、查

(1)增加一列

       alter table table_name add col_name type

(2)刪除一列

       alter table table_name drop (column) col_name 注:column可有可無

(3)修改列的數據類型

       alter table table_name modify col_name type

(4)修改列的數據類型並且改名

       alter table table_name change old_colname new_colname type

3.2.14數據庫常用約束

(1)添加主鍵約束

語法:alter table table_name add constraint PK_col_name primary key(col_name)

(2)添加外鍵約束

語法:alter table table_name add constraint FK_con_name foreign key(col_name) references table(col_name)

(3)添加檢查約束(在mysql中檢查約束不起作用)

語法:alter table table_name add constraint CK_con_name check(expression)

(4)添加默認約束

語法:alter table table_name modify col_name type default value

(5)添加自動增長

語法:alter table table_name modify col_name type auto_increment

注意:添加此約束時,被添加的列必須爲主鍵且列中必須沒有0

(6)添加非空約束

語法:alter table table_name modify col_name type not null

(7)添加唯一約束

語法:alter table table_name add constraint UQ_col_name unique(col_name)

3.2.15條件查詢

(1)普通條件查詢

語法:select * from table_name where expression

(2)模糊查詢

語法:

①between….and….

範圍查詢:  in 、or

③like 通配符 %和_,注意:如果要查找真正的%,需要用\對%進行轉義

(3)查詢空值的運算符

is null

3.2.16數據排序

作用:對查詢出的數據進行升序或降序排列

語法:select * from table_name order by col_name asc/desc

           select * from table_name order by col_name asc/desc,col_name asc/desc

3.2.17數據分組

語法:select col1..col2.. from table_name Group by col

注意:如果使用了group by分組,那麼select不允許出現其他列,除非這些列包含在分組中。

3.2.18聚合函數

(1)作用:對多條數據做統計功能

(2)注意:在使用聚合函數後,select後不允許出現其他列,除非這些列包含在分組中或者聚合函數中

(3)常用聚合函數:

3.2.19Having語句

作用:having爲group by之後得到數據進行進一步的篩選

類似於select 和 where的關係。Where爲select前的數據進行進一步的篩選。Having爲group by後的數據進行篩選

3.2.20sql語句執行順序

(1)from 表名

(2)where

(3)group by

(4)select distinct *

(5)having

(6)order by

(7)limit

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章