国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改例子

2024-07-24 12:39:38
字體:
供稿:網(wǎng)友

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

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

  1. [root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' #進入mysql 
  2. mysql> create database gbk default character set gbk collate gbk_chinese_ci; #建立一個名字叫做gbk的數(shù)據(jù)庫 
  3. mysql> use gbk 
  4. mysql> show databases; 
  5. +--------------------+ 
  6. Database | 
  7. +--------------------+ 
  8. | information_schema | 
  9. | gbk | 
  10. +--------------------+ 
  11. mysql> show tables; 
  12. Empty set (0.00 sec) 
  13. mysql> create table test( #建立一個叫做test的數(shù)據(jù)表 
  14. -> id int(4) not null primary key auto_increment, 
  15. -> name char(20) not null 
  16. -> ); 
  17. Query OK, 0 rows affected (0.13 sec) 
  18. mysql> show tables; 
  19. +---------------+ 
  20. | Tables_in_gbk | 
  21. +---------------+ 
  22. | test | 
  23. +---------------+ 
  24. 1 row in set (0.00 sec) 
  25. mysql> insert into test(id,namevalues(1,'zy'); #插入部分內(nèi)容 
  26. mysql> insert into test(id,namevalues(2,'binghe'); 
  27. mysql> insert into test(id,namevalues(3,'zilong'); 
  28. mysql> insert into test(id,namevalues(4,'feng'); 
  29. mysql> select * from test; #檢索整個test表 
  30. +----+--------+ 
  31. | id | name | 
  32. +----+--------+ 
  33. | 1 | zy | 
  34. | 2 | binghe | 
  35. | 3 | zilong | 
  36. | 4 | feng | 
  37. +----+--------+ 
  38. rows in set (0.00 sec) 
  39. [root@hk ~]# /usr/local/mysql/bin/mysqldump -uroot -p'admin' -B gbk >/tmp/gbk.sql #備份gbk數(shù)據(jù)庫 
  40. mysql> update test set name = 'zy' ; #未定義 
  41. mysql> select * from test; # 
  42. +----+------+ 
  43. | id | name | 
  44. +----+------+ 
  45. | 1 | zy | 
  46. | 2 | zy | 
  47. | 3 | zy | 
  48. | 4 | zy | 
  49. +----+------+ 
  50. [root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' mysql> use gbk   --Vevb.com 
  51. mysql> select * from test; 
  52. +----+--------+ 
  53. | id | name | 
  54. +----+--------+ 
  55. | 1 | zy | 
  56. | 2 | binghe | 
  57. | 3 | zilong | 
  58. | 4 | feng | 
  59. +----+--------+ 
  60. mysql> update test set name = 'yadianna' where id =1; 
  61. mysql> select * from test; 
  62. +----+----------+ 
  63. | id | name | 
  64. +----+----------+ 
  65. | 1 | yadianna | 
  66. | 2 | binghe | 
  67. | 3 | zilong | 
  68. | 4 | feng | 
  69. +----+----------+ 
  70. mysql> update test set id = 999 where name ='yadianna'
  71. mysql> select * from test; 
  72. +-----+----------+ 
  73. | id | name | 
  74. +-----+----------+ 
  75. | 2 | binghe | 
  76. | 3 | zilong | 
  77. | 4 | feng | 
  78. | 999 | yadianna | 
  79. +-----+----------+ 
  80. mysql> delete from test where id =999; 
  81. mysql> select * from test; 
  82. +----+--------+ 
  83. | id | name | 
  84. +----+--------+ 
  85. | 2 | binghe | 
  86. | 3 | zilong | 
  87. | 4 | feng | 
  88. +----+--------+ 
  89. mysql> delete from test where id <4; #以條件刪除 
  90. mysql> truncate table test; #刪除all 
  91. mysql> select * from test; 
  92. Empty set (0.00 sec) 

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

  1. mysql> use gbk    #進入gbk數(shù)據(jù)庫 
  2. mysql> desc test; 
  3. +-------+----------+------+-----+---------+----------------+ 
  4. | Field | Type     | Null | Key | Default | Extra          | 
  5. +-------+----------+------+-----+---------+----------------+ 
  6. | id    | int(4)   | NO   | PRI | NULL    | auto_increment | 
  7. name  | char(20) | NO   |     | NULL    |                | 
  8. +-------+----------+------+-----+---------+----------------+ 
  9. mysql> alter table test add gender char(4);    #增加gender 
  10. mysql> desc test; 
  11. +--------+----------+------+-----+---------+----------------+ 
  12. | Field  | Type     | Null | Key | Default | Extra          | 
  13. +--------+----------+------+-----+---------+----------------+ 
  14. | id     | int(4)   | NO   | PRI | NULL    | auto_increment | 
  15. name   | char(20) | NO   |     | NULL    |                | 
  16. | gender | char(4)  | YES  |     | NULL    |                | 
  17. +--------+----------+------+-----+---------+----------------+ 
  18. mysql> alter table test add age int(4) after name
  19. mysql> desc test; 
  20. +--------+----------+------+-----+---------+----------------+ 
  21. | Field  | Type     | Null | Key | Default | Extra          | 
  22. +--------+----------+------+-----+---------+----------------+ 
  23. | id     | int(4)   | NO   | PRI | NULL    | auto_increment | 
  24. name   | char(20) | NO   |     | NULL    |                | 
  25. | age    | int(4)   | YES  |     | NULL    |                | 
  26. | gender | char(4)  | YES  |     | NULL    |                | 
  27. +--------+----------+------+-----+---------+----------------+ 
  28. mysql> show tables; 
  29. +---------------+ 
  30. | Tables_in_gbk | 
  31. +---------------+ 
  32. | test          | 
  33. +---------------+ 
  34. mysql> rename table test to hello; 
  35. mysql> show tables; 
  36. +---------------+ 
  37. | Tables_in_gbk | 
  38. +---------------+ 
  39. | hello         | 
  40. +---------------+ 
  41. mysql> alter table hello rename to world; 
  42. mysql> show tables; 
  43. +---------------+ 
  44. | Tables_in_gbk | 
  45. +---------------+ 
  46. | world         | 
  47. +---------------+ 
  48. mysql> alter table world drop age; 
  49. mysql> desc world; 
  50. +--------+----------+------+-----+---------+----------------+ 
  51. | Field  | Type     | Null | Key | Default | Extra          | 
  52. +--------+----------+------+-----+---------+----------------+ 
  53. | id     | int(4)   | NO   | PRI | NULL    | auto_increment | 
  54. name   | char(20) | NO   |     | NULL    |                | 
  55. | gender | char(4)  | YES  |     | NULL    |                | 
  56. +--------+----------+------+-----+---------+----------------+ 
  57. rows in set (0.00 sec)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日土县| 黔西县| 沁阳市| 荆门市| 望都县| 刚察县| 保德县| 乌兰浩特市| 高台县| 环江| 崇义县| 三亚市| 石城县| 阿城市| 泾阳县| 高州市| 安溪县| 泊头市| 奉贤区| 都江堰市| 江西省| 杭锦旗| 寻乌县| 长春市| 东阿县| 揭西县| 镇原县| 台北市| 金秀| 竹溪县| 太湖县| 三河市| 彭阳县| 醴陵市| 宣武区| 堆龙德庆县| 灌阳县| 景德镇市| 凤山县| 沈丘县| 怀化市|