遷移到 https://github.com/lianginet/notes
連接
# 語法 mysql -h 127.0.0.1 -u root -p
$ sudo mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 40
Server version: 10.0.25-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> # mariadb用法如mysql
database管理
# create
mysql> create database samp_db default
->character set utf8
->collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
# show
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| samp_db |
+--------------------+
# use
mysql> use samp_db;
Database changed
# select database()
mysql> select database();
+------------+
| database() |
+------------+
| samp_db |
+------------+
# drop
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> drop database test1;
Query OK, 0 rows affected (0.00 sec)
table管理
均在sam_db下操作
# create
# create 直接創(chuàng)建表
mysql> create table user(
-> id int auto_increment primary key,
-> name varchar(16) not null,
-> age int not null,
-> birthday datetime
-> );
-----
# create 利用已有表創(chuàng)建
mysql> create table new_user select * from user;
Query OK, 0 rows affected (0.92 sec)
Records: 0 Duplicates: 0 Warnings: 0
# show
mysql> show tables;
+-------------------+
| Tables_in_samp_db |
+-------------------+
| new_user |
| user |
+-------------------+
2 rows in set (0.00 sec)
# desc
mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| birthday | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.06 sec)
# alter
# alter修改表的編碼
mysql> alter table user convert to character set utf8;
Query OK, 0 rows affected (0.82 sec)
Records: 0 Duplicates: 0 Warnings: 0
-----
# alter 添加列
mysql> alter table user add integral int;
Query OK, 0 rows affected (0.54 sec)
Records: 0 Duplicates: 0 Warnings: 0
-----
# alter 修改字段
mysql> alter table user modify integral varchar(12); # 修改類型
Query OK, 0 rows affected (0.88 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user change integral level tinyint(1); # 修改名稱
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
-----
# alter 刪除字段
mysql> alter table user drop column level;
Query OK, 0 rows affected (0.57 sec)
Records: 0 Duplicates: 0 Warnings: 0
-----
# alter 設(shè)置null/not null
mysql> alter table user modify age int(3) null;
Query OK, 0 rows affected (0.59 sec)
Records: 0 Duplicates: 0 Warnings: 0
# rename
mysql> rename table user to users;
Query OK, 0 rows affected (0.47 sec)
數(shù)據(jù)操作
增刪改查數(shù)據(jù):
# 增
mysql> insert into user values (null, 'tom', 23, '1994-10-24');
Query OK, 1 row affected (0.14 sec)
mysql> insert into user values (null, 'sam', 24, '1993-10-24');
Query OK, 1 row affected (0.07 sec)
mysql> select * from user;
+----+------+------+---------------------+
| id | name | age | birthday |
+----+------+------+---------------------+
| 1 | tom | 23 | 1994-10-24 00:00:00 |
| 2 | sam | 24 | 1993-10-24 00:00:00 |
+----+------+------+---------------------+
2 rows in set (0.00 sec)
# 刪
mysql> delete from user where name='tom';
Query OK, 1 row affected (0.09 sec)
mysql> select * from user;
+----+------+------+---------------------+
| id | name | age | birthday |
+----+------+------+---------------------+
| 2 | sam | 24 | 1993-10-24 00:00:00 |
+----+------+------+---------------------+
1 row in set (0.00 sec)
# 改
mysql> update user set age=23 where name='sam';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user;
+----+------+------+---------------------+
| id | name | age | birthday |
+----+------+------+---------------------+
| 2 | sam | 23 | 1993-10-24 00:00:00 |
+----+------+------+---------------------+
1 row in set (0.00 sec)
# 查
mysql> select name,age from user;
+------+------+
| name | age |
+------+------+
| sam | 23 |
+------+------+
1 row in set (0.00 sec)
視圖
視圖是從一個或多個表導(dǎo)出的虛擬表
# 創(chuàng)建視圖
mysql> create view user_view (
-> name,age
-> ) as select name,age from user;
Query OK, 0 rows affected (0.10 sec)
mysql> select * from user_view;
+------+------+
| name | age |
+------+------+
| sam | 23 |
+------+------+
1 row in set (0.00 sec)
# 創(chuàng)建或替換視圖
mysql> create or replace view user_view (
-> user_id,
-> user_name,
-> user_age
-> ) as select id, name, age from user;
mysql> show tables;
+-------------------+
| Tables_in_samp_db |
+-------------------+
| new_user |
| user |
| user_view |
+-------------------+
3 rows in set (0.00 sec)
mysql> select * from user_view;
+---------+-----------+----------+
| user_id | user_name | user_age |
+---------+-----------+----------+
| 2 | sam | 23 |
+---------+-----------+----------+
1 row in set (0.00 sec)
# 插入數(shù)據(jù)
# 操作視圖即操作數(shù)據(jù)表
mysql> insert into user_view values (null, 'fmt', 22);
Query OK, 1 row affected (0.05 sec)
mysql> select * from user_view;
+---------+-----------+----------+
| user_id | user_name | user_age |
+---------+-----------+----------+
| 2 | sam | 23 |
| 3 | fmt | 22 |
+---------+-----------+----------+
2 rows in set (0.00 sec)
mysql> select * from user;
+----+------+------+---------------------+
| id | name | age | birthday |
+----+------+------+---------------------+
| 2 | sam | 23 | 1993-10-24 00:00:00 |
| 3 | fmt | 22 | NULL |
+----+------+------+---------------------+
2 rows in set (0.00 sec)
# 刪除視圖
mysql> drop view user_view;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_samp_db |
+-------------------+
| new_user |
| user |
+-------------------+
2 rows in set (0.00 sec)
用戶管理
# 創(chuàng)建用戶
# 語法
# create user 'username'@'host' identified by 'password'
# host用'%' 則代表可以從任意遠(yuǎn)程登錄
mysql> create user 'samp'@'localhost' identified by 'samp';
Query OK, 0 rows affected (0.10 sec)
# 授權(quán)
# 語法
# grant privileges on database.table to 'username'@'host'
# privileges - 用戶的操作權(quán)限(select, update等 all所有權(quán)限)
# database.table 可使用*設(shè)置所有穿剖,如samp_db.* *.*
mysql> grant select on samp_db.* to samp@localhost;
Query OK, 0 rows affected (0.11 sec)
# 使用samp登錄測試權(quán)限
mysql> use samp_db;
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> update user set birthday='1991-10-24' where name='fmt';
ERROR 1142 (42000): UPDATE command denied to user 'samp'@'localhost' for table 'user'
# 授權(quán)所有
mysql> grant all on samp_db.* to samp@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> use samp_db;
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> update user set birthday='1991-10-24' where name='fmt';
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 用以上命令授權(quán)的用戶無法給其他用戶授權(quán)
# 若要帶上授權(quán)權(quán)限携御,則使用命令
# grant privileges on database.table to 'username'@'host' with grant option;
# 密碼設(shè)置/更改
# 1.非要設(shè)置/修改的用戶登錄
# SET PASSWORD FOR 'username'@'host' = PASSWORD('password');
# 2 當(dāng)前登錄用戶
# set password = password('password');
# samp用戶登錄
mysql> set password = password('samp_db');
Query OK, 0 rows affected (0.00 sec)
# root登錄
mysql> set password for samp@localhost = password('samp');
Query OK, 0 rows affected (0.00 sec)
# 銷毀用戶權(quán)限
revoke privilege on database.table from 'username'@'host';
# 刪除用戶
drop user username@host
表的復(fù)制/備份/還原
# 復(fù)制表結(jié)構(gòu)
# 1.含主鍵等信息的完整表
mysql> create table new_user like user;
Query OK, 0 rows affected (0.45 sec)
mysql> show tables;
+-------------------+
| Tables_in_samp_db |
+-------------------+
| new_user |
| user |
+-------------------+
2 rows in set (0.00 sec)
mysql> desc new_user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
| birthday | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
# 2.只有表結(jié)構(gòu)啄刹,沒有主鍵等信息
> create table new_user1 select * from user;
# 或 create table new_user1 as (select * from user);
Query OK, 2 rows affected (0.90 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> create table new_user2 select * from user where 1=2; # 不會復(fù)制數(shù)據(jù)
Query OK, 0 rows affected (0.60 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show tables;
+-------------------+
| Tables_in_samp_db |
+-------------------+
| new_user |
| new_user1 |
| new_user2 |
| user |
+-------------------+
4 rows in set (0.00 sec)
mysql> desc new_user1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| name | varchar(16) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
| birthday | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc new_user2;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| name | varchar(16) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
| birthday | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
# 表數(shù)據(jù)復(fù)制
mysql> select * from new_user;
Empty set (0.00 sec)
mysql> insert into new_user select * from user;
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from new_user;
+----+------+------+---------------------+
| id | name | age | birthday |
+----+------+------+---------------------+
| 2 | sam | 23 | 1993-10-24 00:00:00 |
| 3 | fmt | 22 | 1991-10-24 00:00:00 |
+----+------+------+---------------------+
2 rows in set (0.00 sec)
# 查看表的創(chuàng)建語句
> show create table user;
+-------+-------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------+
| user | CREATE TABLE `user` ( |
| | `id` int(11) NOT NULL AUTO_INCREMENT, |
| | `name` varchar(16) NOT NULL, |
| | `age` int(3) DEFAULT NULL, |
| | `birthday` datetime DEFAULT NULL, |
| | PRIMARY KEY (`id`) |
| | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+----- -------------------------------------------------+
1 row in set (0.00 sec)
# 清空表
mysql> truncate new_user1;
Query OK, 0 rows affected (0.37 sec)
mysql> select * from new_user1;
Empty set (0.00 sec)
# 備份數(shù)據(jù)庫
$ sudo mysqldump -u root -p samp_db > /data/backups/samp.sql
# 還原數(shù)據(jù)庫
> create samp_db1;
MariaDB [(none)]> create database samp_db1;
Query OK, 1 row affected (0.00 sec)
MariaDB [samp_db1]> use samp_db1
Database changed
MariaDB [samp_db1]> source /data/backups/samp.sql
Query OK, 0 rows affected (0.00 sec)
......
Query OK, 0 rows affected (0.00 sec)
MariaDB [samp_db1]> show tables;
+--------------------+
| Tables_in_samp_db1 |
+--------------------+
| new_user |
| new_user1 |
| new_user2 |
| user |
+--------------------+
4 rows in set (0.00 sec)