0
点赞
收藏
分享

微信扫一扫

mysql基础学习--day4

624c95384278 2022-03-26 阅读 58

create table test_001(
     id int auto_increment primary key,
      sname char(20),
      sex enum('男','女','未知'),   //下拉框选择一个
       xingbie set('男','女','未知') //可以多选
);
select * from test_001;

查看表结构:
语法一 : desc 表名

 desc test_001;

语法二:  show create table 表名

 show create table test_001

练习:创建数据表stu_010为学生信息表,包含字段:id为整型,自增,sno为整型,学号,sname为字符串,长度为24,学生姓名,score字段为浮点型,整数部分长度为3,小数点后面保留2位小数,考试分数,createtime为 时间格式,为创建时间,sno设置为主键,name不能为空,默认值为“张三”

create table stu_010(
  id int auto_increment unique,
  sno int primary key,
  sname char(24) not null default '张三',
  score float(5,2),
  createtime datetime
);
select * from stu_010;

再创建表是必须要有的三个字段:
    1;id,自增,尽可能不要参与具体的业务逻辑
    2;创建时间:该字段值为本条数据插入到数据库中的时间点,该值不允许再被修改
        3:修改时间:该字段值为本条数据最后一次被修改的时间点


修改数据库:
   修改数据库只能修改数据库的默认字符集,不能修改库名
语法:
  alter database 库名 default character set 字符集
例:alter database database_work default character set utf8

修改数据库表: alter table 表名 操作关键字 具体操作

   修改表--新增字段

语法:
 alter table 表名 add 字段名称 数据类型 [约束] [放置位置]
     关于放置位置:
        1;不指定具体位置时,默认放在最后
        2;使用after xx字段,将新增字段放置在指定xx字段后面
        3;使用first将新增字段放置在最前面


示例1:修改表stu_010,新增字段sex,类型为int,不能为空,默认值为0
alter table stu_010 add sex int not null default 0;

select * from stu_010;

示例2:修改表stu_010,在score后面新增字段sex_1,类型为int,不能为空,默认值为0
alter table stu_010 add sex_1 int not null default 0 after score


示例3:修改表stu_010,在score前面新增字段sex_2,类型为int,不能为空,默认值为0
alter table stu_010 add sex_2 int not null default 0 after sname

示例4:修改表stu_010,在id前面新增字段sex_3,类型为int,不能为空,默认值为0
alter table stu_010 add sex_3 int not null default 0 first


    修改表--修改字段

语法1:alter table 表名 modify 字段名 数据类型[约束]
语法2:alter table 表名 change 原字段名 新字段名 数据类型[约束]
  注意:
   1;不管修改啥,数据类型一定得存在
   2;如果只是修改字段名的名称的话,字段后面的数据类型,字段级别的数据约束都得加上
   3;修改字段时,字段级别的约束如果没有跟上的话,代表将其删除
   4;字段级别的约束:非空、默认值、自增、注释,表级别约束:主键、唯一键、外键


示例1:修改表stu_010,将sex_3的数据类型修改为字符串,长度为8
alter table stu_010 modify sex_3 char(8) not null default 0

示例2:修改表stu_010,将sex_3的名称改为xingbie
alter table stu_010 change sex_3 xingbie char(8) not null default 0

练习1:修改表stu_010,修改字段sex字段数据类型为int,不能为空,默认值为0,注释为“性别(0-男生,1-女生)”
alter table stu_010 modify sex int not null default 0 comment '性别(0-男生,1-女生)'
select * from stu_010
练习2:修改表stu_010,修改字段createtime名称为ctime
alter table stu_010 change createtime ctime datetime


  修改表--删除字段

语法:alter table 表名 drop 字段名

示例1:修改表test_001,将xingbie段删除
alter table test_001 drop xingbie
select * from test_001

练习:修改表stu_010,将sex_1字段删除
alter table stu_010 drop sex_1


   修改表--新增表级别的约束

alter table 表名 add 具体约束

新增主键:
语法:alter table 表名 add primary key(字段1名称,字段2名称,.....,字段n名称) 

示例:修改表yste,将cno设置为主键
alter table yste add primary key(cno);
show create table yste;

删除主键:
语法: alter table 表名 drop primary key;

示例:修改表yste,将主键删除
alter table yste drop primary key;
show create table yste;

练习1:修改表stu_010,将id和sno设置成联合主键
alter table stu_010 drop primary key;
alter table stu_010 add primary key(id,sno)
show create table stu_010


练习2:修改表stu_010,将该表的主键约束删除
alter table stu_010 drop primary key


新增唯一键:
语法; alter table 表名 add unique(字段1名称,字段2名称,....,字段名称)

示例:修改表stu_010,将sno添加唯一约束
alter table stu_010 add unique(sno);
show create table stu_010

 删除唯一键:
语法; alter table 表名 drop index 索引名称

练习1:修改stu_010表,将id和sno添加联合唯一约束
alter table stu_010 add unique(id,sno)
show create table stu_010

练习2:修改stu_010表,将id和cno的联合唯一约束删除
alter table stu_010 drop index id_2;
show create table stu_010


  修改表名
语法1:alter table 表名 rename to 新表名
语法2:rename table 原表名 to 新表名

练习1:修改表名stu_010为t30_test;
alter table stu_010 rename to t30_test
select * from t30_test

练习2:使用另一种方式将t30_test表名改回为stu_010;
rename table t30_test to stu_010;
select * from stu_010


删除数据库:
语法; drop database 数据库名

删除数据库名:
语法:drop table 表名

if exists 表示如果存在则进行具体操作,否则忽略
if not exists 表示如果不存在则进行具体操作,否则忽略

create table if not exists test(
        id int,
        cname char(20)
);

drop table test;
 

举报

相关推荐

0 条评论