MySQL學習筆記

來源:互聯網
上載者:User

標籤:

MySQL是一個資料庫管理系統,主要是對資料庫進行增加、修改、刪除、查詢,具有大小寫不敏感的特點(輸入中用大寫、小寫均可)

1、基本命令

MySQL可以直接通過cmd進行登入,登入命令列:mysql -u使用者名稱 -p密碼/mysql -uroot -proot

1-1、DDL(Data Definition Language 資料定義語言 (Data Definition Language))

1-1-1、基本語句

查看全部資料庫:show databases;

建立資料庫:create database 資料庫名稱;/create database name;

建立資料庫並設定資料庫的編碼:create database 資料庫名稱 default charset=‘utf8’;/create database  name default charset=‘utf8’;

查看資料庫的建立資訊:show create database 資料庫名稱;/show create database name;

刪除資料庫:drop database 資料庫名稱;/drop database name;

使用資料庫(資料庫需要使用以後才能對資料庫內的資料進行操作):use 資料庫名稱;/use name;

查看全部表(需要先使用資料庫):show tables;

查看錶的建立資訊:show create table 表名稱;/show create table name;

建立表:

  create table 表名稱(

  欄位名1 資料類型,

  欄位名2 資料類型,

  constraint fk_欄位名 foreign key 欄位名2 references 表名1(欄位名1)

  );(注意:“)”前的最後一句不能有“,”結尾)

  create table name(

  a int primary key auto_increment,

  b varchar(20) not null,

  c double(3,2) default 0,

  d char(1) check(d=’男‘ or d=’女‘),

  constraint fk_b foreign key (b) references name1(b)-- 由name1表中的b來約束name表中的b,前提name1表中的b是主鍵且資料類型與name表中的b相同

  )auto_increment=100;

修改列類型:alter table 表名 modify 列名 新類型/alter table name modify b int;

增加列:alter table 表名 add 列名 類型/alter table name add e int;

刪除列:alter table 表名 drop 列名/alter table name drop e;

修改列名:alter table 表名 change 舊列名 新列名 類型/alter table name b x int;

修改表名:alter table 表名 rename 新表名/alter table name rename name1;或者rename table 表名 to 新表名/rename table name to name1

1-1-2、約束語句

約束語句用於約束資料的類型、數值等資訊。

非空(資料不可為空):欄位後+not null;

唯一:欄位後+unique(注意:可以有多個null值);

主鍵:欄位後+primary key(注意:一個表中只能有一個主鍵,主鍵欄位非空且唯一);

自動成長:主鍵列+auto_increment,若需要從某個數開始增加可以在create table name()後+auto_increment=起始數值,如果不設定起始數值則預設從1開始(注意:只有int類型的欄位才能使用,一張表中最多有一個自增);

設定預設值:欄位後+default+預設值;

檢查約束:欄位後+check+條件(注意:mysql中對check會忽略,即寫了也是白寫,mysql是為了保持與其他資料庫的一致性才設定的check約束,其他資料庫中check約束是有用的);

外鍵約束:foreign key 建立兩張或多張表中的聯絡,以保證資料的完整性。子表的外鍵參照列只能是主表的外鍵列或者有unique約束的列。子表中外鍵約束的列的值必須在主表的被參照列的值內,即若主表中有1,2,那麼子表中只能有1或者2,不能有其他值。若主表中有值被參照,那麼主表的相應記錄不能被刪除,若要刪除需要先將子表中對應刪除後才能刪除主表中的值。

1-2、DML(Data Manipulation Language 資料操控語言)

增加資料:insert into 表名(列名1,列名2……)values(值1,值2……)/insert into name(a,b,c,d)values(1,2,3,4)。如果輸入的值是表中全部的值,可以省略表名後面的括弧,即insert into name values(a,b,c,d);

刪除資料:delete from 表名 where 列名=條件值/delete from name where a=1。如果需要刪除表格中的全部資料可以省略where語句。此外可以用truncate table name,該語句不能使用where條件限制,只能直接刪除表中的全部資料,據說刪除效率比delete語句高;

修改資料:update 表名 set 列名=新值 where 列名=條件值 and 列名like ’%條件值‘;/update name set a=1 where b=‘2’ and like ’_花%‘;where和lie語句根據實際情況使用,like後的’%‘表示多個不確定的值,‘_’表示一個不確定的字元,以上的like條件可以找出如‘無花果’、‘梅花一朵’等內容;

查詢資料:select * from 表名

 

where 列名=條件值 and/or like 列名=條件值/select * from name where name.a=1 and name.b like ‘2’,該語句用於查詢表格中滿足條件的所有值,若只需要查詢某幾列內容可以使用:select 列名1,列名2 from where 列名=條件值 and/or 列名=條件值/select name.a,name.b from where name.a=1。由於表名較長可以用簡稱,查詢出的列名也可以用簡稱:select 列名 as 別名1,列名 as 別名2 from 表名 別名3 where 列名=條件值/select x3.a as ‘編號’,x3.b as ‘內容’ from name x3 where x3.a=1,其中as可以省略。

1-3、DCL(Data Control Language 資料控制語言)

標準SQL語句:

  select 展示列(*表示全部列)

  from 表名

  where/like 條件

  group by 列名(分組)

  having 條件(必須有group by 語句才能使用)

  order by 列名(排序,預設升序,若要降序需要在語句最後加上 desc)

  limit 條件值(limit 開始行號,返回的行數。如果不寫開始行號則預設從0開始,開始行號是實際行號-1)

指定展示列:select 列名1,列名2 from 表名/select name.a,name.b from name。

列名去除重複 :select distinct 列名1,列名2 from 表名/select distinct name.a,name.b from name;

列名取別稱:由於檔案名稱較長不便於使用,可以取別稱簡化或者更改列名顯示,select 列名 as 別名1,列名 as 別名2 from 表名 別名3 /select x3.a as ‘編號’,x3.b as ‘內容’ from name x3 ,其中as可以省略;

between A and B語句:A和B之間的資料:select * from name where name.a between 1 and 10;

查詢出的結果可以嵌套使用:複製表格create table newname select * from name;複製空表格create table newname select * from name where 1=2或者create table newname like name;

彙總函式:

count(1)統計全部數量

count(distinct 列名)統計列名中有多少種類

sum(列名)對列名的內容進行求和

avg(列名)對列名內容求平均數

列名的結果用於算術計算,即可以使用+、-、*、/、%等

max(列名)找出列名中的最大值

min(列名)找出列名中的最小值

left (列名,數字)找出列名中左起‘數字’個值

right(列名,數字)找出列名中右起‘數字’個值

全錶鏈接:將兩個或多個表進行連結select * from 表名1,表名2,表名3 where 表名1.列名1=表名2.列名2 and 表名2.列名3=表名3.列名3或者select * from 表名1 inner join 表名2 on 表名1.列名1=表名2.列名2 inner join 表名3 on 表名2.列名3=表名3.列名3

外連結:左外連結left join/left outer join,右外連結right join right outer join。左外連結是左邊表全部顯示,右邊表沒有對應資訊則用null填補,右外連結是右邊表全部顯示,左邊表沒有對應資訊則用null填補。

重要語句case when...then...else...end相當於if... ...else...

MySQL學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.