MySQL學習筆記_8_SQL語言基礎複習,學習筆記_8_sql

來源:互聯網
上載者:User

MySQL學習筆記_8_SQL語言基礎複習,學習筆記_8_sql
SQL語言基礎複習



一、概述

SQL語句注釋方式

1)以“#”開頭直到行尾的所有內容都是注釋

2)以“--”(--後還有一個空格)開頭直到行尾的所有內容都是注釋

3)以“/*”開始,以“*/”結束的所有內容都是注釋,可用於注釋多行


二、資料庫操作

1、建立資料庫

create database db_name;

db_name命名規則:

1)名稱可由任意字母,數字,”_”或”$”組成,可以是上述任一字元作為開頭,但是不能單獨使用數字作為資料庫名稱。

2)長度限制:資料庫、表、列和索引的名稱最多64個字元,別名最多可長達256個字元。

3)不能使用MySQL關鍵字作為資料庫、表名。


2、刪除資料庫

drop database db_name; #drop下降,終止

他將不可恢複的刪除資料庫及其所有資料表,建議在使用drop database前,先對資料庫進行備份


三、表的操作

1、建立資料表

create table <表名>

(<列名> <資料類型> [<列級完整性條件約束條件>]

[,<列名> <資料類型> [<列級完整性條件約束條件>]]...

[,表級完整性條件約束條件]

);


拓展:create temporary table... #建立暫存資料表,暫存資料表在伺服器互動結束時會自動刪除


2、修改資料表

指:修改表的結構,使用alter talbe語句來修改表中列的屬性,甚至修改表的名稱

alter talbe <表名>

[add <新列名> <資料類型> [完整性條件約束]]

[drop <完整性條件約束>]

[alter column <列名> <資料類型>];   #alter修改,更改


3、刪除表

drop table table_name;

drop table if exists table_name;


四、記錄的操作

1、插入資料

insert into <表名>

[(<屬性列1>,<屬性列2>...)]

values(<常量1>[,<常量2>...])

e.g. insert into student_info(stu_id,stu_name,str_sex,str_age)

values(234,”xiaofang”,”男”,18);


2、更新記錄

update <表名>

set <列名>=<運算式>[,<列名>=<運算式>]...

[where<條件>];

說明:update語句包括set子句和where子句,set子句指定修改方式,要修改的列以及修改後的取值,where子句用於指定鑰修改的資料記錄,預設修改表中的所有記錄。更新語句的關鍵就是要設定好用於進行判斷的where條件!

e.g. update student_info set str_age=22 where stu_id = 9028;


3、刪除記錄

delete from <表名>[where <條件>];

說明:如果使用者在使用delete語句時不設定where條件,則表格中的所有記錄將被清空!

delete from student_info where stu_id = 9028;


五、查詢

select [all | distinct] <目標列表運算式>[,<目標列表運算式>] …

from <表名或視圖名>[,<表名或視圖名>]...

[where <條件運算式>]

[group by <列名1>[having <條件運算式>]]

[order by <列名2>[asc|desc]];


六、學生選課系統資料庫設計流程樣本

1、資料庫設計流程:

系統分析--->邏輯設計--->物理實現


2、系統分析



3、邏輯設計





相關文章

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.