sql語句整理

2021-08-27 17:47:15 字數 1617 閱讀 9525

mysql 命令大全:

1、修改主鍵id的遞增預設值:

alter table tablename auto_increment=100000;

2、將表重新命名:

alter table oldtablename rename to newtablename;

3、為表新增新的字段:

alter table tablename add column `列名` varchar(255) default null;

4、修改表中某個欄位名:

alter table tablename change oldname newname varchar(255) default null;

5、為表中某個字段建立索引:

create index 索引名 on tablename(列名);

alter table 表名 drop constraint 約束名字

說明:刪除表的字段的原有約束

alter table 表名 add constraint 約束名字 default 預設值 for 欄位名稱

說明:新增乙個表的字段的約束並指定預設值

6、檢視表中索引:

show index from tablename;

show keys from tablename;

7、索引檢視中各個字段解釋:

· table

表的名稱。

· non_unique

如果索引不能包括重複詞,則為0。如果可以,則為1。

· key_name

索引的名稱。

· seq_in_index

索引中的列序列號,從1開始。

· column_name

列名稱。

· collation

列以什麼方式儲存在索引中。在mysql中,有值『a』(公升序)或null(無分類)。

· cardinality

索引中唯一值的數目的估計值。通過執行analyze table或myisamchk -a可以更新。基數根據被儲存為整數的統計資料來計數,所以即使對於小型表,該值也沒有必要是精確的。基數越大,當進行聯合時,mysql使用該索引的機 會就越大。

· sub_part

如果列只是被部分地編入索引,則為被編入索引的字元的數目。如果整列被編入索引,則為null。

· packed

指示關鍵字如何被壓縮。如果沒有被壓縮,則為null。

· null

如果列含有null,則含有yes。如果沒有,則該列含有no。

· index_type

用過的索引方法(btree, fulltext, hash, rtree)。

· comment

8、顯示表的建表語句:

show create table tablename;

9、顯示表結構語句:

show columns from tablename;

10、修改欄位的型別:

alter table tablename modify column content text(0);

11、修改欄位的長度:

alter table tablename modify column content varchar(5000);

sql語句整理

建立表 create table teacher id int 11 not null auto increment,teaname varchar 10 not null,varchar 10 not null,course varchar 10 not null,primary key id e...

常用sql語句整理

a 判斷資料庫是否存在 if exists select from sys.databases where name 庫名 刪除資料庫 drop database 庫名b 判斷要建立的表名是否存在 if exists select from dbo.sysobjects where id objec...

常用SQL語句整理

檢視資料庫 show databases 選擇資料庫 use database name 表定義資訊 describe table name 插入資料 insert into table name filed 1 field n values value 1 value n eg insert in...