MySQL學習筆記_5_SQL語言的設計與編寫(上),學習筆記_5_sql

來源:互聯網
上載者:User

MySQL學習筆記_5_SQL語言的設計與編寫(上),學習筆記_5_sql
SQL語言的設計與編寫(上)



一、SQL語句分類



資料定義語言 (Data Definition Language)(DDL):

用於定義和管理資料對象,包括資料庫、資料表、視圖、索引等。例如:CREATE、DROP、ALTER等語句。



資料操作語言(DML):【和表中的資料記錄有關的語言】

用於操作資料庫對象中所包含的資料。例如:INSERT、UPDATE、DELETE語句。



資料查詢語言(DQL):

用於查詢資料庫物件中所包含的資料,能夠進行單表查詢、串連查詢、巢狀查詢,以及集合查詢等各種複雜程度不同的資料庫查詢,並將資料返回到客戶機中顯示。例如:SELECT語句(佔60%)。



資料控制語言(DCL):

是用來管理資料庫的語言,包含系統管理權限及資料更改。例如:GRANT、REVOKE、COMMIT、ROLLBACK等語句。



二、SQL語句應用案例

1、DDL【可以按照以下格式寫入一個檔案,然後再粘貼到MySQL資料庫中】

createtable if not exists cats (

idint not null auto_increment,

pidint not null default '0',

namevarchar(30) not null default '',

desntext not null default '',

primarykey(id),

indexname(name,pid)

);

createtable if not exists products(

idint not null auto_increment,

cidint not null default 0,

namevarchar(60) not null default '',

pricedouble (7,2) not null default 0.00,

numint not null default 0,

desntext,

ptimeint not null default 0,

primarykey(id),

keypname(name,price)

);


2、DML

a)insert,插入表資料

insertinto表名([欄位列表])values(值列表),(值列表2),(值列表3),...,(值列表n);

特點:

1.如果在表名後沒有給出欄位列表,則值列表必須填充所有欄位的值,必須按表中預設的順序插入

2.所有需要寫欄位名的地方都不加單引號或雙引號,但是建議所有值都要以字元形式使用

3.建議在插入資料時,最好給出欄位列表,則值只要和欄位列表一一對應即可,可以不按表中欄位的順序

b)update表名set欄位='值'[,欄位2='值2',...,欄位n='值n'][條件]#條件指定需要更改的記錄

e.g. updatecats set pid='3' where id='1';

updatecats set pid='99' where id >= '1' && id <= '3';


c)deletefrom表名 [條件]

deletefrom cats; #清空資料表

truncatecats; #也可以清空資料表,效率更高,truncate將...截斷


d)where條件

無論更新、刪除、尋找,只要寫對條件就能準確找到要管理的一條或多條資料

【都可以使用各種運算子號,可以把欄位當作一個變數來使用】


3、DQL【select】

SELECT[ALL | DISTINCT]

{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]}

FROM表名

[WHERE...]

[GROUPBY...]

[HAVING...]

[ORDERBY ...]

[LIMITcount]

使用SELECT查詢語言,目的是可以按使用者的想法將資料查出來,將結果返回!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.