MySQL 第二天作业 操作表和用户权限

一、1.创建数据库 Market,在 Market 中创建数据表customers,customers表结构如表4.6所示,按要求进行操作。

在这里插入图片描述
(1)创建数据库Market。

create database Market;

在这里插入图片描述
(2)创建数据表customers,在c_num字段上添加主键约束和自增约束,在c_birth字段上添加非空约束。

         CREATE TABLE customers (
    ->   `c_num` int(11) NOT NULL AUTO_INCREMENT,
    ->   `c_name` varchar(50) NULL,
    ->   `c_city` varchar(50) NULL,
    ->   `c_birth` datetime NOT NULL,
    ->   PRIMARY KEY (`c_num`)
    -> );

在这里插入图片描述
(3)将c_contact字段插入到c_birth字段后面。

alter table customers ADD c_contact varchar(50);

在这里插入图片描述

(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。

alter table customers rename customers_info;

在这里插入图片描述

(8)删除字段c_city。

alter table customers drop c_city;

在这里插入图片描述

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

alter table customers_info ENGINE = MyISAM;

在这里插入图片描述

二、在 Market中创建数据表orders,orders表结构如表4.7所示,按要求进行操作。

在这里插入图片描述
该表中c_id与cusomers表中c_num字段的类型不同,无法创建关联,因此我将c_id类型改为了int类型。
(1)创建数据表orders,在o_num字段上添加主键约束和自增约束,在c_id字段上添加键约束,关联customers表中的主键c_num.

CREATE TABLE orders  (
    ->   `o_num` int(1) NOT NULL AUTO_INCREMENT,
    ->   `o_data` date NULL,
    ->   `c_id` int(50) NOT NULL,
    ->   PRIMARY KEY (`o_num`),
    ->   CONSTRAINT `co` FOREIGN KEY (`c_id`) REFERENCES `customers` (`c_num`) 
    -> );

在这里插入图片描述
(2)删除orders表的外键约束,然后删除表customers。

alter table orders drop foreign key co;
drop table customers;

在这里插入图片描述

三、创建数据库Team,定义数据表player,语句如下:

在这里插入图片描述
(1)创建一个新账户,用户名为accountl,该用户通过本地主机连接数据库,密码为oldpwd1。授权该用户对Team数据库中 player表的SELECT 和INSERT权限,并且授权该用户对player表的 info字段的UPDATE权限。

create user accountl@'localhost' identified by 'oldpwdl';
grant select,insert,update(info) on Team.player to accountl@'localhost';

(2)创建SQL语句,更改account1用户的密码为newpwd2。

set password for accountl@localhost = password('newpwd2');

(3)创建SQL语句,使用FLUSH PRIVILEGES重新加载权限表。

FLUSH PRIVILEGES;

(4)创建SQL语句,查看授权给account1用户的权限。

SHOW GRANTS FOR 'accountl'@'localhost'

(5)创建SQL语句,收回account1用户的权限。

revoke all on Team.player from accountl@'localhost';

(6)创建SQL语句,将account1用户的账号信息从系统中删除。

DROP USER 'accountl'@'localhost';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值