1)描述事物的符号记录称为数据,数字,文字,图形,图像,声音,档案记录等都是数据。
2)数据是以“记录”的形式按照统一的格式进行存储的,而不是杂乱无章的,
相同格式和类型的数据统一存放在一起,而不会把“人”和“书”混在一起存储,这样,数据的存储就能够井然有序。
记录:行
字段(属性):列
以行+列的形式就组成了表(数据存储在表中)
关系数据库的表由记录组成,记录由字段组成,字段由字符或数字组成,他可以供各种用户共享,具有最小冗余度和较高的数据独立性,他是统一管理的相关数据的集合。
数据库就是表的集合,他是以一定的组织方式存储的相互有关的数据集合。
1)接受应用程序的数据请求和处理请求
2)将用户的数据请求(高级指令)转换为复杂机器代码(底层指令)
3)实现对数据库的操作
4)从数据库的操作中接受查询结果
5)对查询结果进行处理(格式转换)
6)将处理结果返回给用户
关系数据库
关系数据库结构是二维数据库表,二维表当中每个字段(列)用来描述对象的一个属性,每个记录(行)用来描述一个对象的信息(完整信息),关系数据库写到哪里也就是存储在硬盘当中,读写系统会受到IO限制或者瓶颈。关系型数据库最典型的数据结构是表,由二维表及其之间的联系所组成的一个数据组织
优点:
1.易于维护,都是使用表结构,格式一致
2.使用方便,sql语言通过,可用于复杂查询
3.复杂操作,支持sql,可用于一个表以及多个表之间非常复杂的查询
缺点:
1.读写性能比较差,尤其是海量数据的高效率读写
2.固定的表结构,灵活度稍欠
3.高并发读写需求,传统关系型数据库来说,硬盘I/O是一个很大的瓶颈
mangoDB,redis(内存数据库/缓存数据库)k-v键值对,与之类似的memcache,k-v键值对
redis-memcache对比:
相同点:存储高热数据(在内存中高速运行) 不同点:redis可以做持久化保存,可以存储对象
非关系型数据库严格上不是一种数据库,应该是一种数据结构化存储方法的集合,可以是文档或者是键值对等
优点:
1.格式灵活:存储数据的格式可以是key,value形式,文档形式,图片形式等,使用灵活,应用场景广泛,而关系型数据库则只支持基础类型,可以高并发读写
2.速度快:nosql可以使用硬盘或者随机存储器作为载体,而关系型数据库只能使用硬盘。对海量数据高效率存储与访问
3.高扩展性,高可用性
4.成本低:nosql数据库部署简单,基本上都是开源软件
缺点:
1.不提供sql支持,学习和使用成本较高
2.无事务处理
3.数据结构相对复杂,复杂查询方面稍欠
4.好多数据写在内存中
在数据库系统中,SQL 语句不区分大小写,建议用大写
SQL语句可单行或多行书写,默认以 " ; " 结尾
关键词不能跨多行或简写
用空格和TAB 缩进来提高语句的可读性
子句通常位于独立行,便于编辑,提高可读性
DDL: Data Defination Language 数据定义语言,用于创建数据库对象,用于数据库的操作,如库、表、索引等。eg:create、drop、alter。
数据库相关操作(查show、增create、删drop):
1、查看数据库信息:show database;
2、创建数据库:create database 数据库名;
3、删除数据库:drop database 数据库名;
DML: Data Manipulation Language 数据操纵语言,用于对表中的数据进行管理,用来插入、删除、修改数据库中的数据。eg:insert、delete、update。
DQL:数据查询语言,用于从数据表中查找符合条件的数据记录。eg:select。
DCL:数据控制语言,用于设置或者更改数据库用户或角色权限(数据控制语句,用于控制语句,用于控制不同数据段直接的许可和访问级别的语句,这些 语句定义了数据库、表、字段、用户的访问权限和安全级别,如commit、follback、grant、revoke)
###数据库的基本操作命令###
【查看数据库列表信息】
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
【查看数据库中的数据表信息】
mysql> use mysql
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_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
【显示数据表的结构(字段)】
mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | NO | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
| password_last_changed | timestamp | YES | | NULL | |
| password_lifetime | smallint(5) unsigned | YES | | NULL | |
| account_locked | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)
###DDL操作命令###
●DDL语句用于创建数据库对象,如库、表、索引等
1、使用DDL语句新建库、表
创建数据库:CREATE DATABASE 数据库名
创建数据表:CREATE TABLE 表名 (字段定义……)
mysql> use auth;
Database changed
mysql> create table users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name));
mysql> create table ky (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', level CHAR(16) NOT NULL, PRIMARY KEY (user_name));
2、使用DDL语句删除库、表
●删除指定的数据表:DROP TABLE [数据库名.]表名
mysql> drop table auth.users;
Query OK, 0 rows affected (0.00 sec)
●删除指定的数据库 :DROP DATABASE 数据库名
mysql> drop database auth;
Query OK, 0 rows affected (0.00 sec)
###DML操作命令###
DML语句用于对表中的数据进行管理
包括以下操作
INSERT:插入新数据
UPDATE :更新原有数据
DELETE :删除不需要的数据
#####################
●向数据表中插入新的数据记录
INSERT INTO 表名(字段1, 字段2, ……) VALUES(字段1的值, 字段2的值, ……)
insert into users(user_name,user_passwd) values('zhangsan', password('123456')); ###标准使用方法
insert into users values('lisi', password('123456')); ###如果这条记录包含表中所有字段的值,则插入语句中的制定字段可以省略
mysql> create database auth; ###刚才删掉了,现在要创建了
Query OK, 1 row affected (0.00 sec)
mysql> use auth; ###进入数据库
Database changed
mysql> create table users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name)); #创建一个表
Query OK, 0 rows affected (0.01 sec)
mysql> insert into users(user_name,user_passwd) values('zhangsan', password('123456')); #在表种插入张三 密码123456
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> insert into users values('lisi', password('123456'));
Query OK, 1 row affected, 1 warning (0.00 sec)
●修改、更新数据表中的数据记录
UPDATE 表名 SET 字段名1=值1[,字段名2=值2] WHERE 条件表达式
mysql> update auth.users set user_passwd=password('abc123') where user_name='lisi';
Query OK, 0 rows affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
###修改root密码###
update mysql.user set authentication_string=PASSWORD('abc123') where User='root'; ####mysql5.7版本修改root的命令
###DQL操作命令###
####SQL语句概述####
1、SQL语言
是Structured Query Language的缩写,即结构化查询语言
是关系型数据库的标准语言
用于维护管理数据库,如数据查询、数据更新、访问控制、对象管理等功能
2、SQL分类
●DDL:数据定义语言 Data Definition Language
●DML:数据操纵语言 Data Manipulation Language
●DQL:数据查询语言 Data Query Language
●DCL:数据控制语言 Data Control Language
mysql -u root -p ##这个命令敲下,提示要输入密码,这个就是刚才设置的密码abc123
#########################
mysql> status; ####查看数据库基本信息
mysql> exit ####退出MySQL操作环境
Bye
#########################
mysql> show databases; #####查看数据库列表信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| myadm |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
##########################
查看数据库中的数据表信息
USE 数据库名
SHOW TABLES
#####列子###
mysql> use mysql; ####切到mysql 这个库, 解释:拿这本书
mysql> show tables; ####查看着个库中有哪些表 解释:看下这本书目录结构
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
##########################
显示数据表的结构(字段)
DESCRIBE [数据库名.]表名
######列子#####
mysql> use mysql ####切到mysql 这个库, 解释:拿这本书
Database changed
mysql> describe user; ####查看着个user表的结构
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | NO | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
| password_last_changed | timestamp | YES | | NULL | |
| password_lifetime | smallint(5) unsigned | YES | | NULL | |
| account_locked | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)
【DDL语句操作】数据定义语言
###########################
●DDL语句用于创建数据库对象,如库、表、索引等
●使用DDL语句新建库、表
##########################
创建新的数据库
CREATE DATABASE 数据库名
刚创建的数据库是空的,其中不包含任何表,在/usr/local/mysql/data 目录下会自动生
成一个与新建的数据库名相同的文件夹
#####列子#######
mysql> create database auth; ####新建一个auth数据库
Query OK, 1 row affected (0.00 sec)
###########################
创建新的数据表
CREATE TABLE 表名 (字段定义……)
######列子########
mysql> use auth;
mysql> create table users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name));
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; ###查看新建的users表
+----------------+
| Tables_in_auth |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
#################
使用DDL语句删除库、表
●删除指定的数据表:DROP TABLE [数据库名.]表名
●删除指定的数据库 :DROP DATABASE 数据库名
###########################
删除指定的数据表
DROP TABLE [数据库名.]表名
#####列子#####
mysql> use auth ####进入auth 这个数据库
Database changed
mysql> drop table auth.users; ####删除auth中这个users表
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; ###发现表已经删除
Empty set (0.00 sec)
############################
删除指定的数据库
DROP DATABASE 数据库名
######列子####
mysql> drop database auth; #####删除auth数据库
Query OK, 0 rows affected (0.01 sec)
mysql> show databases; ####发现auth数据库没了
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
【DML语句操作】
#################
DML语句用于对表中的数据进行管理
包括以下操作
●INSERT:插入新数据
●UPDATE :更新原有数据
●DELETE :删除不需要的数据
############################
向数据表中插入新的数据记录
INSERT INTO 表名(字段1, 字段2, ……) VALUES(字段1的值, 字段2的值, ……)
insert into users(user_name,user_passwd) values('zhangsan', password('123456')); ###标准使用方法
insert into users values('lisi', password('123456')); ###如果这条记录包含表中所有字段的值,则插入语句中的制定字段可以省略
#######列子#####
mysql> use auth; ####进入auth 这个数据库
Database changed
mysql> insert into users(user_name,user_passwd) values('zhangsan', password('123456'));
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> insert into users values('lisi', password('123456'));
Query OK, 1 row affected, 1 warning (0.01 sec)
###########################
修改、更新数据表中的数据记录
UPDATE 表名 SET 字段名1=值1[,字段名2=值2] WHERE 条件表达式
update auth.users set user_passwd=password('123456') where user_name='lisi';
update mysql.user set authentication_string=PASSWORD('abc123') where User='root'; ####mysql5.7版本修改root的命令
############列子#########
mysql> update auth.users set user_passwd=password('abc123') where user_name='lisi'; ###修改lisi的密码
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges; ####刷新下
########列子#########
mysql> update mysql.user set authentication_string=PASSWORD('password') where User='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges; ####刷新下
############################
在数据表中删除指定的数据记录
DELETE FROM 表名 WHERE 条件表达式
delete from auth.users where user_name='lisi';
######列子####
mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6691484EA6B50DDDE1926A220DA01FA9E575C18A |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> delete from auth.users where user_name='lisi';
Query OK, 1 row affected (0.01 sec)
mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
【DQL语句操作】
DQL是数据查询语句,只有一条:SELECT
用于从数据表中查找符合条件的数据记录
############################
从数据表中查找符合条件的数据记录
SELECT 字段名1,字段名2 …… FROM 表名 WHERE 条件表达式
select * from auth.users;
select user_name,user_passwd from auth.users where user_name='zhangsan';
######列子######
mysql> select * from auth.users; ###查看数据库auth中users表中所有的数据,这边用通配符*表示所有
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
######列子######
mysql> select user_name,user_passwd from auth.users where user_name='zhangsan'; ####查看数据库auth的users表中zhangsan 数据
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
####
mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
【数据库表高级操作】
关于数据库表的一些基础操作,比如:创建表、删除表、表的 查询等等。数据表在数据库的使用过程中,
有着很高的使用频率,所以本节会讲解些关于 表的升级用法,包括清空表、使用临时表和克隆表。
1、清空列表
清空一个数据表就是删除这个表内的所有数据。前面的小节已经学习过 DELETE FROM 语句,可以删除表内的数据,
除此之外还可以使用 TRUNCATE TABLE 语句实现清 空表内记录。DELETE FROM 语句可以使用 WHERE 子句对删除
的结果集进行过滤选择, 这样更方便、更灵活。TRUNCATE TABLE 语句是删除表中所有记录数据,没法定制,灵活
性上稍差。清空表的具体操作
create table ky (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', level CHAR(16) NOT NULL, PRIMARY KEY (user_name));
insert into ky values('lisi', password('123456'),'10');
insert into ky values('lisi1', password('123456'),'45');
insert into ky values('lisi2', password('123456'),'46');
insert into ky values('lisi3', password('123456'),'47');
insert into ky values('lisi4', password('123456'),'48');
insert into ky values('lisi5', password('123456'),'49');
insert into ky values('lisi6', password('123456'),'50');
insert into ky values('lisi7', password('123456'),'1');
mysql> show tables;
+----------------+
| Tables_in_auth |
+----------------+
| ky |
| ky05 |
| tmp |
| tmp1 |
| tmp3 |
| users |
+----------------+
6 rows in set (0.00 sec)
mysql> select * from ky;
+-----------+-------------------------------------------+-------+
| user_name | user_passwd | level |
+-----------+-------------------------------------------+-------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 10 |
| lisi1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 45 |
| lisi2 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 46 |
| lisi3 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 47 |
| lisi4 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 48 |
| lisi5 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 49 |
| lisi6 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 50 |
| lisi7 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1 |
+-----------+-------------------------------------------+-------+
8 rows in set (0.00 sec)
mysql> delete from ky where level >=45;
Query OK, 6 rows affected (0.00 sec)
mysql> select * from ky;
+-----------+-------------------------------------------+-------+
| user_name | user_passwd | level |
+-----------+-------------------------------------------+-------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 10 |
| lisi7 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1 |
+-----------+-------------------------------------------+-------+
2 rows in set (0.00 sec)
mysql> truncate table ky;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from ky;
Empty set (0.00 sec)
2.临时表
MySQL 的临时表,顾名思义,就是临时建立的表,并不会长期存在,主要用于保存一 些临时数据。临时表有个特性,
就是只在当前连接可见,当前连接下可执行增删改查等操作, 当连接被关闭后,临时表就会被 MySQL 删除,相关的资源也会被释放。
下面创建临时表 mytmp,然后插入数据,之后断开当前连接,最后重新连到 MySQL 查看临时是否还存在,具体操作如下
mysql>CREATE TEMPORARY TABLE `mytmp` (`id` int(10) NOT NULL AUTO_INCREMENT,`NAME` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,`level` int(10) NOT NULL,PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into mytmp(name,level) values('aa',10);
select * from mytmp;
退出数据库
mysql -uroot -p
mysql> use auth;
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> select * from mytmp;
ERROR 1146 (42S02): Table 'auth.mytmp' doesn't exist
3、克隆表
在 MySQL 的开发和维护过程中,会有原样拷贝某个数据表的需求。怎么样才能够快速、 完整的拷贝数据表呢?
先来看一下 CREATE TABLE new_tablename AS SELECT 这个语 句,具体实现的 SQL 语句如下
mysql> select * from mytmp;
ERROR 1146 (42S02): Table 'auth.mytmp' doesn't exist
mysql> ^C
mysql> select * from ky;
Empty set (0.00 sec)
mysql> insert into ky values('lisi', password('123456'),'10');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi1', password('123456'),'45');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi2', password('123456'),'46');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi3', password('123456'),'47');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi4', password('123456'),'48');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi5', password('123456'),'49');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi6', password('123456'),'50');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into ky values('lisi7', password('123456'),'1');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql>
mysql> drop table tmp;
Query OK, 0 rows affected (0.00 sec)
mysql> create table tmp as select * from ky;
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select count(*) from tmp;
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)
mysql> use auth;
Database changed
mysql> create table tmp like users; ###通过 LIKE 方法,复制users表生成 tmp 表
Query OK, 0 rows affected (0.00 sec)
mysql> describe tmp; ###表的结构
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name | char(16) | NO | PRI | NULL | |
| user_passwd | char(48) | YES | | | |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from auth.tmp ; ###查询表的数据
Empty set (0.00 sec)
mysql> select * from users; ###查询users表数据,准备导入tmp表中
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into tmp select * from users; ###将users表数据导入tmp中
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from tmp; ###查询tmp中数据
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from ky;
+-----------+-------------------------------------------+-------+
| user_name | user_passwd | level |
+-----------+-------------------------------------------+-------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 10 |
| lisi1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 45 |
| lisi2 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 46 |
| lisi3 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 47 |
| lisi4 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 48 |
| lisi5 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 49 |
| lisi6 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 50 |
| lisi7 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1 |
+-----------+-------------------------------------------+-------+
8 rows in set (0.00 sec)
mysql> truncate table ky;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from ky;
Empty set (0.00 sec)
mysql> show create table ky\G
*************************** 1. row ***************************
Table: ky
Create Table: CREATE TABLE "ky" (
"user_name" char(16) NOT NULL,
"user_passwd" char(48) DEFAULT '',
"level" char(16) NOT NULL,
PRIMARY KEY ("user_name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
CREATE TABLE test2 AS SELECT * FROM test1;
CREATE TABLE test3 ("user_name" char(16) NOT NULL,"user_passwd" char(48) DEFAULT '',"level" char(16) NOT NULL,PRIMARY KEY ("user_name")) ENGINE=InnoDB DEFAULT CHARSET=utf8
【DCL语句操作】
############################
设置用户权限(用户不存在时,则新建用户)
GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY ‘密码’ ]
grant select on auth.* to 'xiaoqi'@'localhost' identified by '123456';
######列子####
mysql> grant select on auth.* to 'xiaoqi'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
######退出mysql用xiaoqi账户登录####
[root@localhost ~]# mysql -u xiaoqi -p ##登录
Enter password: ##输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.20 Source distribution
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> ###已经登录
#######验证xiaoqi#####
mysql> select * from auth.users; ###验证授权的访问操作
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> drop table auth.users; ####验证非授权的访问操作
ERROR 1142 (42000): DROP command denied to user 'xiaoqi'@'localhost' for table 'users'
########创建bdqn数据库,授权192.168.100.1的主机连接,用户名为dbuser 密码为pwd@123允许bdqn库中操作###
create database bdqn;
grant all on auth.* to 'dbuser'@'192.168.100.%' identified by 'pwd@123';
grant create on bdqn.* to 'dbuser'@'192.168.100.1' identified by 'pwd@123';
#######列子####3
mysql> create database bdqn; ####创建bdqn数据库
Query OK, 1 row affected (0.02 sec)
mysql> grant all on bdqn.* to 'dbuser'@'192.168.100.1' identified by 'pwd@123'; ###做授权###
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants; #查看授权情况
############################
查看用户的权限
SHOW GRANTS FOR 用户名@来源地址
######列子####
mysql> show grants for 'dbuser'@'192.168.100.1'; ####查看用户的权限
+--------------------------------------------------------------+
| Grants for [email protected] |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dbuser'@'192.168.100.1' |
| GRANT ALL PRIVILEGES ON "bdqn".* TO 'dbuser'@'192.168.100.1' |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)
revoke all on bdqn.* from 'xiaoqi'@'localhost';
############################
撤销用户的权限
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址
revoke all on bdqn.* from 'dbuser'@'192.168.100.1';
#####列子####
mysql> revoke all on bdqn.* from 'dbuser'@'192.168.32.2';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'dbuser'@'192.168.100.1';
+------------------------------------------------+
| Grants for [email protected] |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'dbuser'@'192.168.100.1' |
+------------------------------------------------+
1 row in set (0.00 sec)
############问题1#####
遇到root密码忘了怎么办?
【处理方案】
1、在 my.ini 或 my.cnf 的 [mysqld] 下添加 skip-grant-tables
[root@localhost mysql]# vi /etc/my.cnf
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
skip-grant-tables ####此处添加###
2、重启mysql数据库
[root@localhost mysql]# systemctl restart mysqld
3、登录数据库
[root@localhost mysql]# mysql -uroot -p
Enter password: ###回车直接登录
mysql>
4、修改root密码
mysql> update mysql.user set authentication_string=PASSWORD('abc123') where User='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5、修改mysql免密码登录配置 然后重启服务
[root@localhost mysql]# vi /etc/my.cnf
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
#skip-grant-tables
6、重启mysql数据库
[root@localhost mysql]# systemctl restart mysqld
7、mysql数据库验证
[root@localhost mysql]# mysql -uroot -p
Enter password: ####输入abc123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution
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>
######案例:建立公司IT运营部工资表#####
需求描述:
为公司建立员工工资数据库 imployee_salary
在imployee_salary数据库中,建立 IT_salary数据表,以保存IT运营部员工的工资信息,如下表所示
表格如下:
岗位类别 姓名 年龄 员工ID 学历 年限 薪资
网络工程师 孙空武 27 011 本科 3 4800
Windows工程师 蓝凌 19 012 中专 2 3500
Linux工程师 姜纹 32 013 本科 8 15000
Java软件工程师 关园 38 014 大专 10 16000
硬件驱动工程师 罗中昆 29 015 大专 9 16500
1、建立数据库
mysql> create database imployee_salary;
Query OK, 1 row affected (0.00 sec)
mysql> use imployee_salary;
Database changed
2、建立数据表IT_salary ###这边一定要注意是中文的逗号还是英文的逗号
MySQL [imployee_salary]> create table IT_salary (
-> type varchar(32) not null,
-> name char(16) not null,
-> age tinyint not null,
-> id smallint not null,
-> degree char(16) default '大专',
-> years tinyint not null,
-> salary int not null,
-> primary key (id));
3、将IT运营部的员工工资信息插入到IT_salary
mysql> insert into IT_salary values(
-> '网络工程师','孙空武',27,011,'本科',3,4800);
Query OK, 1 row affected (0.04 sec)
mysql> insert into IT_salary values(
-> 'Windows工程师','蓝凌',19,012,'中专',2,3500);
Query OK, 1 row affected (0.01 sec)
mysql> insert into IT_salary values(
-> 'Linux工程师','姜纹',32,013,'本科',8,15000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into IT_salary values(
-> 'Java软件工程师','关园',38,014,'大专',10,16000);
Query OK, 1 row affected (0.00 sec)
4、查看核对信息
mysql> select * from IT_salary;
+---------------------+-----------+-----+----+--------+-------+--------+
| type | name | age | id | degree | years | salary |
+---------------------+-----------+-----+----+--------+-------+--------+
| 网络工程师 | 孙空武 | 27 | 11 | 本科 | 3 | 4800 |
| Windows工程师 | 蓝凌 | 19 | 12 | 中专 | 2 | 3500 |
| Linux工程师 | 姜纹 | 32 | 13 | 本科 | 8 | 15000 |
| Java软件工程师 | 关园 | 38 | 14 | 大专 | 10 | 16000 |
+---------------------+-----------+-----+----+--------+-------+--------+
4 rows in set (0.00 sec)