mysql 修改表/欄位 增加/刪除表索引

來源:互聯網
上載者:User

mysql教程 修改表/欄位 增加/刪除表索引

create table test (blob_col blob, index(blob_col(10)));在mysql 5.1中,對於myisam和innodb表,首碼可以達到1000位元組長。請注意首碼的限制應以位元組為單位進行測量,而create table語句中的前置長度解釋為字元數。當為使用多位元組字元集的列指定前置長度時一定要加以考慮。

還可以建立fulltext索引。該索引可以用於全文檢索搜尋。只有myisam儲存引擎支援fulltext索引,並且只為char、varchar和text列。索引總是對整個列進行,不支援局部(首碼)索引

 

加索引
mysql> alter table 表名 add index 索引名 (欄位名1[,欄位名2 …]);

例子: mysql> alter table employee add index emp_name (name);
加主關鍵字的索引
mysql> alter table 表名 add primary key (欄位名);
例子: mysql> alter table employee add primary key(id);
加唯一限制條件的索引
mysql> alter table 表名 add unique 索引名 (欄位名);
例子: mysql> alter table employee add unique emp_name2(cardnumber);
mysql alter文法運用:查看某個表的索引
mysql> show index from 表名;

例子: mysql> show index from employee;

刪除某個索引
mysql> alter table 表名 drop index 索引名;

例子: mysql>alter table employee drop index emp_name;

修改表:增加欄位:mysql> alter table table_name add field_name field_type;
查看錶:mysql> select * from table_name;

修改原欄位名稱及類型:mysql> alter table table_name change old_field_name new_field_name field_type;

刪除欄位:mysql alter table table_name drop field_name;


最後補充一點

多列索引
mysql可以為多個列建立索引。一個索引可以包括15個列。對於某些列類型,可以索引列的首碼(參見7.4.3節,“列索引”)。

多列索引可以視為包含通過串連索引列的值而建立的值的排序的數組。

mysql按這樣的方式使用多列索引:當你在where子句中為索引的第1個列指定已知的數量時,查詢很快,即使你沒有指定其它列的值。

假定表具有下面的結構:

create table test (    id int not null,    last_name char(30) not null,    first_name char(30) not null,    primary key (id),    index name (last_name,first_name));name索引是一個對last_name和first_name的索引。索引可以用於為last_name,或者為last_name和first_name在已知範圍內指定值的查詢。因此,name索引用於下面的查詢:

select * from test where last_name='widenius'; select * from test    where last_name='widenius' and first_name='michael'; select * from test    where last_name='widenius'    and (first_name='michael' or first_name='monty'); select * from test    where last_name='widenius'    and first_name >='m' and first_name < 'n';然而,name索引不用於下面的查詢:

select * from test where first_name='michael'; select * from test    where last_name='widenius' or first_name='michael';

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.