mysql中alter数据表中增加、删除字段与表名修改例子
2019/10/10/17:32:29 阅读:1939 来源:谷歌SEO算法 标签:
夫唯SEO视频教程
alter是非常强大的一个功能我们可以利用alter来修改数据表表名字体名及一些其它的操作了,下面一起来看看mysql中alter数据表中增加、删除字段与表名修改的一个例子.
修改删除mysql数据库中的数据内容:
- [root@hk~]#/usr/local/mysql/bin/mysql-uroot-p'admin'#进入mysql
- mysql>createdatabasegbkdefaultcharactersetgbkcollategbk_chinese_ci;#建立一个名字叫做gbk的数据库
- mysql>usegbk
- mysql>showdatabases;
- +--------------------+
- |Database|
- +--------------------+
- |information_schema|
- |gbk|
- +--------------------+
- mysql>showtables;
- Emptyset(0.00sec)
- mysql>createtabletest(#建立一个叫做test的数据表
- ->idint(4)notnullprimarykeyauto_increment,
- ->namechar(20)notnull
- ->);
- QueryOK,0rowsaffected(0.13sec)
- mysql>showtables;
- +---------------+
- |Tables_in_gbk|
- +---------------+
- |test|
- +---------------+
- 1rowinset(0.00sec)
- mysql>insertintotest(id,name)values(1,'zy');#插入部分内容
- mysql>insertintotest(id,name)values(2,'binghe');
- mysql>insertintotest(id,name)values(3,'zilong');
- mysql>insertintotest(id,name)values(4,'feng');
- mysql>select*fromtest;#检索整个test表
- +----+--------+
- |id|name|
- +----+--------+
- |1|zy|
- |2|binghe|
- |3|zilong|
- |4|feng|
- +----+--------+
- 4rowsinset(0.00sec)
- [root@hk~]#/usr/local/mysql/bin/mysqldump-uroot-p'admin'-Bgbk>/tmp/gbk.sql#备份gbk数据库
- mysql>updatetestsetname='zy';#未定义
- mysql>select*fromtest;#
- +----+------+
- |id|name|
- +----+------+
- |1|zy|
- |2|zy|
- |3|zy|
- |4|zy|
- +----+------+
- [root@hk~]#/usr/local/mysql/bin/mysql-uroot-p'admin'mysql>usegbk--phpfensi.com
- mysql>select*fromtest;
- +----+--------+
- |id|name|
- +----+--------+
- |1|zy|
- |2|binghe|
- |3|zilong|
- |4|feng|
- +----+--------+
- mysql>updatetestsetname='yadianna'whereid=1;
- mysql>select*fromtest;
- +----+----------+
- |id|name|
- +----+----------+
- |1|yadianna|
- |2|binghe|
- |3|zilong|
- |4|feng|
- +----+----------+
- mysql>updatetestsetid=999wherename='yadianna';
- mysql>select*fromtest;
- +-----+----------+
- |id|name|
- +-----+----------+
- |2|binghe|
- |3|zilong|
- |4|feng|
- |999|yadianna|
- +-----+----------+
- mysql>deletefromtestwhereid=999;
- mysql>select*fromtest;
- +----+--------+
- |id|name|
- +----+--------+
- |2|binghe|
- |3|zilong|
- |4|feng|
- +----+--------+
- mysql>deletefromtestwhereid<4;#以条件删除
- mysql>truncatetabletest;#删除all
- mysql>select*fromtest;
- Emptyset(0.00sec)
接上上面,修改数据库中表名,表中增加、删除字段.
- mysql>usegbk#进入gbk数据库
- mysql>desctest;
- +-------+----------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +-------+----------+------+-----+---------+----------------+
- |id|int(4)|NO|PRI|NULL|auto_increment|
- |name|char(20)|NO||NULL||
- +-------+----------+------+-----+---------+----------------+
- mysql>altertabletestaddgenderchar(4);#增加gender
- mysql>desctest;
- +--------+----------+------+-----+---------+----------------+
- |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>altertabletestaddageint(4)aftername;
- mysql>desctest;
- +--------+----------+------+-----+---------+----------------+
- |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>showtables;
- +---------------+
- |Tables_in_gbk|
- +---------------+
- |test|
- +---------------+
- mysql>renametabletesttohello;
- mysql>showtables;
- +---------------+
- |Tables_in_gbk|
- +---------------+
- |hello|
- +---------------+
- mysql>altertablehellorenametoworld;
- mysql>showtables;
- +---------------+
- |Tables_in_gbk|
- +---------------+
- |world|
- +---------------+
- mysql>altertableworlddropage;
- mysql>descworld;
- +--------+----------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +--------+----------+------+-----+---------+----------------+
- |id|int(4)|NO|PRI|NULL|auto_increment|
- |name|char(20)|NO||NULL||
- |gender|char(4)|YES||NULL||
- +--------+----------+------+-----+---------+----------------+
- 3rowsinset(0.00sec)
热门评论