一、数据库简介
我们常用的数据存储方式,比如内存和文件,数据保存在内存中时,存取速度快,但是数据不能永久保存 ;数据保存在文件时,数据永久,但是速度比内存操作慢,频繁的IO操作,并且查询数据不方便。而数据库的存储方式,数据持久化保存,高可靠,高可用,数据的快速提取。所以在企业中得以广泛使用。
安装MySQL的详细步骤在这篇文章MYSQL安装
二、查看数据库
查看有已有哪些数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql默认4个数据库
information_schema: 定义访问数据库元数据的方式。数据库名和表名,列的数据类型、访问权限等。
mysql:核心数据库,负责存储数据库用户、权限、关键字等用户自己需要使用的控制和管理信息。
performance_schema:数据库的性能参数,存储引擎等。
sys:sys系统库下包含许多视图,它们以各种方式对preformance_schema表进行聚合计算展示。
查看数据库中有哪些表:
mysql> use mysql; #use后跟数据库名
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
......
Mysql数据库的数据文件都存放在/usr/local/mysql/data目录下,每个数据库对应一个子目录,用于储存数据表文件。每一个数据表对应为三个文件,后缀名分别为’’.frm “.myd” 和“.myi"。当然也有少数以opt、csm、csv、ibd结尾的。
查看表的结构:
mysql> use mysql;#先使用数据库
mysql> desc user;#再查看表
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
或者
mysql> desc mysql.user;#用 数据库名.表名 的格式查看
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
三、SQL语句
SQL分类
- DDL:数据定义语言
- DML:数据操纵语
- DQL:数据查询语言(select)
- DCL:数据控制语言
3.1 DDL语句
DDL语句用于创建数据库对象,如库、表、索引等。
1、新建库、表
mysql> create database student;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;#可查看到
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
+--------------------+
5 rows in set (0.00 sec)
#建表
mysql> use student;
Database changed
mysql> create table info (id int(3) not null primary key,name char(10) not null,address varchar(50) default 'nj');
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| info |
+-------------------+
1 row in set (0.00 sec)
#查看表结构
mysql> describe info;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(3) | NO | PRI | NULL | |
| name | char(10) | NO | | NULL | |
| address | varchar(50) | YES | | nj | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
create table 表名(字段01名称 字段01类型 字段01约束,字段02名 字段02类型 字段02约束)存储引擎 字符集
多个字段用逗号加空格隔开。
字段01名称:属性名称,自定义
字段01类型:
int (5) 整型 00000-99999
double 浮点型
decimal(5,2) 有效数字是5位,小数点后面保留2位
fioat 单精度浮点 4字节
varchar(50) 可变长度字符串
char(10) 固定长度字符串
字段01约束:
非空约束:内容不允许为空
主键约束:非空且唯一 典型的标识
默认值:假如没有填数据,默认预先设定的值
自增值:自动排序1、2、3、4…
存数引擎:myisam innodb
字符集:UTF-8
2、删除库、删除表
mysql> drop database 后面跟库名或者 库名.表名;
Query OK, 0 rows affected (0.02 sec)
3.2 DML语句
DML语句用于对表中的数据进行管理
- INSERT:插入新数据
- UPDATE:更新原有数据
- DELETE:删除不不需要的数据
1、表中插入数据
#方法1:
insert into info (id,name,address) values (1,'lisi','上海'),(3,'wangwu','北京');#对应各个字段
#方法2:
insert into info values (4,'yiyi','北京');
查看:
mysql> select * from info;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | lisi | 上海 |
| 3 | wangwu | 北京 |
| 4 | yiyi | 北京 |
+----+--------+---------+
3 rows in set (0.00 sec)
2、修改、更新数据表中的数据记录
UPDATe 表名 SET 字段名1=值1[,字段名2=值2] WHERe条件表达式
#将info表中lisi的地址替换为苏州
mysql> update tom set address=‘苏州’ where name='lisi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看
mysql> select * from info;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
| 4 | yiyi | 北京 |
+----+--------+---------+
3 rows in set (0.00 sec)
修改用户登录数据库密码
mysql> update mysql.user set authentication_string=password('123123') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
在数据库中删除指定的数据记录
delete from 表名 where 条件表达式(不带where代表删除表中所有记录)
mysql> select * from student.info;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
| 4 | yiyi | 北京 |
+----+--------+---------+
3 rows in set (0.00 sec)
mysql> delete from student.info where name='yiyi';
Query OK, 1 row affected (0.01 sec)
mysql> select * from student.info;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
+----+--------+---------+
2 rows in set (0.00 sec)
3.3 数据库高级操作
1、清空表
delete from info;
truncate table info;
truncate清空表数据,表还在;
drop是删除数据和表;
2、临时表
临时建立的表,用于保存一些临时数据,不会长期存在
mysql> create temporary table cdc (id int(3) not null auto_increment,name varchar(10) not null,hobby varchar(10) not null,primary key(id))engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> describe cdc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| hobby | varchar(10) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into cdc (name,hobby) values ('boy','dog');
Query OK, 1 row affected (0.00 sec)
mysql> select * from cdc;
+----+------+-------+
| id | name | hobby |
+----+------+-------+
| 1 | boy | dog |
+----+------+-------+
1 row in set (0.00 sec)
mysql> show tables;#并没有cdc表
+-------------------+
| Tables_in_student |
+-------------------+
| info |
| jerry |
| tom |
+-------------------+
3 rows in set (0.00 sec)
#不在硬盘上,在内存上
3、克隆表
like方法:从info表完整复制结构生成test表
mysql> use student;
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> create table test like info;
Query OK, 0 rows affected (0.02 sec)
导入数据
mysql> insert into test select * from info;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
+----+--------+---------+
2 rows in set (0.00 sec)
或者用as
mysql> create table zf as select * from info;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from zf;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
+----+--------+---------+
2 rows in set (0.00 sec)
3.4 DCL数据库用户授权
1、DCL语句设置用户权限(用户不存在时;则会新建用户)
mysql> create user 'lisi'@'%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
#设置登录密码为abc123的lisi用户,可以从任意终端(%)登录
#若要限制只能本地登录,%换为localhost,(主机名或终端IP地址)
mysql> grant all on *.* to 'lisi'@'%' identified by '123123';
Query OK, 0 rows affected, 1 warning (0.01 sec)
#对所有库和所有表(*.*)有全部权限
#若只允许对mysql库中user表有select权限
mysql> grant select on mysql.user to 'lisi'@'%' identified by '123123';
2、查看当前用户的权限
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
查看从本地登录的用户的权限
mysql> show grants for 'lisi'@'localhost';
+------------------------------------------------------+
| Grants for lisi@localhost |
+------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lisi'@'localhost' |
| GRANT SELECT ON `mysql`.`user` TO 'lisi'@'localhost' |
+------------------------------------------------------+
3、查看当前系统中的用户
mysql> select user from mysql.user;
+---------------+
| user |
+---------------+
| lisi |
| mysql.session |
| mysql.sys |
| root |
+---------------+
4 rows in set (0.00 sec)
4、撤销用户的权限
mysql> revoke select on mysql.user from 'lisi'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'lisi'@'localhost';
+------------------------------------------+
| Grants for lisi@localhost |
+------------------------------------------+
| GRANT USAGE ON *.* TO 'lisi'@'localhost' |
+------------------------------------------+
1 row in set (0.00 sec)