mysql表修改

來源:互聯網
上載者:User

標籤:

CREATE TABLE tab2 AS (SELECT * FROM tab1)
這種做法表的儲存引擎也會採用伺服器預設的儲存引擎而不是源表的儲存引擎,此種複製方法把表的內容也一起複製過來了。

CREATE TALBE tab2 ENGINE=MYISAM, CHARSET=‘UTF8‘ AS (SELECT * FROM tab1)
可以自己指定儲存引擎和字元集,彌補方法一的不足

CREATE TABLE tab2 LIKE tab1
使用和tab1表相同的結構來建立一個新表,列名、資料類型、空指和索引也將複製,但是表的內容不會被複製。外鍵和專用的許可權也沒有被複製。

MySQL複製表結構及資料到新表
CREATE TABLE tab_new SELECT * FROM tab_old

複製舊錶的資料到新表(假設兩個表結構一樣)
INSERT INTO tab1 SELECT * FROM tab2

複製舊錶的資料到新表(假設兩個表結構不一樣)
INSERT INTO tab1(欄位1, 欄位2, ...) SELECT 欄位1, 欄位2, ... FROM tab2

更改表名
ALTER TABLE employee RENAME TO staff

更改列類型
ALTER TABLE employee MODIFY COLUMN truename VARCHAR(10) NOT NULL DEFAULT ‘‘

更改列名
ALTER TABLE employee CHANGE COLUMN truename employeename VARCHAR(10) NOT NULL DEFAULT ‘‘

添加預設值
ALTER TABLE employee ALTER COLUMN truename SET DEFAULT ‘‘

刪除預設值
ALTER TABLE employee ALTER COLUMN truename DEOP DEFAULT


mysql修改表
表的結構如下:

mysql> show create table person;
| person | CREATE TABLE `person` (
  `number` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `birthday` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
刪除列:

ALTER TABLE person DROP COLUMN birthday;
添加列:

ALTER TABLE person ADD COLUMN birthday datetime;
修改列,把number修改為bigint:

ALTER TABLE person MODIFY number BIGINT NOT NULL;
或者是把number修改為id,類型為bigint:

ALTER TABLE person CHANGE number id BIGINT;
 

添加主鍵:

ALTER TABLE person ADD PRIMARY KEY (id);
刪除主鍵:

ALTER TABLE person DROP PRIMARY KEY;
添加唯一索引:

ALTER TABLE person ADD UNIQUE name_unique_index (`name`);
為name這一列建立了唯一索引,索引的名字是name_unique_index.

 

添加普通索引:

ALTER TABLE person ADD INDEX birthday_index (`birthday`);
 

刪除索引:

ALTER TABLE person DROP INDEX birthday_index;
ALTER TABLE person DROP INDEX name_unique_index;
 

禁用非唯一索引

ALTER TABLE person DISABLE KEYS;
ALTER TABLE...DISABLE KEYS讓MySQL停止更新MyISAM表中的非唯一索引。

啟用非唯一索引

ALTER TABLE person ENABLE KEYS;
ALTER TABLE ... ENABLE KEYS重新建立丟失的索引。

 

把表預設的字元集和所有字元列(CHAR, VARCHAR, TEXT)改為新的字元集:

ALTER TABLE person CONVERT TO CHARACTER SET utf8;
修改表某一列的編碼

ALTER TABLE person CHANGE name name varchar(255) CHARACTER SET utf8;
僅僅改變一個表的預設字元集

ALTER TABLE person DEFAULT CHARACTER SET utf8;
 修改表名

RENAME TABLE person TO person_other;
移動表到其他資料庫

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
 

 

 
在mysql中我們對資料表欄位的修改命令只要使用alter就可以了,下面我來給大家詳細介紹mysql中修改表欄位名/欄位長度/欄位類型等等一些方法介紹,有需要瞭解的朋友可參考。

先來看看常用的方法
MySql的簡單文法,常用,卻不容易記住。當然,這些Sql文法在各資料庫中基本通用。下面列出:
1.增加一個欄位
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一個欄位,預設為空白
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;    //增加一個欄位,預設不可為空
2.刪除一個欄位
alter table user DROP COLUMN new2;                //刪除一個欄位
3.修改一個欄位
alter table user MODIFY new1 VARCHAR(10);            //修改一個欄位的類型
alter table user CHANGE new1 new4 int;              //修改一個欄位的名稱,此時一定要重新

//主鍵
alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
//增加一個新列
alter table t2 add d timestamp;
alter table infos add ex tinyint not null default ‘0′;
//刪除列
alter table t2 drop column c;
//重新命名列
alter table t1 change a b integer;
//改變列的類型
alter table t1 change b b bigint not null;
alter table infos change list list tinyint not null default ‘0′;
//重新命名表
   alter table t1 rename t2;
加索引
mysql> alter table tablename change depno depno int(5) not null;
mysql> alter table tablename add index 索引名 (欄位名1[,欄位名2 …]);
mysql> alter table tablename add index emp_name (name);
加主關鍵字的索引
mysql> alter table tablename add primary key(id);
加唯一限制條件的索引
mysql> alter table tablename add unique emp_name2(cardnumber);
刪除某個索引
mysql>alter table tablename drop index emp_name;
增加欄位:
mysql> ALTER TABLE table_name ADD field_name field_type;
修改原欄位名稱及類型:
mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
刪除欄位:
mysql> ALTER TABLE table_name DROP field_name;
mysql修改欄位長度
alter table 表名 modify column 欄位名 類型;
例如
資料庫中user表 name欄位是varchar(30)
可以用
alter table user modify column name varchar(50) ;


mysql更改表結構:添加、刪除、修改欄位、調整欄位順序
mysqltablenulluserlist
添加欄位:

alter table `user_movement_log`
Add column GatewayId int not null default 0 AFTER `Regionid` (在哪個欄位後面添加)

刪除欄位:

alter table `user_movement_log` drop column Gatewayid

調整欄位順序:

ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID

//主鍵

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);

