MySQL 数据库和表的使用


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 表以后虽然未释放磁盘空间,但是下次插入数据的时候,仍然可以使用这部分空间。


学习 MySQL:目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在PyCharm中使用MySQL创建数据库和表,你可以按照以下步骤进行操作: 1. 安装MySQL Connector/Python:在PyCharm中使用MySQL,首先需要安装MySQL Connector/Python。你可以使用以下命令在PyCharm的终端或系统终端中安装: ``` pip install mysql-connector-python ``` 2. 导入MySQL Connector库:在Python脚本中,导入MySQL Connector库以便连接和操作MySQL数据库。 ```python import mysql.connector ``` 3. 连接到MySQL数据库使用MySQL Connector库提供的`connect()`函数连接到MySQL数据库。你需要提供数据库的相关信息,如用户名、密码、主机名和数据库名称。 ```python config = { 'user': 'your_username', 'password': 'your_password', 'host': 'localhost', 'database': 'your_database', } conn = mysql.connector.connect(**config) ``` 4. 创建数据库使用已建立的数据库连接对象,创建一个新的数据库。 ```python cursor = conn.cursor() cursor.execute("CREATE DATABASE your_database") ``` 5. 创建表:在已连接的数据库上,使用SQL语句创建一个新的表。 ```python cursor.execute("CREATE TABLE your_table (column1 datatype, column2 datatype, ...)") ``` 注意替换SQL语句中的`your_table`为你想创建的表名称,并为每个列指定名称和数据类型。 6. 提交更改并关闭连接:在创建数据库和表之后,记得提交更改和关闭连接。 ```python conn.commit() cursor.close() conn.close() ``` 这样,你就可以使用PyCharm和MySQL Connector/Python在MySQL数据库中创建数据库和表。希望这些步骤对你有所帮助!如有任何问题,请随时向我提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值