首页 > 数据库 >MySQL 考试练习题

MySQL 考试练习题

时间:2023-03-14 16:36:02浏览次数:43  
标签:练习题 02 01 15 04 mysql char MySQL 考试

1、用户表account

1、用户表account(用户编号userid,用户名fullname,密码passward,性别sex,住址adderss,邮箱email,电话phone)

  • account(userid char(6),fullname char(4),passward char(7),sex char(2),adderess char(6),email char(20),phone char(12))

  • account(userid,fullname,passward,sex,adderess,email,phone)

('u0001','刘晓和','123456','男','广东深圳市','[email protected]','13512345678'),
('u0002','张嘉庆','123456','男','广东深圳市','[email protected]','13512345679'),
('u0003','罗红红','123456','女','广东深圳市','[email protected]','13512345689'),
('u0004','李昊华','123456','女','广东深圳市','[email protected]','13812345679'),
('u0005','王天赐','123456','男','广东中山市','[email protected]','13802345679'),
('u0006','吴美霞','123456','女','广东珠海市','[email protected]','13512345879');

代码过程

#显示所有数据库
mysql> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| club               |
| exam               |
| mysql              |
| performance_schema |
| qq                 |
| sys                |
+--------------------+
7 rows in set (0.02 sec)
#创建exam数据库
mysql> create database exam; 
Query OK, 1 row affected (0.01 sec)

#定义字符类型为utf8
mysql>  alter database exam charset utf8; 
Query OK, 1 row affected (0.00 sec)

#进入exam数据库
mysql> use exam;
Database changed

#创建数据库的字段
mysql> create table account(userid char(6),fullname char(4),passward char(7),sex char(2),adderess char(6),email char(20),phone char(12));
Query OK, 0 rows affected (0.03 sec)

#进入数据库exam
mysql> use exam;
Database changed

#选择account数据表
mysql> select * from account;
Empty set (0.00 sec)

#插入新的内容
mysql> insert into account(userid,fullname,passward,sex,adderess,email,phone) values('u0001','刘晓和','123456','男','广 东深圳市','[email protected]','13512345678');
Query OK, 1 row affected (0.01 sec)

#批量插入新的内容
mysql> insert into account(userid,fullname,passward,sex,adderess,email,phone) values
    -> ('u0002','张嘉庆','123456','男','广东深圳市','[email protected]','13512345679'),
    -> ('u0003','罗红红','123456','女','广东深圳市','[email protected]','13512345689'),
    -> ('u0004','李昊华','123456','女','广东深圳市','[email protected]','13812345679'),
    -> ('u0005','王天赐','123456','男','广东中山市','[email protected]','13802345679'),
    -> ('u0006','吴美霞','123456','女','广东珠海市','[email protected]','13512345879');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

#显示所有内容
mysql> select * from account;
+--------+-----------+----------+------+-----------------+-----------------+-------------+
| userid | fullname  | passward | sex  | adderess        | email           | phone       |
+--------+-----------+----------+------+-----------------+-----------------+-------------+
| u0001  | 刘晓和    | 123456   | 男   | 广东深圳市      | [email protected]   | 13512345678 |
| u0002  | 张嘉庆    | 123456   | 男   | 广东深圳市      | [email protected] | 13512345679 |
| u0003  | 罗红红    | 123456   | 女   | 广东深圳市      | [email protected]   | 13512345689 |
| u0004  | 李昊华    | 123456   | 女   | 广东深圳市      | [email protected]    | 13812345679 |
| u0005  | 王天赐    | 123456   | 男   | 广东中山市      | [email protected]  | 13802345679 |
| u0006  | 吴美霞    | 123456   | 女   | 广东珠海市      | [email protected]    | 13512345879 |
+--------+-----------+----------+------+-----------------+-----------------+-------------+
6 rows in set (0.00 sec)

2、商品表product

  • (商品编号productid,商品分类号catid,商品名name,商品介绍descn,市场价格listprice,成本价格unitcost,数量qty)

  • (productid,catid,name,descn,listprice,unitcost,qty)

  • product(productid char(15),catid char(3),name char(10),descn char(15),listprice float(7,2),unitcost float(7,2),qty float(7,2))

