2018-07-29學習mysql小結

來源:互聯網
上載者:User

標籤:ogr   roo   支援   開啟   top   too   block   rom   sql語句   

    此前在校學習期間,只是知道資料庫很重要,但是並未主動去學習瞭解。現在的學習過程中學了一遍mysql,就簡單的做一個總結吧。

    首先記住三個概念:

    1.資料庫(Database)是按照資料結構來組織、儲存和管理資料的建立在電腦存放裝置上的倉庫。   

    2.SQL :結構化查詢語言 (SQL)(Structured Query Language)

    3.MySQL:關係型資料庫管理系統

    database中儲存著各種資料,sql語句用於從database中找出我們需要的資料,mysql是一種應用軟體,通過語句對database進行操作。

    MySQL我使用的是5.6版本,通過管理員身份開啟cmd後,啟用mysql服務為:net start mysql56,關閉服務為:net stop mysql56。登入:mysql -h localhost -u root -p     斷行符號後輸入密碼:123456(使用者名稱和密碼在安裝時進行設定)

    下面將會從四個方面進行總結:

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

    2.資料操作語言(DML)

    3.資料查詢語言(DQL)

    4.函數

    本篇小結大部分是語句格式,如果需要代碼實現的,以及部分額外知識點標註,可以下載安裝xmind軟體後,下載雲端硬碟裡的思維導圖進行查看。

連結:https://pan.baidu.com/s/18a3gY9Rzu7TOox-Tkgq-_w 密碼:qrie

ximd內容如下:


 資料定義語言 (Data Definition Language)(DDL):1.建表:

create table 表名(                                                    create table test(

          列名稱1 資料類型 ,                                                           id int,

          列名稱2 資料類型,                                                            name char(10),

          .........                                                                                    ......

          列名稱n 資料類型);                                                       birthday date);

常用的資料類型:整數(int,tinyint,smallint),小數(float,decimal),字串(char,varchar),時間(date,time)

2.約束:

primary key(主鍵):約束唯一標識資料庫表中的每條記錄

foreign key(外鍵):foreign key就是表與表之間的某種約定的關係,從一個表指向另一個表

unique:約束用於限制加入表的資料的類型

建表時可在資料類型後添加:

create table test(id int primary key);

