创建修改mysql表练习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/majichen95/article/details/86546125

例:创建一个存储引擎为InnoDB,字符集为GBK的表test,字段为id int和name varchar(16),并完成下列要求:

1、批量插入数据:1,newlhr; 2,小麦苗; 3,xiaomaimiao

2、把数据ID等于1的名字newlhr更改为oldlhr。

3、在字段id后插入age字段,类型为tinyint(4)。

4、删除age列。

5、对name列添加唯一约束。

6、删除(2,小麦苗)这条记录。

7、对id列添加索引,索引名为id_index。

8、将表重命名为user。


解:

0、

create table test(id int not null, name varchar(16) not null)engine=InnoDB default charset=GBK;

1、

insert into test values(1,'newlhr'),(2,'小树苗'),(3,'xiaoshumiao');

2、

update test set name='oldlhr' where name='newlhr';

3、

alter table test add age tinyint(4) after id;

4、

alter table test drop age;

5、

alter table test add unique key (name);

6、

delete from test where id=2;

7、

alter table test add index id_index(id);

8、

alter table test rename to user;

注:插入记录时,要实现若不存在则插入,存在则忽略:

insert ignore into test values(...);

要实现不存在则插入,存在则更新:

replace into test set id=1,name='sam';

猜你喜欢

转载自blog.csdn.net/majichen95/article/details/86546125