('av-cb-01','05','亚马逊鹦鹉','75岁以上高龄的好伙伴',50.00,60.00,100),
('av-sb-02','05','燕雀','非常好的减压宠物',45.00,50.00,98),
('fi-fw-01','01','锦鲤','来自日本的淡水鱼',45.50,45.50,300),
('fi-fw-02','01','金鱼','来自中国的淡水鱼',6.80,6.80,100),
('fi-sw-01','01','天使鱼','来自澳大利亚的海水鱼',10.00,10.00,100),
('fi-sw-02','01','虎鲨','来自澳大利亚的海水鱼',18.50,20.00,200),
('fl-dlh-02','04','波斯猫','友好的家钜猫',1000.00,1200.00,15),
('fl-dsh-01','04','马恩岛猫','灭鼠能手',80.00,100.00,40),
('k9-bd-01','02','牛头犬','来自英格兰的友好的狗',1350.00,1500.00,5),
('k9-cw-01','02','吉娃娃犬','很好的陪伴狗',180.00,200.00,120),
('k9-dl-01','02','斑点狗','来自消防队的大狗',3000.00,3000.00,1),
('k9-po-02','02','狮子犬','来自法国可爱的狗',300.00,300.00,200),
('rp-li-02','03','鼠蹊','友好的绿色朋友',60.00,78.00,40),
('rp-sn-01','03','响尾蛇','兼当看门狗',200.00,240.00,10);

代码过程

mysql> create table product(productid char(15),catid char(3),name char(10),descn char(15),listprice float(7,2),unitcost float(7,2),qty float(7,2))
    -> ;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from product;
Empty set (0.00 sec)

mysql> insert into product(productid,catid,name,descn,listprice,unitcost,qty) values
    -> ('av-cb-01','05','亚马逊鹦鹉','75岁以上高龄的好伙伴',50.00,60.00,100),
    -> ('av-sb-02','05','燕雀','非常好的减压宠物',45.00,50.00,98),
    -> ('fi-fw-01','01','锦鲤','来自日本的淡水鱼',45.50,45.50,300),
    -> ('fi-fw-02','01','金鱼','来自中国的淡水鱼',6.80,6.80,100),
    -> ('fi-sw-01','01','天使鱼','来自澳大利亚的海水鱼',10.00,10.00,100),
    -> ('fi-sw-02','01','虎鲨','来自澳大利亚的海水鱼',18.50,20.00,200),
    -> ('fl-dlh-02','04','波斯猫','友好的家钜猫',1000.00,1200.00,15),
    -> ('fl-dsh-01','04','马恩岛猫','灭鼠能手',80.00,100.00,40),
    -> ('k9-bd-01','02','牛头犬','来自英格兰的友好的狗',1350.00,1500.00,5),
    -> ('k9-cw-01','02','吉娃娃犬','很好的陪伴狗',180.00,200.00,120),
    -> ('k9-dl-01','02','斑点狗','来自消防队的大狗',3000.00,3000.00,1),
    -> ('k9-po-02','02','狮子犬','来自法国可爱的狗',300.00,300.00,200),
    -> ('rp-li-02','03','鼠蹊','友好的绿色朋友',60.00,78.00,40),
    -> ('rp-sn-01','03','响尾蛇','兼当看门狗',200.00,240.00,10);
Query OK, 14 rows affected (0.00 sec)
Records: 14  Duplicates: 0  Warnings: 0

