900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > mysql中alter数据表中增加 删除字段与表名修改例子

mysql中alter数据表中增加 删除字段与表名修改例子

时间:2022-10-17 09:57:55

相关推荐

mysql中alter数据表中增加 删除字段与表名修改例子

数据库|mysql教程

mysql,alter,数据,表中,增加,删除,字段,表名,

数据库-mysql教程

access asp 源码下载,ubuntu查看网卡代号,python大作业爬虫,php中的tr和tdth是什么,南通seo教学lzw

alter是非常强大的一个功能我们可以利用alter来修改数据表表名字体名及一些其它的操作了,下面一起来看看mysql中alter数据表中增加、删除字段与表名修改的一个例子. 修改删除mysql数据库中的数据内容: [root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p’

php后台系统源码,vscode选择设置,ubuntu更快,tomcat默认启动,图片怎么存进sqlite,WordPress显示插件,后端需要学习前端框架吗教程,企查查免登陆爬虫,php 数组 搜索,黄冈seo排名费用,西安工会医院网站源码,html5旅游简单网页源代码,京东客网站模板lzw

9158 源码,ubuntu终端怎么登录,32位tomcat闪退,河北寄养爬虫,php怎么编写脚本,seo 翻译seo博客lzw

alter是非常强大的一个功能我们可以利用alter来修改数据表表名字体名及一些其它的操作了,,下面一起来看看mysql中alter数据表中增加、删除字段与表名修改的一个例子.

修改删除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)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。