mysql5.7建库建表,MySQL5.7 建库建表的命令

习题源自 《MySQL 5.7从入门到精通》清华大学出版社 --第四章数据表的基本操作

1 创建数据库Market,在market中创建数据库表customers, customers表结构如表1

(1)创建数据库market

DELIMITER //

create database market;

(2)创建数据库表customers

--指定操作在market数据中进行

usemarket;--创建customers表

create tablecustomers(

c_numint(11) primary key not null uniqueauto_increment,

c_namevarchar(50),

c_contactvarchar(50),

c_cityvarchar(50),

c_birthdatetime not null

(3)将c_contact字段插入c_birth之后

alter table customers modify c_contact varchar(50) after c_birth;

(4)将c_name字段类型改为varchar(70)

alter table customers modify c_name varchar(70);

(5)将c_contact字段改名为c_phone

alter table customers change c_contact c_phone varchar(50);

(6)增加c_gender字段,数据类型为char(1)

alter table customers add c_gender char(1);

(7)将表名修改为customers_info

--修改表名为c_city

alter table customers rename to customers_info;

(8)删除字段c_city

alter table customers_info drop c_city;

(9)修改数据表的存储引擎为MyISAM

alter table customers_info engine = MyISAM;

2 在market数据库中创建数据表orders,orders表结构如表2

(1)创建数据表orders,其中在c_id字段上添加外键约束,关联customers表中的主键c_num

create tableorders(

o_numint(11) PRIMARY KEY not null uniqueauto_increment,

o_date date,

c_idint(varchar50)

);--因为外键列的数据类型与父表关联的列的数据类型要匹配,且表的存储引擎要一样,不然会报错--在MySQL5.5以上,表的存储引擎默认为是InnoDB,因为在上一题中关联的表改了存储引擎,所以这里也要改

alter table orders engine =MyISAM;alter table orders add constraint fk_order_custom FOREIGN KEY(c_id) references customers_info (c_num);

(2)删除 orders 表的外键约束,然后删除表customers

--因为此处删除的表与其他表有索引关联,因此在删表之前要删除外键约束

alter table orders drop Foreign keyfk_order_custom;drop table customers_info;

数据库的基本操作

数据表的基本操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值