mysql> select * fromm product;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fromm product' at line 1
mysql> select * from product;
+-----------+-------+-----------------+--------------------------------+-----------+----------+--------+
| productid | catid | name            | descn                          | listprice | unitcost | qty    |
+-----------+-------+-----------------+--------------------------------+-----------+----------+--------+
| av-cb-01  | 05    | 亚马逊鹦鹉      | 75岁以上高龄的好伙伴           |     50.00 |    60.00 | 100.00 |
| av-sb-02  | 05    | 燕雀            | 非常好的减压宠物               |     45.00 |    50.00 |  98.00 |
| fi-fw-01  | 01    | 锦鲤            | 来自日本的淡水鱼               |     45.50 |    45.50 | 300.00 |
| fi-fw-02  | 01    | 金鱼            | 来自中国的淡水鱼               |      6.80 |     6.80 | 100.00 |
| fi-sw-01  | 01    | 天使鱼          | 来自澳大利亚的海水鱼           |     10.00 |    10.00 | 100.00 |
| fi-sw-02  | 01    | 虎鲨            | 来自澳大利亚的海水鱼           |     18.50 |    20.00 | 200.00 |
| fl-dlh-02 | 04    | 波斯猫          | 友好的家钜猫                   |   1000.00 |  1200.00 |  15.00 |
| fl-dsh-01 | 04    | 马恩岛猫        | 灭鼠能手                       |     80.00 |   100.00 |  40.00 |
| k9-bd-01  | 02    | 牛头犬          | 来自英格兰的友好的狗           |   1350.00 |  1500.00 |   5.00 |
| k9-cw-01  | 02    | 吉娃娃犬        | 很好的陪伴狗                   |    180.00 |   200.00 | 120.00 |
| k9-dl-01  | 02    | 斑点狗          | 来自消防队的大狗               |   3000.00 |  3000.00 |   1.00 |
| k9-po-02  | 02    | 狮子犬          | 来自法国可爱的狗               |    300.00 |   300.00 | 200.00 |
| rp-li-02  | 03    | 鼠蹊            | 友好的绿色朋友                 |     60.00 |    78.00 |  40.00 |
| rp-sn-01  | 03    | 响尾蛇          | 兼当看门狗                     |    200.00 |   240.00 |  10.00 |
+-----------+-------+-----------------+--------------------------------+-----------+----------+--------+
14 rows in set (0.00 sec)

3、购物车表shopcat

  • (购物车编号shopcatid,用户编号userid,商品编号productid,数量quantity,单价unitprice,总价totalprice,订单日期orderdate,订单状态status)

  • (shopcatid,userid,productid,quantity,unitprice,totalprice,orderdate,status)

  • (shopcatid int,userid char(6),productid char(9),quantity int,unitprice float(6,2),totalprice int,orderdate char(11),status int)

(20130411,'u0001', 'fi-sw-01',10,18.50,0,'2013-04-11 15:07:34', 0),
(20130412,'u0002', 'k9-bd-01',2,120.00,0,'2013-04-09 15:08:11', 0),
(20130413,'u0003', 'k9-dl-01',2,130.00,0,'2013-04-15 15:09:34', 0),
(20130414,'u0003', 'rp-sn-01',3,125.00,0,'2013-04-15 15:09:30', 1),
(20130415,'u0004', 'av-sb-02',2,50.00,0,'2013-04-02 15:10:34', 0);

代码过程

mysql> create table shopcat;
ERROR 1113 (42000): A table must have at least 1 column
mysql> create table shopcat(shopcatid int,userid char(6),productid char(9),quantity int,unitprice float(6,2),totalprice int,orderdate char(11),status int);
Query OK, 0 rows affected (0.02 sec)

mysql> select * from shopcat;
Empty set (0.00 sec)

mysql> insert into shopcat(shopcatid,userid,productid,quantity,unitprice,totalprice,orderdate,status) values
    -> (20130411,'u0001', 'fi-sw-01',10,18.50,0,'2013-04-11 15:07:34', 0),
    -> (20130412,'u0002', 'k9-bd-01',2,120.00,0,'2013-04-09 15:08:11', 0),
    -> (20130413,'u0003', 'k9-dl-01',2,130.00,0,'2013-04-15 15:09:34', 0),
    -> (20130414,'u0003', 'rp-sn-01',3,125.00,0,'2013-04-15 15:09:30', 1),
    -> (20130415,'u0004', 'av-sb-02',2,50.00,0,'2013-04-02 15:10:34', 0);
ERROR 1406 (22001): Data too long for column 'orderdate' at row 1
mysql> create table shopcat(shopcatid int,userid char(6),productid char(9),quantity int,unitprice float(6,2),totalprice int,orderdate char(25),status int);
ERROR 1050 (42S01): Table 'shopcat' already exists

# 修改字段长度命令
mysql> alter table shopcat modify column orderdate varchar(25);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into shopcat(shopcatid,userid,productid,quantity,unitprice,totalprice,orderdate,status) values
    -> (20130411,'u0001', 'fi-sw-01',10,18.50,0,'2013-04-11 15:07:34', 0),
    -> (20130412,'u0002', 'k9-bd-01',2,120.00,0,'2013-04-09 15:08:11', 0),
    -> (20130413,'u0003', 'k9-dl-01',2,130.00,0,'2013-04-15 15:09:34', 0),
    -> (20130414,'u0003', 'rp-sn-01',3,125.00,0,'2013-04-15 15:09:30', 1),
    -> (20130415,'u0004', 'av-sb-02',2,50.00,0,'2013-04-02 15:10:34', 0);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from shopcat;
