企業如何給MYSQL建立表,查詢表,建立索引執行個體

來源:互聯網
上載者:User

標籤:企業如何給mysql建立表   索引   

建立表

資料類型   int(整形,整數) not null,char(字元) tinyint(最小的整形) varchar (變長的字元類型)

create table xiaohu(

id int(4) not null,

name char(20) not null,

age tinyint(2) not null default ‘0‘,(不可以為空白,但可以給0)

dept varchar(16) default null (可以為空白)

mysql> create table student(

    -> id  int(4) not null,

    -> name char(20) not null,

    -> age tinyint(20) not null default ‘0‘,

    -> dept varchar(16) default null

    -> );

Query OK, 0 rows affected (0.01 sec)




查看見過表的語句:

show  create table 表名\G;


mysql> show create table student\G;

*************************** 1. row ***************************

       Table: student

Create Table: CREATE TABLE `student` (

  `id` int(4) NOT NULL,

  `name` char(20) NOT NULL,

  `age` tinyint(20) NOT NULL DEFAULT ‘0‘,

  `dept` varchar(16) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=utf8

1 row in set (0.00 sec)



查看錶結構

desc  加表名

mysql> desc student;

+-------+-------------+------+-----+---------+-------+

| Field | Type        | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id    | int(4)      | NO   |     | NULL    |       |

| name  | char(20)    | NO   |     | NULL    |       |

| age   | tinyint(20) | NO   |     | 0       |       |

| dept  | varchar(16) | YES  |     | NULL    |       |

+-------+-------------+------+-----+---------+-------+

4 rows in set (0.01 sec)



建立索引  提升查詢速度

索引分類:

主鍵索引   主鍵列所有內容必須唯一  比如學號,准考證號

mysql> create table student(id int(4) not null AUTO_INCREMENT,遞增 name char(20) not null, age tinyint(2) not null default ‘0‘, dept varchar(16) default null, primary key(id),主鍵索引  key index_name (name)普通索引 );

Query OK, 0 rows affected (0.01 sec)




如果在建立表的時候忘記加索引怎麼辦?

可以添加  文法

mysql> alter table student change id id  int primary key auto_increment;

Query OK, 0 rows affected (0.00 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc student

    -> ;

+-------+-------------+------+-----+---------+----------------+

| Field | Type        | Null | Key | Default | Extra          |

+-------+-------------+------+-----+---------+----------------+

| id    | int(11)     | NO   | PRI | NULL    | auto_increment |

| name  | char(20)    | NO   |     | NULL    |                |

| age   | tinyint(2)  | NO   |     | 0       |                |

| dept  | varchar(16) | YES  |     | NULL    |                |

+-------+-------------+------+-----+---------+----------------+

4 rows in set (0.00 sec)


普通索引:

mysql> alter table student add index index_name(name);  改表 表的名字 添加 索引  索引名字(在哪個列上添加)

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;

+-------+-------------+------+-----+---------+----------------+

| Field | Type        | Null | Key | Default | Extra          |

+-------+-------------+------+-----+---------+----------------+

| id    | int(11)     | NO   | PRI | NULL    | auto_increment |

| name  | char(20)    | NO   | MUL | NULL    |                |

| age   | tinyint(2)  | NO   |     | 0       |                |

| dept  | varchar(16) | YES  |     | NULL    |                |

+-------+-------------+------+-----+---------+----------------+

4 rows in set (0.00 sec)



指定前N個字元建立索引

mysql> create index index_dept  on  student(dept(8));  建立 索引 索引名  在 表(第幾個列(前幾個字元))

Query OK, 0 rows affected (0.00 sec)

Records: 0  Duplicates: 0  Warnings: 0


查看索引 詳細

mysql> show index from student\G;

*************************** 1. row ***************************

       Table: student

  Non_unique: 0

    Key_name: PRIMARY

Seq_in_index: 1

 Column_name: id

   Collation: A

 Cardinality: 0

    Sub_part: NULL

      Packed: NULL

        Null: 

  Index_type: BTREE

     Comment: 

*************************** 2. row ***************************

       Table: student

  Non_unique: 1

    Key_name: index_name

Seq_in_index: 1

 Column_name: name

   Collation: A

 Cardinality: NULL

    Sub_part: NULL

      Packed: NULL

        Null: 

  Index_type: BTREE

     Comment: 

*************************** 3. row ***************************

       Table: student

  Non_unique: 1

    Key_name: index_x

Seq_in_index: 1

 Column_name: age

   Collation: A

 Cardinality: NULL

    Sub_part: NULL

      Packed: NULL

        Null: 

  Index_type: BTREE

     Comment: 

*************************** 4. row ***************************

       Table: student

  Non_unique: 1

    Key_name: index_dept

Seq_in_index: 1

 Column_name: dept

   Collation: A

 Cardinality: NULL

    Sub_part: 8

      Packed: NULL

        Null: YES

  Index_type: BTREE

     Comment: 

4 rows in set (0.00 sec)



聯合索引文法

create index index_name_dept on student(name,dept); 


       Table: student

  Non_unique: 1

    Key_name: ind_name_dept

Seq_in_index: 2

 Column_name: dept

   Collation: A

 Cardinality: NULL

    Sub_part: NULL

      Packed: NULL

        Null: YES

  Index_type: BTREE

     Comment: 

6 rows in set (0.00 sec)



mysql> drop index ind_name_dept on student; 刪除索引 索引名 在 哪個表

Query OK, 0 rows affected (0.00 sec)

Records: 0  Duplicates: 0  Warnings: 0



建立聯合索引前幾個字元建立索引

create index ind_name_dept on student(name(8),dept(10));



到底給那些資料建立索引呢?

索引占空間,更新資料庫還需要維護索引資料 寫頻繁讀取好的語句少建立索引 十到幾百行的小表不用建立索引

盡量在唯一值多的大表上建立索引



營運就是開個慢查詢的語句開個監控


企業如何給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.