创建数据库Market、Team,按要求完成指定操作

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

 代码如下:

#(1)创建数据库Market
mysql> create database Market;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Market             |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use Market
Database changed
mysql> show tables;
Empty set (0.00 sec)

#(2)创建数据表customers,在c_num字段上添加主键约束和自增约束,在c_birth字段上添加非空约束
mysql> create table customers(
    -> c_num int(11) primary key auto_increment,
    -> c_name varchar(50),
    -> c_contact varchar(50),
    -> c_city varchar(50),
    -> c_birth datetime not null)
    -> ;
Query OK, 0 rows affected (0.04 sec)

mysql> desc customers
    -> ;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| c_num     | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name    | varchar(50) | YES  |     | NULL    |                |
| c_contact | varchar(50) | YES  |     | NULL    |                |
| c_city    | varchar(50) | YES  |     | NULL    |                |
| c_birth   | datetime    | NO   |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

#(3)将c_contack字段插入到c_birth字段后面
mysql> alter table customers modify c_contact varchar(50) after c_birth;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc customers
    -> ;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| c_num     | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name    | varchar(50) | YES  |     | NULL    |                |
| c_city    | varchar(50) | YES  |     | NULL    |                |
| c_birth   | datetime    | NO   |     | NULL    |                |
| c_contact | varchar(50) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

#(4)将c_name字段数据类型改为VARCHAR(70)
mysql> alter table customers modify c_name varchar(70);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc customers;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| c_num     | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name    | varchar(70) | YES  |     | NULL    |                |
| c_city    | varchar(50) | YES  |     | NULL    |                |
| c_birth   | datetime    | NO   |     | NULL    |                |
| c_contact | varchar(50) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

#(5)将c_contact字段改名为c_phone
mysql> alter table customers change c_contact c_phone varchar(50);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc customers;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| c_num   | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name  | varchar(70) | YES  |     | NULL    |                |
| c_city  | varchar(50) | YES  |     | NULL    |                |
| c_birth | datetime    | NO   |     | NULL    |                |
| c_phone | varchar(50) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

#(6)增加字段c_gender,数据类型为CHAR(1)
mysql> alter table customers add c_gender char(1);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc customers;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_city   | varchar(50) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

#(7)将表名修改为customers_info
mysql> alter table customers rename customers_info;
Query OK, 0 rows affected (0.02 sec)

mysql> desc customers;
ERROR 1146 (42S02): Table 'Market.customers' doesn't exist
mysql> desc customers_info;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_city   | varchar(50) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

#(8)删除字段c_city
mysql> alter table customers_info drop c_city;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc customers_info;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


#(9)修改数据表的存储引擎为MyISAM
mysql> alter table customers_info engine=MyISAM;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

代码如下:

在关联customers_info 表中的主键c_num时,orders表中的c_id和customers_info表中的c_num

的类型必须相同才能关联,并且customers_info表的存储引擎需要改回InnoDB才可以。

#修改customers_info表中的存储引擎为InnoDB
mysql> alter table customers_info engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create table orders(
    -> o_num int(11) primary key auto_increment,
    -> o_date date,
    -> c_id int(11),
    -> foreign key(c_id) references customers_info(c_num) #添加外键约束
    -> );
Query OK, 0 rows affected (0.02 sec)

#创建orders表成功
mysql> desc orders;
+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| o_num  | int(11) | NO   | PRI | NULL    | auto_increment |
| o_date | date    | YES  |     | NULL    |                |
| c_id   | int(11) | YES  | MUL | NULL    |                |
+--------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

