数据库(MySQL)练习
- 一、练习
- 1.15练习
- 练习
- 二、注意事项
- 2.1 第四天
一、练习
1.15练习
win11安装配置MySQL超详细教程: https://baijiahao.baidu.com/s?id=1786910666566008458&wfr=spider&for=pc
准备工作:
mysql -uroot -p #以管理员身份登录
mysql> select user(); #查看当前登录账户,登录方式
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)mysql> show databases; #查看当下账户的所有库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb1_test |
| mydb2_stuinfo |
| mydb3_employee |
| mydb4_product |
| mydb5_sales |
| mydb_temp1 |
| mysql |
| performance_schema |
| sys |
| temp1 |
+--------------------+
11 rows in set (0.00 sec)mysql> create database mydb6_product; #创建新库 mydb6_product
Query OK, 1 row affected (0.02 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb1_test |
| mydb2_stuinfo |
| mydb3_employee |
| mydb4_product |
| mydb5_sales |
| mydb6_product |
| mydb_temp1 |
| mysql |
| performance_schema |
| sys |
| temp1 |
+--------------------+
12 rows in set (0.00 sec)
mysql> use mydb6_product; #转换当前登录库
Database changed
mysql> select database();
+---------------+
| database() |
+---------------+
| mydb6_product |
+---------------+
1 row in set (0.00 sec)
employees表:
主键:primary key
不能为空:not null
设置默认值:default’***'
mysql> create table employees(id int primary key , name varchar(50) not null ,age int ,gender varchar(10) not null default'unknown' ,salary float);
Query OK, 0 rows affected (0.04 sec)mysql> desc employees;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(10) | NO | | unknown | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)
orders表
mysql> create table orders(id int primary key , name varchar(100) not null ,price float,
quantity int ,category varchar(50));
Query OK, 0 rows affected (0.03 sec)mysql> desc orders;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| price | float | YES | | NULL | |
| quantity | int | YES | | NULL | |
| category | varchar(50) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
invoices表:
主键自增长:primary key auto_increment
外键关联到orders表的id列:,foreign key(order_id) references orders(id) 注意用逗号分隔后再开始写外键关联
日期型:date
要求数据大于0:check(数据>0)
mysql> create table invoices(number int primary key auto_increment, order_id int ,foreign key(order_id) references orders(id) ,in_date date,total_amount float check(total_amount>0));
Query OK, 0 rows affected (0.03 sec)mysql> desc invoices;
+--------------+-------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------+------+-----+---------+----------------+
| number | int | NO | PRI | NULL | auto_increment |
| order_id | int | YES | MUL | NULL | |
| in_date | date | YES | | NULL | |
| total_amount | float | YES | | NULL | |
+--------------+-------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
3张表如下:
mysql> show tables;
+-------------------------+
| Tables_in_mydb6_product |
+-------------------------+
| employees |
| invoices |
| orders |
+-------------------------+
3 rows in set (0.01 sec)
练习
二、注意事项
2.1 第四天
日期需要以字符串形式添加 eg.‘2000-10-1’
mysql> insert into student2 value(1001 , '李成峰','男','2000-10-1');
Query OK, 1 row affected (0.01 sec)mysql> select * from student2;
+------+--------+------+------------+
| id | name | sex | birthday |
+------+--------+------+------------+
| 1001 | 李成峰 | 男 | 2000-10-01 |
+------+--------+------+------------+
1 row in set (0.00 sec)
delete一定要带where,
mysql> select * from student2;
+------+--------+------+------------+
| id | name | sex | birthday |
+------+--------+------+------------+
| 1001 | 李成峰 | 男 | 2000-10-01 |
| 1002 | 薛佳尹 | 女 | 2012-12-12 |
| 1003 | 孙德胜 | 男 | 1998-12-31 |
| 1004 | 凤飞飞 | 女 | 2009-03-15 |
| 1005 | 尹志平 | 男 | 1235-09-01 |
+------+--------+------+------------+
5 rows in set (0.00 sec)mysql> delete from student2 where id=1005; #单条删除
Query OK, 1 row affected (0.02 sec)mysql> select * from student2;
+------+--------+------+------------+
| id | name | sex | birthday |
+------+--------+------+------------+
| 1001 | 李成峰 | 男 | 2000-10-01 |
| 1002 | 薛佳尹 | 女 | 2012-12-12 |
| 1003 | 孙德胜 | 男 | 1998-12-31 |
| 1004 | 凤飞飞 | 女 | 2009-03-15 |
+------+--------+------+------------+
4 rows in set (0.00 sec)
delete from student2不加where,删除全表内容,表结构还存在
drop 删除包括表结构
删除之前可开启一个事务,方便回溯
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
开启一个事务,方便回溯mysql> delete from student2 ;
Query OK, 4 rows affected (0.00 sec)mysql> select * from student2;
Empty set (0.00 sec)mysql> desc student2;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | NO | | NULL | |
| sex | char(2) | YES | | 女 | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)mysql> rollback;
Query OK, 0 rows affected (0.00 sec)mysql> select * from student2;
+------+--------+------+------------+
| id | name | sex | birthday |
+------+--------+------+------------+
| 1001 | 李成峰 | 男 | 2000-10-01 |
| 1002 | 薛佳尹 | 女 | 2012-12-12 |
| 1003 | 孙德胜 | 男 | 1998-12-31 |
| 1004 | 凤飞飞 | 女 | 2009-03-15 |
+------+--------+------+------------+
4 rows in set (0.00 sec)