create table test(id int,primary key(id);

或者建表時未添加,後面需要時再添加:

alter table test add primary key(id);

刪除主鍵:alter table test drop primary key;

unique的用法與primary key 相同。

外鍵的用法:

create table t1(id int primary key,name char(10));

create table t2(id int primary key,pri_id int,name char(10),

constraint i foreign key(pri_id) references t1(id));

意思為:把t2中的pri_id 作為外鍵,指向t1中的主鍵id

刪除外鍵:constraint i,把外鍵命名成i,方便了我們刪除外鍵

alter table t2 drop foreign key i; 

3.欄位屬性

1.unsigned(無符號型),只能用在數值型欄位

2.zerofill(自動補零),只能用在數值型欄位,前置字元為零,同時該欄位自動式UNSIGNED

3.AUTO_INCREMENT(自動成長),寄生於主鍵

4.NOT NULL:強制限制式列不守NULL值,即不添加數值就無法插入資料

5.預設值(default):給資料一個預設值(系統預設值是NULL),不能與auto_increment同時用於一個欄位上

寫法均為:create table test(id int 欄位屬性);

 

DML(資料操作語言):1.索引:

建立:create index 索引名 on 表名(欄位);            create index i on test(id);

刪除:drop index 索引名;                                        drop index i;

2.對資料操作:insert(插入):

插入單獨資料:insert into 表名 欄位1、欄位2... values(值1、值2...);

插入預設資料:insert inro 表名 values(值1、值2...);

insert into test(id,name) values(008,‘周星星‘);

update(修改、更新):修改(更新)資料:update 表名 set 欄位=新值 where 列名稱=某值

update test set name=‘詹姆斯‘ where id=008;

修改多條資料:

update test set name=case id

when 001 then ‘qwe‘

when 002 then ‘asd‘

end

where id in(001,002)

replace(批次更新資料)(也可用於資料添加):replace into 表名(欄位1、欄位2...) values(值1、值2...)

replace into test(id,name) values(002,‘777‘);

未添加的資料會預設為null

批次更新(通過update):

insert into test(id,name) values(001,‘i am 001‘),(002,‘i am 002‘)

on duplicate key update

id=values(id),name=values(name);

delete刪除資料(一條):delete from 表名 where 條件

delete from test where id=001;

 3.對錶操作修改表名:alter table 舊錶名 rename as 新表名;

alter table test rename as father;

修改欄位的資料類型:alter table 表名 modify 欄位名 資料類型

alter table test modify id char(10);

增加欄位:alter table 表名 add 欄位名1 資料類型

alter table test add address char(30);

刪除欄位:alter table 表名 drop 欄位名;

alter table test drop address;

 

DQL(資料查詢語句insert):1.交叉查詢:select 表1.欄位,表2.欄位 from 表1,表2;多表聯集查詢:select 表1.欄位,表2.欄位 from 表1,表2 where 表1.id=表2.id;

select * from t1,t2 where t1.id=t2.id;

2.查詢不重複的資料:select distinct 欄位 from 表名;

select distinct id from test;

3.取別名alias:select * from 表名 as 別名;

自串連:select b.* from shop as a,shop as b where a.name=‘包子‘ and a.price

把shop表取別名為a,b,通過a和b進行自己資料的對比

4.limit(位移量):

查詢前n條資料:select *from 表名 limit n;

查詢前n條資料後的i條資料:select *from 表名 limit n,i;

5.in(在where後規定多個值):select 欄位 from 表名 where 欄位 in(值1,值2);

select * from test where id in(1,2,3);

6.like(用於where子句中搜尋列中的指定模式):select 欄位 from 表名 where 欄位 like 運算式;

例如搜尋王姓青年:

select *from test where name like‘王%‘;

%:表示0~多個字元

_:表示一個字元

7.order by(排序):select 欄位 from 表名 order by 欄位(排序方式) [desc](使用倒序排列)

select * from test order by id desc;

表示通過id從大到小進行排序顯示

8.join串連:

inner jor(內串連),left join(左串連),right join(右串連)

full join(全串連)(mysql不支援全串連)

格式都相同:select 欄位 from 表1 inner/left/right join 表2 on 表1.欄位=表2.欄位;

9.union(聯集查詢):select *from 表1 union select *from 表2;

 

函數:1.合集合函式:操作面向一系列的值,並返回一個單一的值

寫法都相同:

查詢某列的平均值:select avg(欄位) from 表名;

返回某列的行數:select count(欄位) from 表名;

查詢某列最大值:select max(欄位) from 表名;

查詢某列最小值:select min(欄位) from 表名;

返回某列總和:select sum(欄位) from 表名;

分組顯示總和:select sum(欄位) from 表名 group by 欄位;

2.純量涵式:操作面向某個單一的值,並返回基於輸入值的一個單一的值

寫法也都相同:

ucase(把欄位的值轉換為大寫):select ucase(欄位) from 表名;

lcase(把欄位的值轉換為小寫):select lcase(欄位) from 表名;

mid(提取字元):mid(欄位,起始,結束):

select mid(name,2,3) from test;

表示從name列的第二個資料開始,每個資料只顯示3位

len(返迴文本長度):select length(欄位) from 表名;

round(把數值四捨五入,並保留相應小數位):select round(欄位,數字) from 表名;

now()(查詢目前時間):select now() from 表名;(有幾個資料就出現幾個)

 

    此文章雖然是自己的學習小結,而且還是身為初學者的我寫的,但也希望更多的朋友能看到我的文章,如果有不足之處或疑問,歡迎到留言區留言。

2018-07-29學習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.