MySQL基本语句

迁移到 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 直接创建表
mysql> create table user(
    -> id int auto_increment primary key,
    -> name varchar(16) not null,
    -> age int not null,
    -> birthday datetime
    -> );
-----
# create 利用已有表创建
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 设置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)

数据操作

增删改查数据:

# 增
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)

视图

视图是从一个或多个表导出的虚拟表

# 创建视图
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)
# 创建或替换视图
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)
# 插入数据
# 操作视图即操作数据表
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)

用户管理

# 创建用户
# 语法
# create user 'username'@'host' identified by 'password'
# host用'%' 则代表可以从任意远程登录

mysql> create user 'samp'@'localhost' identified by 'samp';
Query OK, 0 rows affected (0.10 sec)
# 授权
# 语法
# grant privileges on database.table to 'username'@'host'
# privileges - 用户的操作权限(select, update等 all所有权限)
# database.table 可使用*设置所有,如samp_db.*  *.*

mysql> grant select on samp_db.* to samp@localhost;
Query OK, 0 rows affected (0.11 sec)

# 使用samp登录测试权限
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'

# 授权所有
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

# 用以上命令授权的用户无法给其他用户授权
# 若要带上授权权限,则使用命令
# grant privileges on database.table to 'username'@'host' with grant option;
# 密码设置/更改
# 1.非要设置/修改的用户登录
# SET PASSWORD FOR 'username'@'host' = PASSWORD('password');
# 2 当前登录用户
# 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)
# 销毁用户权限
revoke privilege on database.table from 'username'@'host';
# 删除用户
drop user username@host

表的复制/备份/还原

# 复制表结构
# 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.只有表结构,没有主键等信息
> 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;  # 不会复制数据
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)
# 表数据复制
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)
# 查看表的创建语句
> 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)
# 备份数据库
$ sudo mysqldump -u root -p samp_db > /data/backups/samp.sql
# 还原数据库
> 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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容