mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改例子
來源:易賢網(wǎng) 閱讀:1183 次 日期:2015-03-10 15:55:32
溫馨提示:易賢網(wǎng)小編為您整理了“mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改例子”,方便廣大網(wǎng)友查閱!

alter是非常強大的一個功能我們可以利用alter來修改數(shù)據(jù)表表名字體名及一些其它的操作了,下面一起來看看mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改的一個例子.

修改刪除mysql數(shù)據(jù)庫中的數(shù)據(jù)內(nèi)容:

[root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' #進入mysql

mysql> create database gbk default character set gbk collate gbk_chinese_ci; #建立一個名字叫做gbk的數(shù)據(jù)庫

mysql> use gbk

mysql> show databases;

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

| Database |

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

| information_schema |

| gbk |

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

mysql> show tables;

Empty set (0.00 sec)

mysql> create table test( #建立一個叫做test的數(shù)據(jù)表

-> id int(4) not null primary key auto_increment,

-> name char(20) not null

-> );

Query OK, 0 rows affected (0.13 sec)

mysql> show tables;

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

| Tables_in_gbk |

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

| test |

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

1 row in set (0.00 sec)

mysql> insert into test(id,name) values(1,'zy'); #插入部分內(nèi)容

mysql> insert into test(id,name) values(2,'binghe');

mysql> insert into test(id,name) values(3,'zilong');

mysql> insert into test(id,name) values(4,'feng');

mysql> select * from test; #檢索整個test表

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

| id | name |

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

| 1 | zy |

| 2 | binghe |

| 3 | zilong |

| 4 | feng |

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

4 rows in set (0.00 sec)

[root@hk ~]# /usr/local/mysql/bin/mysqldump -uroot -p'admin' -B gbk >/tmp/gbk.sql #備份gbk數(shù)據(jù)庫

mysql> update test set name = 'zy' ; #未定義

mysql> select * from test; #

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

| id | name |

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

| 1 | zy |

| 2 | zy |

| 3 | zy |

| 4 | zy |

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

[root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' mysql> use gbk

mysql> select * from test;

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

| id | name |

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

| 1 | zy |

| 2 | binghe |

| 3 | zilong |

| 4 | feng |

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

mysql> update test set name = 'yadianna' where id =1;

mysql> select * from test;

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

| id | name |

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

| 1 | yadianna |

| 2 | binghe |

| 3 | zilong |

| 4 | feng |

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

mysql> update test set id = 999 where name ='yadianna';

mysql> select * from test;

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

| id | name |

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

| 2 | binghe |

| 3 | zilong |

| 4 | feng |

| 999 | yadianna |

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

mysql> delete from test where id =999;

mysql> select * from test;

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

| id | name |

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

| 2 | binghe |

| 3 | zilong |

| 4 | feng |

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

mysql> delete from test where id <4; #以條件刪除

mysql> truncate table test; #刪除all

mysql> select * from test;

Empty set (0.00 sec)

接上上面,修改數(shù)據(jù)庫中表名,表中增加、刪除字段。

mysql> use gbk #進入gbk數(shù)據(jù)庫

mysql> desc test;

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

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

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

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

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

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

mysql> alter table test add gender char(4); #增加gender

mysql> desc test;

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

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

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

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

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

| gender | char(4) | YES | | NULL | |

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

mysql> alter table test add age int(4) after name;

mysql> desc test;

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

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

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

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

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

| age | int(4) | YES | | NULL | |

| gender | char(4) | YES | | NULL | |

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

mysql> show tables;

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

| Tables_in_gbk |

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

| test |

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

mysql> rename table test to hello;

mysql> show tables;

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

| Tables_in_gbk |

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

| hello |

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

mysql> alter table hello rename to world;

mysql> show tables;

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

| Tables_in_gbk |

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

| world |

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

mysql> alter table world drop age;

mysql> desc world;

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

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

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

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

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

| gender | char(4) | YES | | NULL | |

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

3 rows in set (0.00 sec)

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡編程

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)