溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL主健、索引講義

發(fā)布時(shí)間:2020-04-24 14:25:26 來(lái)源:億速云 閱讀:231 作者:三月 欄目:數(shù)據(jù)庫(kù)

下文內(nèi)容主要給大家?guī)?lái)MySQL主健、索引講義,這里所講到的知識(shí),與書(shū)籍略有不同,都是億速云專(zhuān)業(yè)技術(shù)人員在與用戶接觸過(guò)程中,總結(jié)出來(lái)的,具有一定的經(jīng)驗(yàn)分享價(jià)值,希望給廣大讀者帶來(lái)幫助。 

<--目錄-->

1)主健

   1、操作表的約束

     (1)非空約束

     (2)字段默認(rèn)值

     (3)唯一約束

     (4)主健約束

     (5)添加主?。ㄖ攸c(diǎn))

     (6) 自動(dòng)增加  

2)索引

   1、創(chuàng)建普通索引(重點(diǎn))

   2、創(chuàng)建唯一索引

   3、創(chuàng)建全文索引

   4、創(chuàng)建多列索引

   5、刪除索引

MySQL主健、索引講義

【主健】

1、操作表的約束

###########################################################################

約束關(guān)健字                       含義                                     #

not null                   約束的字段值不能為空                           #

default                    設(shè)置字段的默認(rèn)值                               #

unique key (uk)            約束字段的值是唯一                             #

primary key (pk)           約束字段為表的主健,可以作為該記錄的唯一標(biāo)識(shí)   #

auto_increment             約束字段的值為自動(dòng)增加                         #

foreign key (fk)           約束字段為表的外健                             #

###########################################################################


#設(shè)置非空約束

#解釋?zhuān)涸O(shè)置了非空約束,字段內(nèi)容則不允許為空

mysql> create table t1(

    -> id int(20) not null,

    -> loc varchar(40)

    -> );


#設(shè)置字段的默認(rèn)值

#解釋?zhuān)寒?dāng)為表中某個(gè)字段插入記錄沒(méi)有給他賦值,則系統(tǒng)會(huì)為這個(gè)字段插入默認(rèn)值

mysql> create table t1(

    -> id int(20) not null,

    -> dname varchar(20) default 'cjgong',

    -> loc varchar(40)

    -> );


#設(shè)置唯一約束

#解釋?zhuān)簲?shù)據(jù)庫(kù)表中某個(gè)字段的內(nèi)容不允許重復(fù)

mysql> create table t1(

    -> id int(20) not null,

    -> dname varchar(20) unique,

    -> loc varchar(40)

    -> );


#設(shè)置主健約束

#解釋?zhuān)河脭?shù)據(jù)庫(kù)表中的某個(gè)字段來(lái)標(biāo)識(shí)所有記錄

mysql> create table t1(

    -> id int(20) primary key,

    -> loc varchar(40)

    -> );


#添加主健

alter table student change id id int primary key auto_increment;


#設(shè)置字段值自動(dòng)增加

#解釋?zhuān)寒?dāng)為數(shù)據(jù)庫(kù)表中插入新記錄時(shí),字段上的值會(huì)自動(dòng)生成唯一的ID

mysql> create table t1(

    -> id int(20) primary key auto_increment,

    -> loc varchar(40)

    -> );


【索引】

1、創(chuàng)建普通索引

#解釋?zhuān)浩胀ㄋ饕桓郊尤魏蜗拗茥l件,可以創(chuàng)建在任何數(shù)據(jù)類(lèi)型的字段上

mysql> create table t1(

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> ... ...

    -> index  索引名(字段名)

    -> );


#創(chuàng)建表時(shí)創(chuàng)建普通索引

mysql> create table t1(

    -> id int(20) primary key auto_increment,

    -> loc varchar(40)

    -> index index_deptno(deptno)

    -> );


#在已經(jīng)存在的表上創(chuàng)建普通索引

mysql> create index 索引名 on 表名(字段名)

mysql> create index index_deptno on t1(deptno)


