MySQL 数据库的使用
数据库(database):保存有组织的数据的容器(通常是一个文件或一组文件)。
一、查询可用数据库
SHOW DATABASES;
返回可用数据库的一个列表。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
二、创建数据库
create 命令创建数据库
CREATE DATABASE <数据库名>;
mysql> create DATABASE testdb;
Query OK, 1 row affected (0.11 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
CREATE DATABASE IF NOT EXISTS <数据库名>;
这个命令表示:如果这个数据库不存在,则创建;否则不创建。
mysql> create database if not exists testdb;
Query OK, 1 row affected, 1 warning (0.08 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database if not exists testdb2;
Query OK, 1 row affected (0.07 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
| testdb2 |
+--------------------+
6 rows in set (0.00 sec)
使用 mysqladmin 创建数据库
使用普通用户,可能需要特定的权限来创建或者删除 MySQL 数据库。
可以使用 root 用户登录,root 用户拥有最高权限,可以使用mysqladmin
命令来创建数据库。
以下命令简单的演示了创建数据库的过程,数据名为 testdb3:
F:\web\mysql-8.0.17-winx64\bin>mysqladmin -u root -p create testdb3
Enter password: ******
F:\web\mysql-8.0.17-winx64\bin>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
| testdb2 |
| testdb3 |
+--------------------+
7 rows in set (0.00 sec)
三、删除数据库
使用普通用户登陆 MySQL 服务器,可能需要特定的权限来创建或者删除 MySQL 数据库,所以使用 root 用户登录,root 用户拥有最高权限。
在删除数据库过程中,务必要十分谨慎,因为在执行删除命令后,所有数据将会消失。
drop 命令删除数据库
drop database <数据库名>;
mysql> drop database testdb3;
Query OK, 0 rows affected (0.12 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
| testdb2 |
+--------------------+
6 rows in set (0.00 sec)
使用 mysqladmin 删除数据库
可以使用 mysqladmin
命令在终端来执行删除命令。
F:\web\mysql-8.0.17-winx64\bin>mysqladmin -u root -p drop testdb2
Enter password: ******
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'testdb2' database [y/N] y
Database "testdb2" dropped
使用这个命令会出现一个提示,来确认是否真的删除数据库。
四、选择数据库
在连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以需要选择你要操作的数据库。
必须先使用 USE
打开数据库,才能读取其中的数据。
mysql> use testdb;
Database changed
执行以上命令后,就已经成功选择了 testdb 数据库,在后续的操作中都会在 testdb 数据库中执行。
注意:所有的数据库名,表名,表字段都是区分大小写的。所以在使用 SQL 命令时需要输入正确的名称。
MySQL 数据表的使用
表(table):某种特定类型数据的结构化清单。
数据库中的每个表都有一个名字,用来标识自己。此名字是唯一的, 这表示数据库中没有其他表具有相同的名字。
一、创建数据表
创建MySQL数据表需要信息:表名、表字段名、定义每个表字段。
CREATE TABLE <table_name> (<column_name> <column_type>);
在 testdb 数据库中创建数据表 student_tbl:
mysql> use testdb;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS student_tbl(
-> student_id INT NOT NULL AUTO_INCREMENT,
-> student_name VARCHAR(100) NOT NULL,
-> student_gender VARCHAR(40) NOT NULL,
-> submission_date DATE,
-> PRIMARY KEY (student_id)
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 1 warning (0.28 sec)
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| student_tbl |
+------------------+
1 row in set (0.00 sec)
mysql> show columns from student_tbl;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| student_id | int(11) | NO | PRI | NULL | auto_increment |
| student_name | varchar(100) | NO | | NULL | |
| student_gender | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
对于上面的例子:
- 如果不想字段为 NULL 可以设置字段的属性为 NOT NULL,在操作数据库时如果输入该字段的数据为NULL,就会报错。
含有 NULL 值的列很难进行查询优化,而且对表索引时不会存储 NULL 值的,所以如果索引的字段可以为 NULL,索引的效率会下降很多。因为它们使得索引、索引的统计信息以及比较运算更加复杂。可以用 0、一个特殊的值或者一个空串代替空值。 - AUTO_INCREMENT 定义列为自增的属性,一般用于主键,数值会自动加1。
- PRIMARY KEY 关键字用于定义列为主键。可以使用多列来定义主键,列间以逗号分隔。
- ENGINE 设置存储引擎,CHARSET 设置编码。
- InnoDB,是MySQL的数据库引擎之一,现为MySQL的默认存储引擎,为 MySQL AB 发布 binary 的标准之一。事务型数据库的首选引擎,支持 ACID 事务,支持行级锁定。InnoDB是为处理巨大数据量时的最大性能设计。
二、删除数据表
在进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失。
DROP TABLE table_name;
删除数据库 testdb 中的表 student_tbl:
mysql> drop table student_tbl;
Query OK, 0 rows affected (0.24 sec)
mysql> show tables;
Empty set (0.00 sec)
MySQL 删除表的几种情况
1、drop table <table_name>;
删除表全部数据和表结构,立刻释放磁盘空间,不管是 InnoDB 还是 MyISAM。例,删除学生表:
drop table student;
2、truncate table <table_name>;
删除表全部数据,保留表结构,立刻释放磁盘空间,不管是 InnoDB 还是 MyISAM。例,删除学生表:
truncate table student;
3、delete from <table_name>;
删除表全部数据,表结构不变,对于 MyISAM 会立刻释放磁盘空间,InnoDB 不会释放磁盘空间。例,删除学生表:
delete from student;
4、delete from <table_name> where xxx;
带条件的删除,表结构不变,不管是 InnoDB 还是 MyISAM 都不会释放磁盘空间。例,删除学生表中姓名为“张三”的数据:
delete from student where T_name = "张三";
5、delete 操作以后,使用 optimize table table_name
会立刻释放磁盘空间,不管是 InnoDB 还是 MyISAM。例,删除学生表中姓名为 “张三” 的数据并释放学生表的表空间:
delete from student where T_name = "张三";
optimize table student;
6、delete from 表以后虽然未释放磁盘空间,但是下次插入数据的时候,仍然可以使用这部分空间。