//增加一個新列

alter table t2 add d timestamp;
alter table infos add ex tinyint not null default ‘0‘;

//刪除列

alter table t2 drop column c;

//重新命名列

alter table t1 change a b integer;

//改變列的類型

alter table t1 change b b bigint not null;
alter table infos change list list tinyint not null default ‘0‘;

//重新命名表

alter table t1 rename t2;

加索引

mysql> alter table tablename change depno depno int(5) not null;
mysql> alter table tablename add index 索引名 (欄位名1[,欄位名2 …]);
mysql> alter table tablename add index emp_name (name);

加主關鍵字的索引

mysql> alter table tablename add primary key(id);

加唯一限制條件的索引

mysql> alter table tablename add unique emp_name2(cardnumber);

刪除某個索引

mysql>alter table tablename drop index emp_name;

修改表:

增加欄位:

mysql> ALTER TABLE table_name ADD field_name field_type;

修改原欄位名稱及類型:

mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

刪除欄位:

mysql> ALTER TABLE table_name DROP field_name;


MySql表結構修改詳解

修改表的文法
=========================
增加列[add 列名]
=========================
①alter table 表名 add 列名 列類型 列參數【加的列在表的最後面】
    例:alter table test add username char(20) not null default ‘‘;
        alter table test add birth date not null default ‘0000-00-00‘;

②alter table 表名 add 列名 列類型 列參數 after 某列【把新列加在某列後面】
    例:alter table test add gender char(1) not null default ‘‘ after username;

③alter table 表名 add 列名 列類型 列參數 first【把新列加在最前面】
    例:alter table test add pid int not null default 0 first;

=========================
刪除列[drop 列名]
=========================
①alter table 表名 drop 列名
    例:alter table test drop pid;

=========================
修改列[modife 列名]
=========================
①alter table 表名 modify 列名 新類型 新參數【修改列類型】
    例:alter table test modify gender char(4) not null default ‘‘;
②alter table 表名 change 舊列名 新列名 新類型 新參數【修改列名和列類型】
    例:alter table test change pid uid int unsigned not null default 0;

=========================
查詢列
=========================
①desc 表名【查詢所有列】
    例: desc test;
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId   | int(11)     | NO   | PRI |         |       |
| dName | varchar(32) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

②show columns from 表名【效果和desc一樣】
mysql> show columns from department;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId   | int(11)     | NO   | PRI |         |       |
| dName | varchar(32) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

③show create table 表名【查看錶的建立代碼】
mysql> show create table department;
CREATE TABLE `department` (
  `dId` int(11) NOT NULL,
  `dName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`dId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

mysql> ALTER TABLE table_name DROP field_name;_name new_field_name field_type;)性、預設值) primary key (new_field_id);


 

ORDER BY _column1, _column2; /* _column1升序,_column2升序 */
ORDER BY _column1, _column2 DESC; /* _column1升序,_column2降序 */
ORDER BY _column1 DESC, _column2 ; /* _column1降序,_column2升序 */
ORDER BY _column1 DESC, _column2 DESC; /* _column1降序,_column2降序 */
用 DESC 表示按倒序排序(即:從大到小排序)
用 ACS   表示按正序排序(即:從小到大排序)



order應該是以漢字的 ASCII 碼排序,下面是按照漢字拼音排序
select * from corp_data where Chengshi like "圖木舒克" order by convert(name using gbk);


#select count(*) as count from corp_data where Chengshi like "圖木舒克";
select * from corp_data where Chengshi like "圖木舒克" order by convert(name using gbk);
#delete from corp_data where Chengshi like "圖木舒克";
#desc corp_data;
#alter table corp_data modify column hangye varchar(100) ;
#alter table corp_data modify column jianjie varchar(10000) ;

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.