mysql中alter資料表中增加、刪除欄位與表名修改例子.

來源:互聯網
上載者:User

修改刪除mysql資料庫中的資料內容:

[root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' #進入mysql
mysql> create database gbk default character set gbk collate gbk_chinese_ci; #建立一個名字叫做gbk的資料庫
mysql> use gbk
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gbk |
+--------------------+
mysql> show tables;
Empty set (0.00 sec)
mysql> create table test( #建立一個叫做test的資料表
-> 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'); #插入部分內容
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資料庫
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)

接上上面,修改資料庫中表名,表中增加、刪除欄位。

mysql> use gbk    #進入gbk資料庫
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)

聯繫我們

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