[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 增加列
说
修改列中不支持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)