项目3创建与维护MySQL数据表

创建表

1.创建表 “create table 表明(

         字段名1 字段数据类型1 comment "备注信息1",

         字段名n 字段数据类型n comment "备注信息n"

);

执行结果

2.删除数据表

drop table 表名;

 执行结果

3.查看数据库中所有表

show tables;

 执行结果

 4.查看数据表字段信息

describe 表名;

desc 表名;

 执行结果

 5.查看数据表创建信息

show create table 表名\G;

 执行结果

 6.修改表名

alter table 表名 rename 新表名;

 执行结果

 7.修改字段名和数据类型

alter table 表名 change 原字段名 新字段名 新数据类型;

 执行结果

 8.添加字段

alter table 表名 add 字段名 数据类型 ;

执行结果

 

 9.在表中第一个位置添加字段

alter table 表名 add 字段名 数据类型  first;

 10.在指定字段之后添加字段

alter table 表名 add 字段名 数据类型 after 已存在字段名;

11.删除字段 

alter table 表名 drop 字段名;

执行结果

 12.修改字段排列位置

 alter table 表名 modify 待修改字段名 数据类型 after 字段名;

 执行结果

 13.退出MySQL

\q

 执行结果

单字段主键

create table 表名(
    字段名1 数据类型1 comment “备注信息1”primary key,
    字段名2 数据类型2 comment  "备注信息2"
);

执行结果

 

 多字段主键

create table 表名(
    字段名1 数据类型1  comment  "备注信息1",
    字段名2 数据类型2  comment "备注信息2",
    字段名3 数据类型3 comment“备注信息3”,
    primary key(字段名1,字段名2)
);

 执行结果

 

 外键

constraint 字段别名一般为fk_本表字段名 foreign key(本表字段名)references 来源表名(来源表中字段名)

create table course(
        class_ name varchar (29) comment "班级名",
        teacher_no varchar (10) comment "教师号",
        course_name varchar (30) comment “课程名",
        constraint fk_class_name foreign key (class_name) references class (name),
        constraint fk_teacher_no foreign key (teacher_no) references teacher (no)
);

执行结果

 

 非空 not nu11
唯一 unique
默认 default "默认值"
自增 auto_increment

create table book (
        no int primary key auto_increment,
        name varchar (20) not null unique,
        is_color enum("1", "O") default "0" comment "是否彩色 1是否"
);

执行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值