mysql表屬性、索引、約束

來源:互聯網
上載者:User

標籤:

1、表屬性

  建立表的基本文法: 

    create table 【if not exists】 表名 (欄位列表 【,索引或約束列表】)【表選項列表】

  其中,欄位列表格式如下:

    欄位名 類型 【屬性列表】,

    欄位名 類型 【屬性列表】...

  屬性列表中各個屬性之間用空格隔開。

  常用的欄位屬性:

auto_increment 設定欄位值自動成長,用於整數類型
primary key 設定欄位為主鍵,此時該欄位的值可以“唯一確定”一行資料
unique key 設定欄位為唯一的,在整個資料表中不會重複
not null 設定欄位不能為null,如果不指定該屬性,那麼欄位的值預設是可以為null的
default      設定欄位的預設值,插入資料的時候,若不指定該欄位你的值,會使用預設值填充。
comment 設定欄位的說明

  說明:primary key跟unique key都確定了欄位的唯一性,由這兩個屬性修飾的欄位都能唯一的確定一行資料,區別在於primary key不能為null(指定了primary key的時候,預設的指定了not null屬性),而unique key則可以為null。可以說,primary key是特殊的unique key。

代碼:

/*建立表,注意屬性*/
mysql> create table item_properties_table( -> id int auto_increment primary key, -> name varchar(20) not null unique key, -> pwd varchar(48) not null, -> age tinyint default 18, -> email varchar(50) comment ‘電子郵件‘ -> );Query OK, 0 rows affected (0.02 sec)
/*查看錶結構,主要是為了查看comment*/mysql> show full columns from item_properties_table;+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+--------------+| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+--------------+| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | || name | varchar(20) | utf8_general_ci | NO | UNI | NULL | | select,insert,update,references | || pwd | varchar(48) | utf8_general_ci | NO | | NULL | | select,insert,update,references | || age | tinyint(4) | NULL | YES | | 18 | | select,insert,update,references | || email | varchar(50) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 電子郵件 |+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+--------------+5 rows in set (0.00 sec)mysql>

 

2、索引

 

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.