[toc]
mysql基础 mysql基本操作
Mysql 关系型数据库,表跟表之间可以建立关系
库-->表:列(字段 faield)
行(记录 record)
1.连接mysql
$ mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.6.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
2.设置root密码
方法一 命令行设置
mysqladmin -uroot password '密码'
方法二 进入mysql设置
set password = password('密码');
3.mysql初始安全设置
$ mysql_secure_installation
Enter current password for root (enter for none): # 输入root密码
Change the root password? [Y/n] # 是否改变root密码
Remove anonymous users? [Y/n] # 是否移除匿名用户
Disallow root login remotely? [Y/n] # 是否允许root远程登陆
Remove test database and access to it? [Y/n] # 是否移除test库并且不能访问
Reload privilege tables now? [Y/n] # 是否重新加载权限表
Enter current password for root (enter for none): # 输入root密码
4.mysql文件(yum安装)
$ cd /var/lib/mysql/
$ ls
ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock
文件 | 说明 |
---|---|
ibdata1 | InnoDB存储引擎的系统表空间,存放InnoDB表的数据、回滚段 |
ib_logfile0、ib_logfile1 | InnoDB日志文件组 |
mysql | 数据库 库名字 |
mysql.sock | mysql的socket文件,用于本机用户登陆mysql |
5.库的管理
5.1 创建数据库
create database 数据库名;
mysql> create database DB1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| DB1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.05 sec)
5.2 删除数据库
drop database 数据库名;
mysql> drop database DB1;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.09 sec)
5.3 查看数据库
show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (0.00 sec)
5.4 使用数据库
use 数据库名
mysql> use DB1
Database changed
mysql> select database(); # 查看当前使用哪个数据库
+------------+
| database() |
+------------+
| DB1 |
+------------+
1 row in set (0.00 sec)
6.表的管理
6.1 创建表
创建表
create table 表名(列名1 数据类型,列名2 数据类型);
mysql> create table t1(id int(3),name char(30),sex enum('M','F'),hobby set('a','b','c'));
Query OK, 0 rows affected (0.03 sec)
描述表
desc 表名;
mysql> desc t1;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| name | char(30) | YES | | NULL | |
| sex | enum('M','F') | YES | | NULL | |
| hobby | set('a','b','c') | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
4 rows in set (0.13 sec)
向表中插入数据
insert into 表名 values(...);
mysql> insert into t1 values(1,'xiaoming','M','a,b,c');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+----------+-----+-------+
| id | name | sex | hobby |
+----+----------+-----+-------+
| 1 | xiaoming | M | a,b,c |
+----+----------+-----+-------+
1 row in set (0.12 sec)
6.2删除表
删除一个表
drop table 表名
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.13 sec)
mysql> drop table t1;
Query OK, 0 rows affected (0.12 sec)
删除多个表
drop table 表1,表2,...表n;
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
| t2 |
+---------------+
2 rows in set (0.16 sec)
mysql> drop table t1,t2;
Query OK, 0 rows affected (0.20 sec)
6.3 修改表
6.3.1 增加列
:::tip说明
修改列中不支持before,只有after和first
:::
alter table 表名 add 列名 数据类型;
# 查看t1表,此时表中只有一个列id,现在想增加一个列name
mysql> desc t1;
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
1 row in set (0.10 sec)
# 给t1表增加列name
mysql> alter table t1 add name char(10);
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 查看t1表,已经增加name列
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.10 sec)
add增加列默认是在最后边添加,如果需要指定追到的位置需要做以下操作
# t2表内容如下,现在要追加一个phone列,追加到name列的后边
mysql> desc t2;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| address | char(30) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)
# 需要用到after关键字,此语句表明在name列后追加phone列
mysql> alter table t2 add phone char(11) after name;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t2;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| phone | char(11) | YES | | NULL | |
| address | char(30) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
# 如果要追加到第一列,需要用到first关键字
mysql> alter table t2 add sex enum('F','M') first;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t2;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| sex | enum('F','M') | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| phone | char(11) | YES | | NULL | |
| address | char(30) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
6.3.2 删除列
alter table 表名 drop 列名;
# 查看t1表,表中有两个列id和name,现在要删除name列
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.10 sec)
# 删除name列
mysql> alter table t1 drop name;
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 查看t1表name列已经删除
mysql> desc t1;
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
1 row in set (0.12 sec)
6.3.3 修改列名
alter table 表名 change 旧列名 新列名 数据类型;
:::tip说明
change既可以修改列名,又可以修改列类型
:::
# 查看t1表,表中有id和name两个列,现在要将name列修改为address列
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.10 sec)
# 修改name列
mysql> alter table t1 change name address varchar(30);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 查看t1表,原name列已经修改为address,列类型已由char修改为varchar
mysql> desc t1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| address | varchar(30) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.17 sec)
6.3.4 修改列的数据类型
alter table 表名 modify 列名 新列数据类型;
# t1表中有id列和address列,现在要把address列的数据类型由varchar改为char
mysql> desc t1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| address | varchar(30) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.16 sec)
# 修改address列数据类型为char
mysql> alter table t1 modify address char(20);
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 查看t1表,address列的数据类型已经修改为char
mysql> desc t1;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
2 rows in set (0.19 sec)
6.3.5修改表名
rename table 旧表名 to 新表名;
# 查看表,现在要将表t1修改为table1
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.14 sec)
# 修改表t1为table1
mysql> rename table t1 to table1;
Query OK, 0 rows affected (0.16 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| table1 |
+---------------+
1 row in set (0.18 sec)
6.4 查看表
show tables
# 先进入一个库
mysql> use db1;
Database changed
# 查看库中所有的表
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.18 sec)
7.数据管理
7.1 增加数据
insert into 表名 values(......);
方式一 直接插入值
# 查看表t1,现在是一张空表
mysql> desc t1;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
2 rows in set (0.17 sec)
mysql> select * from t1;
Empty set
# 向表中插入数据,插入一条
mysql> insert into t1 values(1,'北京');
Query OK, 1 row affected (0.19 sec)
# 向表中插入数据,插入多条
mysql> insert into t1 values(2,'上海'),(3,'广州'),(4,'深圳');
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0
# 查看t1表中的数据
mysql> select * from t1;
+----+---------+
| id | address |
+----+---------+
| 1 | 北京 |
| 2 | 上海 |
| 3 | 广州 |
| 4 | 深圳 |
+----+---------+
4 rows in set (0.16 sec)
方式二 从别的表中选择数据插入
# 创建t1表
mysql> create table t1(id int primary key auto_increment,address char(10));
Query OK, 0 rows affected (0.02 sec)
# 向表中插入数据
mysql> insert into t1(address) values('北京'),('杭州'),('深圳');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+---------+
| id | address |
+----+---------+
| 1 | 北京 |
| 2 | 杭州 |
| 3 | 深圳 |
+----+---------+
3 rows in set (0.00 sec)
# 创建t2表
mysql> create table t2(id int primary key auto_increment,address char(10));
Query OK, 0 rows affected (0.02 sec)
# 将t1表中的内容插入到t2表中
mysql> insert into t2(select * from t1);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t2;
+----+---------+
| id | address |
+----+---------+
| 1 | 北京 |
| 2 | 杭州 |
| 3 | 深圳 |
+----+---------+
3 rows in set (0.00 sec)
如果两个表中的字段不一致,从另一张表中插入数据的时候需要手动指定字段
# 创建t3表
mysql> create table t3(id int primary key auto_increment,address char(10),qnum tinyint);
Query OK, 0 rows affected (0.02 sec)
# 此时想插入t1表的数据,需要手动指定一下两张表中的共同字段
mysql> insert into t3(id,address) (select * from t1);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+----+---------+------+
| id | address | qnum |
+----+---------+------+
| 1 | 北京 | NULL |
| 2 | 杭州 | NULL |
| 3 | 深圳 | NULL |
+----+---------+------+
3 rows in set (0.00 sec)
7.2 删除数据
delete from 表名 where 条件;
# 查看t1表中的数据
mysql> select * from t1;
+----+---------+
| id | address |
+----+---------+
| 1 | 北京 |
| 2 | 上海 |
| 3 | 广州 |
| 4 | 深圳 |
+----+---------+
4 rows in set (0.16 sec)
# 删除表中地址为上海的数据
mysql> delete from t1 where id=2;
Query OK, 1 row affected (0.16 sec)
# 查看t1表中内容
mysql> select * from t1;
+----+---------+
| id | address |
+----+---------+
| 1 | 北京 |
| 3 | 广州 |
| 4 | 深圳 |
+----+---------+
3 rows in set (0.16 sec)
# 再次新建t1表
ysql> create table t1(id int primary key auto_increment,address char(10));
Query OK, 0 rows affected (0.02 sec)
# 向t1表中插入数据
mysql> insert into t1(address) values('北京'),('上海'),('广州');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+---------+
| id | address |
+----+---------+
| 1 | 北京 |
| 2 | 上海 |
| 3 | 广州 |
+----+---------+
# delete方式清空t1表
mysql> delete from t1;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from t1;
Empty set (0.00 sec)
# 向表中插入数据
mysql> insert into t1(address) values('杭州'),('深圳');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
# 发现并没有清空自增字段,如果需要清空自增字段,需要用到truncate语句
mysql> select * from t1;
+----+---------+
| id | address |
+----+---------+
| 4 | 杭州 |
| 5 | 深圳 |
+----+---------+
2 rows in set (0.00 sec)
清空表并重置自增字段
truncate table 表名;
# 创建一个t1表
mysql> create table t1(id int,address char(10));
Query OK, 0 rows affected (0.02 sec)
# 向表中插入数据
mysql> insert into t1 values(1,'北京');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+---------+
| id | address |
+------+---------+
| 1 | 北京 |
+------+---------+
1 row in set (0.00 sec)
# truncate清空表,两种写法,truncate后边的table可以不加
mysql> truncate t1;
Query OK, 0 rows affected (0.01 sec)
mysql> truncate table t1;
Query OK, 0 rows affected (0.03 sec)
# 查看表t1,已经清空
mysql> select * from t1;
Empty set (0.00 sec)
# 再创建一个t2表,表中有自增字段
mysql> create table t2(id int primary key auto_increment,address char(10));
Query OK, 0 rows affected (0.02 sec)
# 向表中插入数据
mysql> insert into t2(address) values('杭州'),('深圳');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t2;
+----+---------+
| id | address |
+----+---------+
| 1 | 杭州 |
| 2 | 深圳 |
+----+---------+
2 rows in set (0.00 sec)
# truncate清空表
mysql> truncate t2;
Query OK, 0 rows affected (0.02 sec)
# 向表中插入数据
mysql> insert into t2(address) values('杭州'),('深圳');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
# truncate清空表的方式会将表中的自增字段同时删除,而delete from 表名的方式不可以删除自增
mysql> select * from t2;
+----+---------+
| id | address |
+----+---------+
| 1 | 杭州 |
| 2 | 深圳 |
+----+---------+
2 rows in set (0.00 sec)