作业:
创建一个数据,db_classics
创建一张表:t_hero
id name nickname age address job weapon group
1 张百川 玉皇大帝 200000
2 孙悟空 齐天大圣
3 林妹妹
创建数据库,创建英雄表
添加对应的约束条件
插入80条数据
通过alter添加两个新的字段
通过alter删除一个字段
重命名一张表
create database if not exists db_classics default charset=utf8mb4;
# 如果数据库存在,也不报错,特别适合于写脚本时,编码使用utf8mb4
create table if not exists t_hero(
id int primary key auto_increment,
name varchar(50) not null unique,
nickname varchar(50) not null unique,
age int check(age >= 18) default 20,
address varchar(50) not null unique,
gender varchar(10) default "男",
job varchar(10),
weapon varchar(50),
grou int not null default 1,
);
insert into t_hero values(1,"张百川","玉皇大帝",200000,"天庭","男","刺客","不知道",1)
insert into t_hero values(2,"孙悟空","齐天大圣",600,"花果山","男","战士","金箍棒",2)
insert into t_hero values(3,"林黛玉","林妹妹",18,"贾府","女","法师","面巾",3)
修改表结构:
增加新的列
ALTER TABLE t_hero ADD "爱好" varchar not null;
ALTER TABLE t_hero ADD "评价" default '强';
删除列
ALTER TABLE t_hero DROP "爱好"
表重命名
ALTER TABLE t_hero RENAME t_hero1;
总
create database if not exists db_classics default charset=utf8mb4;
# 如果数据库存在,也不报错,特别适合于写脚本时,编码使用utf8mb4
create table if not exists t_hero(
id int primary key auto_increment,
name varchar(50) not null unique,
nickname varchar(50) not null unique,
age int check(age >= 18),
address varchar(50) not null unique,
gender varchar(10) not null default "男",
job varchar(10),
weapon varchar(50),
grou int not null default 1
);insert into t_hero values(1,"张百川","玉皇大帝",200000,"天庭","男","刺客","不知道",1);
insert into t_hero values(2,"孙悟空","齐天大圣",600,"花果山","男","战士","金箍棒",2)
insert into t_hero values(3,"林黛玉","林妹妹",18,"贾府","女","法师","面巾",3)
#修改表结构:
#增加新的列
ALTER TABLE t_hero ADD 爱好 varchar(100) not null;
ALTER TABLE t_hero ADD 评价 varchar(100) default "强";
#删除列
ALTER TABLE t_hero DROP 爱好;
#表重命名
ALTER TABLE t_hero RENAME t_hero1;