python_fullstack数据库(三)-MySQL表操作

MySQL数据库

三、表操作

1、创建(复制)

①语法:
CREATE TABLE 表名(
    字段名1 类型[(宽度) 约束条件],
    字段名2 类型[(宽度) 约束条件],
    字段名3 类型[(宽度) 约束条件]
)ENGINE=innodb DEFAULT CHARSET utf8;
②创建实例
create table student(
    id int not null auto_increment primary key,
    name varchar(50) not null,
    age int not null,
    sex enum('男','女') not null default '男',
    salary double(10,2) not null
)engine=innodb default charset=utf8;

not null :表示此列不能为空
auto_increment :表示自增长,默认每次增长+1
注意:自增长只能添加在主键或者唯一索引字段上

primary key :表示主键(唯一且不为空)
engine =innodb :表示指定当前表的存储引擎
default charset utf8 :设置表的默认编码集
③主键:一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。
  • 单一列:
create table tb1(
    nid int not null auto_increment primary key,
    num int null
)
  • 多列组合主键:
create table tb1(
    nid int not null,
    num int not null,
    primary key(nid,num)
)
④自增:自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
create table tb1(
    nid int not null auto_increment primary key,
    num int null
)
或
create table tb1(
    nid int not null auto_increment,
    num int null,
    index(nid)
)
注意:
     1、对于自增列,必须是索引(含主键)
     2、对于自增可以设置步长和起始值
    show session variables like 'auto_inc%';
    set session auto_increment_increment=2;
    set session auto_increment_offset=10;

    show global variables like 'auto_inc%';
    set global auto_increment_increment=2;
    set global auto_increment_offset=10;
⑤复制表
  • 只复制表结构和表中数据
    create table tb2 select * from tb1;
    ps:主键自增、索引、触发器、外键,不会被复制

  • 只复制表结构
    create table tb2 like tb1;
    ps: 数据、触发器、外键,不会被复制 

2、删除

  • 删除表(drop后的表自增字段不清空,继续计数)
    drop table 表名;
  • 清空表(truncate后的表自增字段清空,重新开始计数)
    truncate table 表名;

3、修改

  • 添加表字段
    alter table 表名 add 字段名 类型 约束;
    例如:
    alter table student add age int not null default 0 after name;
    ps: after name 表示在name字段后添加字段 age.

  • 修改表字段
    方式一:
    alter table student modify 字段 varchar(100) null;
    方式二:
    alter table student change 旧字段 新字段 int not null default 0;
    ps:二者区别:
    change 可以改变字段名字和属性
    modify只能改变字段的属性

  • 删除表字段 :
    alter table student drop 字段名;

  • 更新表名称:
    rename table 旧表名 to 新表名;

  • 添加主键 :
    alter table student add primary key(字段,"多个","间隔");

  • 移除主键 :
    alter table student drop primary key;
    ps:如果主键为自增长,以上方式则不被允许执行,请先去掉主键自增长属性,然后再移除主键
    alter table student modify id int not null,drop primary key

  • 添加外键:
    alter table 从表 add CONSTRAINT fk_test foreign key 从表(字段) REFERENCES 主表(字段);

  • 移除外键:
    alter table 表 drop foreign key 外键名称;
    ps:如果外键设置后想修改,那么只能是先删除,再添加

  • 修改默认值 :
    alter table 表 alter 字段 set default 100;

  • 删除默认值 :
    alter table 表 alter 字段 drop default;

4、查询

  • 查询表数据
    select 字段(多个以","间隔) from 表名;
    例:
    select name,sex from student;
    或者
    select * from student;

  • 查看表结构
    desc 表名;
    例:
    desc student;

  • 查看创建表信息
    show create table student;

5、存储引擎介绍

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值