+-----------+--------+-----------+----------+-----------+------------+---------------------+--------+
| shopcatid | userid | productid | quantity | unitprice | totalprice | orderdate           | status |
+-----------+--------+-----------+----------+-----------+------------+---------------------+--------+
|  20130411 | u0001  | fi-sw-01  |       10 |     18.50 |          0 | 2013-04-11 15:07:34 |      0 |
|  20130412 | u0002  | k9-bd-01  |        2 |    120.00 |          0 | 2013-04-09 15:08:11 |      0 |
|  20130413 | u0003  | k9-dl-01  |        2 |    130.00 |          0 | 2013-04-15 15:09:34 |      0 |
|  20130414 | u0003  | rp-sn-01  |        3 |    125.00 |          0 | 2013-04-15 15:09:30 |      1 |
|  20130415 | u0004  | av-sb-02  |        2 |     50.00 |          0 | 2013-04-02 15:10:34 |      0 |
+-----------+--------+-----------+----------+-----------+------------+---------------------+--------+
5 rows in set (0.00 sec)
代码说明:修改字段长度命令
  • 语法alter table 表名 modify column 列名 类型(要修改的长度);
  • 修改mrflysand表中id的char值为10个字长alter table mrflysand modify column id varchar(10);

标签:练习题,02,01,15,04,mysql,char,MySQL,考试
From: https://www.cnblogs.com/MrFlySand/p/17215354.html

相关文章

  • 如何将自定义的日志直接写入到 mysql 数据库中?
    要将自定义的日志直接写入MySQL数据库中,您可以使用以下步骤:安装MySQL数据库,并创建一个新的数据库和表来存储日志数据。在您的代码中,使用MySQL连接器来连接到MySQL数据库。创......
  • mysql备份还原
    本次试验用的是mysqldump进行备份。1、操作系统的Shell下,执行  mysqldump-umysql-p db1>/mysqlbak/db1.sql       说明:-umysql内mysql是数据库的登......
  • mysql ERROR 3948 (42000): Loading local data is disabled; this must be enabled o
    mysql>selectversion();+-----------+|version()|+-----------+|8.0.20|+-----------+1rowinset(0.00sec)使用loaddata导入数据时候报错......
  • MySQL Variables--slave_exec_mode参数
    基础信息MySQL提供参数slave_exec_mode来控制主从复制中遇到的数据冲突和错误,有严格模式(STRICT)和冥等模式(IDEMPOTENT)两种选项,默认为严格模式。在严格模式下,MySQL会严......
  • windows系统mysql定时备份
    如下:一、创建bat任务脚本1.新建txt文档2.打开txt文档,并粘贴入以下内容3.按照自己的需求对内容进行修改,并删除掉//后内容以及中文空格,否则会运行失败4.保存,并将文件后......
  • mysql 有守护进程导致无法kill停止
    现象:停止mysqld服务时,发现kill进程后,过一段时间服务会自动重启。查看发现是守护进程导致可以试下以下办法方式一:使用service停止:servicemysqldstop方式二:......
  • MySQL 的事务隔离级别和锁
    MySQL的事务隔离级别(IsolationLevel)  数据库锁,分为悲观锁和乐观锁,“悲观锁”悲观锁一般利用SELECT…FORUPDATE类似的语句乐观锁利用CAS机制,并不会对数据......
  • 力扣178(MySQL)-分数排名(中等)
    题目:表: Scores编写SQL查询对分数进行排序。排名按以下规则计算:分数应按从高到低排列。如果两个分数相等,那么两个分数的排名应该相同。在排名相同的分数后,排名数应......
  • docker安装mysql8
    拉取镜像dockerpullmysql:8.0.26 创建文件夹 mkdir-p/var/mysql/{data,conf}  配置cd/var/mysql/confvimmy.cnf  输入以下内容 ......
  • mysql 遍历字段并根据不同值归类
    使用procedure,注意报表是否支持特定关键字(积木报表不支持注入关键字)dropprocedureifexistsshow_battery_info;delimiter&&CREATEPROCEDUREshow_battery_info()......