#我们在创建外键约束时并未给外键命名,我们可以通过该命令查看系统默认为外键的命名
mysql> show create table orders;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                                                                                                                   |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| orders | CREATE TABLE `orders` (
  `o_num` int(11) NOT NULL AUTO_INCREMENT,
  `o_date` date DEFAULT NULL,
  `c_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`o_num`),
  KEY `c_id` (`c_id`),
  CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`c_id`) REFERENCES `customers_info` (`c_num`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

#删除外键约束
mysql> alter table orders drop foreign key orders_ibfk_1;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

#删除表custormers_info
mysql> drop table if exists customers_info;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+------------------+
| Tables_in_Market |
+------------------+
| orders           |
+------------------+
1 row in set (0.00 sec)

#删除表customers_info

  • 创建数据库Team,定义数据player,语句如下

题目要求密码oldpwd1不满足默认策略要求,所以我们需要更改密码策略(临时的)

mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.00 sec)

mysql> set global validate_password_policy='LOW';
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_number_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=0;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | OFF   |
| validate_password_dictionary_file    |       |
| validate_password_length             | 0     |
| validate_password_mixed_case_count   | 0     |
| validate_password_number_count       | 0     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 0     |
+--------------------------------------+-------+
7 rows in set (0.01 sec)

(1)创建一个新账户,用户名为account1,该用户通过本地主机连接数据库,密码为oldpwd。授权该用户对Team数据库中的player表中的select和insert权限,并且授权该用户对player表中的info字段的update权限。

#创建用户
mysql> create user account1@localhost identified by 'oldpwd1';
Query OK, 0 rows affected (0.01 sec)
#授权查、写
mysql> grant select,insert on Team.player to account1@'localhost';
Query OK, 0 rows affected (0.00 sec)
#授权更新info字段
mysql> grant update(info) on Team.player to account1@localhost;
Query OK, 0 rows affected (0.00 sec)
#查看用户授权
mysql> show grants for account1@localhost;
+----------------------------------------------------------------------------------+
| Grants for account1@localhost                                                    |
+----------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'account1'@'localhost'                                     |
| GRANT SELECT, INSERT, UPDATE (info) ON `Team`.`player` TO 'account1'@'localhost' |
+----------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

新启动一个本地连接的会话进行验证,并验证成功。

#成功登录该用户
[root@localhost ~]# mysql -uaccount1 -poldpwd1 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>     

#存在Team数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Team               |
+--------------------+
2 rows in set (0.00 sec)

mysql> use Team
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_Team |
+----------------+
| player         |
+----------------+
1 row in set (0.00 sec)

mysql> desc player;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| playid   | int(11)     | NO   | PRI | NULL    |       |
| playname | varchar(30) | NO   |     | NULL    |       |
| teamnum  | int(11)     | NO   | UNI | NULL    |       |
| info     | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

#当前playere表中没有任何数据
mysql> select * from player;
Empty set (0.00 sec)

#插入数据
mysql> insert into player values(1,'a',100,'GOOD'),(2,'b',120,'BAD');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

#查询数据
mysql> select * from player;
+--------+----------+---------+------+
| playid | playname | teamnum | info |
+--------+----------+---------+------+
|      1 | a        |     100 | GOOD |
|      2 | b        |     120 | BAD  |
+--------+----------+---------+------+
2 rows in set (0.00 sec)

#由于没有给account1用户授予对player数据表删除的权限,所以失败。
mysql> delete from player where playid=1;
ERROR 1142 (42000): DELETE command denied to user 'account1'@'localhost' for table 'player'
mysql> 

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

mysql> set password for 'account1'@'localhost' = password('newpwd2');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

我们再次去之前启动的那个本地连接会话中进行验证,并验证成功

[root@localhost ~]# mysql -uaccount1 -poldpwd1   #旧密码登录报错
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'account1'@'localhost' (using password: YES)

[root@localhost ~]# mysql -uaccount1 -pnewpwd2   #新密码成功登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

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

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

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

mysql> show grants for account1@localhost;
+----------------------------------------------------------------------------------+
| Grants for account1@localhost                                                    |
+----------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'account1'@'localhost'                                     |
| GRANT SELECT, INSERT, UPDATE (info) ON `Team`.`player` TO 'account1'@'localhost' |
+----------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> 

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

mysql> revoke all on Team.player from account1@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> 

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

mysql> drop user account1@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> 

我们再次去之前启动的那个本地连接会话中进行验证,并验证成功

[root@localhost ~]# mysql -uaccount1 -pnewpwd2   #已删除无法登录
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'account1'@'localhost' (using password: YES)
[root@localhost ~]# 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/36172.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Java阶段五Day02

Java阶段五Day02 文章目录 Java阶段五Day02MAVEN-聚合(多模块3)回顾多模块2个特性依赖:继承: 聚合场景聚合目的实现聚合聚合总结 远程仓库远程仓库概念配置settings.xml远程库配置注意事项 GIT详细学习git概括git历史本地版本控制相关命令git分支管理分支管理基本概念分支管理相…

Failed to initialize NVML: Driver/library version mismatch

nvidia驱动安装之后,nvidia-smi 报错 Driver/library version mismatch 不重启系统的解决方法 查看系统日志,确定具体报错信息: # dmesg | tail [8598493.408944] NVRM: API mismatch: the client has the version 525.125.06, butNVRM: t…

STL好难(4):list的使用

和列表很像 1.list的介绍 点击这里查看 list 的官方文档 list类似数据结构中的链表 1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独…

flutter聊天界面-自定义表情键盘实现

flutter聊天界面-自定义表情键盘实现 flutter 是 Google推出并开源的移动应用开发框架,主打跨平台、高保真、高性能。开发者可以通过 Dart语言开发 App,一套代码同时运行在 iOS 和 Android平台。 flutter开发基础腾讯IM的聊天应用,使用的是t…

css设计表格圆角最简单的方法

代码如下: table {width: 100%;/* border-collapse: collapse; */background-color: #FBFBFB; /* 背景颜色; */border-collapse: separate; /* 让border-radius有效 */border-spacing: 0; /*表格中每个格边距设为0*/border: 1px solid #DFDFDF;/*边框*/border-radi…

ETHERNET/IP 转ETHERCAT连接倍福和欧姆龙PLC的配置方法

ETHERNET/IP和ETHERCAT是两种不同的协议,它们在工业生产中都有广泛的应用。然而,由于协议不同,这两种设备之间无法通讯,这给工业生产带来了很大的麻烦。而远创智控YC-EIP-ECT网关应运而生,它能够连接到ETHERNET/IP总线…

【Linux之拿捏信号3】阻塞信号

文章目录 相关概念原理sigset_t信号集信号集操作函数sigprocmask系统调用sigpending 相关概念 实际执行信号的处理动作——信号递达Delivery(例如自定义捕捉动作,core,Term终止进程的动作)。信号从产生到递达之间的状态——信号未…

Anaconda配置可视化绘图库seaborn的方法

本文介绍在Anaconda的环境中,安装Python语言中,常用的一个绘图库seaborn模块的方法。 seaborn模块是基于Matplotlib的数据可视化库,它提供了一种更简单、更漂亮的界面来创建各种统计图形。seaborn模块主要用于数据探索、数据分析和数据可视化…

换零钱——最小钱币张数(贪心算法)

贪心算法:根据给定钱币面值列表,输出给定钱币金额的最小张数。 (本笔记适合学完python基本数据结构,初通 Python 的 coder 翻阅) 【学习的细节是欢悦的历程】 Python 官网:https://www.python.org/ Free:大咖免费“圣…

C/C++编程安全标准GJB-8114解读——声明定义类

软件检测实验室在建立软件测试体系或申请cnas/cma相关资质时,需要依据相关标准,使用有效的方法开展检验检测活动,GJB-8114是一部嵌入式软件安全测试相关的国家标准,本系列文章我们就针对GJB-8114《C/C语言编程安全子集》的具体内容…

Android 热修复一

一、什么是热修复? 在我们应用上线后出现bug需要及时修复时,不用再发新的安装包,只需要发布补丁包,在客户无感知下修复掉bug。 实现效果: Demo源码: https://gitee.com/sziitjim/hotfix 二、怎么进行热修…

一文了解Docker之网络模型

目录 1.Docker网络 1.1 Docker网络模型概述 1.2 Docker网络驱动程序 1.2.1 host模式 1.2.2 bridge模式 1.2.3 container模式 1.2.4 none模式 1.3 Docker网络命令示例 1.3.1 创建一个自定义网络 1.3.2 列出所有网络 1.3.3 连接容器到网络 1.3.4 断开容器与网络的连接…

如何与ChatGPT愉快地聊天

原文链接:https://mp.weixin.qq.com/s/ui-O4CnT_W51_zqW4krtcQ 人工智能的发展已经走到了一个新的阶段,在这个阶段,人工智能可以像人一样与我们进行深度的文本交互。其中,OpenAI的ChatGPT是一个具有代表性的模型。然而&#xff0…

【ARM Coresight 系列文章 3.1 - ARM Coresight DP 对 AP 的访问 1】

文章目录 1.1 DP 中相关寄存器的介绍1.1.1 DPACC and APACC 寄存器1.1.2 DP SELECT 寄存器1.1.3 AP CSW寄存器1.1.4 AP TAR 寄存器1.1.5 AP DRW寄存器1.1.6 AP Banked Data registers 1.1 DP 中相关寄存器的介绍 如果DAP接入的是JTAG接口,那么将会通过APACC寄存器来…

[VUE学习]权限管理系统前端vue实现8-右上角用户头像显示实现

1.现在有个问题 我们再没有token情况下通过url可以直接访问页面 这不可以 所以我们需要添加路由守卫 拦截 2.permission.js的代码 import router from "/router/index" import store from "/store"router.beforeEach((to,from,next)>{const whiteList…

React类组件

1. React组件 将页面按照界面功能进行拆分,每一块界面都拥有自己的独立逻辑,这样可以提高项目代码的可维护性。其中React组件分为两种,一种是类式组件,一种是函数式组件。这里我们将的是比较常用的类式组件,但是在后续…

括号生成(力扣)递归 JAVA

目录 题目描述:纯递归解法:递归 回溯: 题目描述: 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 示例 1: 输入:n 3 输出&#xff1a…

子集 (力扣)数学推理 JAVA

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 示例 1: 输入:nums [1,2,3] 输出:[[],[1],[2],[1,2],[3],[…

Unity/Shader 零碎知识点

坐标系 Unity使用的是左手坐标系&#xff1b;观察空间&#xff0c;通俗来讲就是以摄像机为原点的坐标系&#xff0c;摄像机的前向是z轴的负方向&#xff0c;与模型和世界空间中的定义相反&#xff0c;z轴的坐标减少意味着场景深度的增加 点积 abba|a||b|cos<a,b> 结果为常…

邮票面值-2022年全国青少年信息素养大赛Python国赛第5题

[导读]&#xff1a;超平老师计划推出《全国青少年信息素养大赛Python编程真题解析》50讲&#xff0c;这是超平老师解读Python编程挑战赛真题系列的第7讲。 全国青少年信息素养大赛&#xff08;原全国青少年电子信息智能创新大赛&#xff09;是“世界机器人大会青少年机器人设计…