#通過(guò)sql語(yǔ)句alter table創(chuàng)建普通索引

mysql> alter table table_name add index 索引名(字段名);

mysql> alter table t1 add index index_deptno(deptno);


2、創(chuàng)建和查看唯一索引

mysql> create table t1(

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> ... ...

    -> unique index  索引名(字段名)

    -> );


#創(chuàng)建表時(shí)創(chuàng)建唯一索引

#解釋?zhuān)簞?chuàng)建索引時(shí),索引的值必段是唯一的

mysql> create table t1(

    -> id int(20) primary key auto_increment,

    -> loc varchar(40)

    ->  unique index index_depktno(deptno)

    -> );


#在已經(jīng)存在的表上創(chuàng)建唯一索引

mysql> create unique index 索引名 on 表名(字段名)

mysql> create unique index index_deptno on t1(deptno)


#通過(guò)sql語(yǔ)句alter table創(chuàng)建唯一索引

mysql> alter table table_name add unique index 索引名(字段名);

mysql> alter table t1 add unique index index_deptno(deptno);


3、創(chuàng)建和查看全文索引

#解釋?zhuān)喝乃饕P(guān)聯(lián)在char、varchar、text字段上,以便快速查詢(xún)數(shù)量較大的字符串類(lèi)型的字段

mysql> create table t1(

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> ... ...

    -> fulltext index  索引名(字段名)

    -> );


#創(chuàng)建表時(shí)創(chuàng)建全文索引

mysql> create table t1(

    -> id int(20) primary key auto_increment,

    -> loc varchar(40)

    ->  fulltext index index_depktno(deptno)

    -> );


#在已經(jīng)存在的表上創(chuàng)建全文索引

mysql> create fulltext index 索引名 on 表名(字段名)

mysql> create fulltext index index_deptno on t1(deptno)


#通過(guò)sql語(yǔ)句alter table創(chuàng)建全文索引

mysql> alter table table_name add fulltext index 索引名(字段名);

mysql> alter table t1 add unique fulltext index index_deptno(deptno);


4、創(chuàng)建和查看多列索引

#解釋?zhuān)憾嗔兴饕趧?chuàng)建索引時(shí),所關(guān)聯(lián)的字段是多個(gè)字段,雖然可以通過(guò)所關(guān)聯(lián)的字段進(jìn)行查詢(xún),但是只有查詢(xún)條件中使用了所關(guān)聯(lián)字段中的第一個(gè)字段,多列索引才會(huì)被使用

mysql> create table t1(

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> ... ...

    -> fulltext index  索引名(字段名)

    -> );


#創(chuàng)建表時(shí)創(chuàng)建多列索引

mysql> create table t1(

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> 字段名 數(shù)據(jù)庫(kù)類(lèi),

    -> ... ...

    -> index  索引名(字段名1,字段名2)

    -> );


#創(chuàng)建表時(shí)創(chuàng)建多列索引

mysql> create table t1(

    -> id int(20) primary key auto_increment,

    -> loc varchar(40)

    -> index index_deptno(deptno,id)

    -> );


#在已經(jīng)存在的表上創(chuàng)建多列索引

mysql> create index 索引名 on 表名(字段名1,字段名2)

mysql> create index index_deptno on t1(deptno,id)


#通過(guò)sql語(yǔ)句alter table創(chuàng)建多列索引

mysql> alter table table_name add index 索引名(字段名1,字段名2);

mysql> alter table t1 add index index_deptno(deptno,id);


5、刪除索引

語(yǔ)法形式如下:

drop index 索引名字 on 表名字

mysql> drop index index_deptno on t1;

對(duì)于以上關(guān)于MySQL主健、索引講義,如果大家還有更多需要了解的可以持續(xù)關(guān)注我們億速云的行業(yè)推新,如需獲取專(zhuān)業(yè)解答,可在官網(wǎng)聯(lián)系售前售后的,希望該文章可給大家?guī)?lái)一定的知識(shí)更新。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI