MySql增删改题

#创建数据库a1

#创建表项aa
#表项:id name age info

#添加表项值
#1 a 10 空
#2 b 11 空
#3 c 12 空
#4 d 13 空

#将表名改为bb

#添加表项time

#修改表项time为times

#删除表项times

#将表项id设为主键约束
#将表项age设为唯一约束
#将表项info设为默认值约束(‘空’)
#将表项name设为非空约束
#将表项id设为自增约束

#删id为3的行

#将数据库安全模式关闭

#将id为3的行中,id值改为2

#将数据库安全模式打开

#删除age的唯一约束
#删除info的默认值约束
#删除name的非空约束
#删除id的自增约束
#删除id的主键约束

#删除表bb

#删除数据库a1

#########################################################
答案:

#创建数据库a1
create database a1;

use a1;

#创建表项aa
#表项:id name age info
create table aa(
id int,name varchar(50),age int,info varchar(50));

#添加表项值
#1 a 10 空
#2 b 11 空
#3 c 12 空
#4 d 13 空
insert into aa
values(1,‘a’,10,null), (2,‘b’,11,null),(3,‘c’,12,null);

#将表名改为bb
rename table aa to bb;

#添加表项time
alter table bb
add time varchar(50);

#修改表项time为times
alter table bb
change time times varchar(50);

#删除表项times
alter table bb
drop times;

#将表项id设为主键约束
alter table bb
add constraint pk_bb_id
primary key(id);

#将表项age设为唯一约束
alter table bb
add constraint uq_bb_age
unique(age);

#将表项info设为默认值约束(‘空’)
alter table bb
alter info set default ‘null’;

#将表项name设为非空约束
alter table bb
modify name varchar(50) not null;

#将表项id设为自增约束
alter table bb
modify id int auto_increment;

#删id为3的行
delete from bb
where id=3;

#将数据库安全模式关闭
set sql_safe_updates=off;

#将id为3的行中,id值改为2
update bb
set id =3
where id=2;

#将数据库安全模式打开
set sql_safe_updates=on;

#删除age的唯一约束
alter table bb
drop index uq_bb_age;

#删除info的默认值约束
alter table bb
alter info drop default;

#删除name的非空约束
alter table bb
modify name varchar(50);

#删除id的自增约束
alter table bb
change id id int;

#删除id的主键约束
alter table bb
drop primary key;

#删除表bb
drop table bb;

#删除数据库a1
drop database a1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值