mysql 的数据库的创建、表的创建以及字段的增删

mysql

  • mysql 是一种关系型数据库,使用的话,需要提前建立数据库(database),数据表(table)不需要提前创建,通过框架的对象关系映射,就可以解决。
  • 数据库 (database)
  • 显示数据库的版本:
    • select version();
  • 数据库的创建
    • create database 数据库名 charset=utf8;
    • create database goods charser=utf8;
  • 查看数据库创建的语句
  • show create database 数据库名;
  • 查看当前使用的数据库
  • select database();
  • 使用数据库
  • use 数据库名
  • use goods;
  • 删除数据库
  • drop database 数据库名;
  • 数据表:
  • 查看当前数据库中的所有的表
  • show tables;
  • 创建表
  • auto_increment 表示自动增长,not null 表示不能为空,primary key 表示主键 default 默认键
  • create table students(id int ,name varchar(30));
  • 查看表创建的sql 语句;
  • show create table students;
create table students(
        id int unsigned not null auto_increment primary key,
        name varchar(30),
        age tinyint unsigned default 0,
        high decimal(5,2),
        gender enum("男", "女", "中性", "保密") default "保密",
        cls_id int unsigned
    );
  • 增加字段和删除字段
alter table students add bith date;
alter table students drop high;
alter table 表名 add 字段 约束;
alter table 表名 drop 字段;
  • 修改字段的约束条件
alter table students modify birthday datatime;
  • 修改字段的名称和约束条件
alter talbe students change birthday birth date default "200-01-01";

删除数据表

drop table 表名
drop database 数据库名,删除数据库同时也把表删除了
  • mysql修改表名
rename table table1 to table2;
rename table class to classes;

猜你喜欢

转载自blog.csdn.net/weixin_44224529/article/details/90288595
今日推荐