《深入浅出MySQL》之SQL基础

SQL是Structure Query language(结构化查询语言)的缩写,它是使用关系模型的数据库应用语言。在众多开源数据库中,MySQL正是其中最杰出的代表,MySQL是由三个瑞典人于20世纪90年代开发的一个关系型数据库。并用了创始人之一Michael Widenius女儿的名字My命名,这就是MySQL的由来,本次博客使用的是开源数据库MySQL,版本5.7.19,下面就开始吧!

SQL分类

1.数据定义语句(Data Definition Language,DDL):主要是用来定义数据库、表、列等对象;

2.数据操作语句(Data Manipulation language,DML):用来添加、更新、删除和查询数据库记录,并检查数据完整性;

3.数据控制语句(Data Control language,DCL):定义了数据库、表、用户的访问权限和安全级别等;

DDL语句

1.创建数据库

语法:create database dbname; 

举例:

mysql> create database test;
Query OK, 1 row affected (0.01 sec)

查看当前系统中有哪些数据库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

选择要操作的数据库:

mysql> use test
Database changed

查看数据库中所创建的表:

mysql> show tables;
Empty set (0.00 sec)
mysql> use mysql
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| dept                      |
| emp                       |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
33 rows in set (0.00 sec)

2.删除数据库

语法:drop database dbname;

举例:

mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)

注意:删除数据库后,数据库下面的所有的的表就被清空了,再删除之前记得备份有用的数据。

3.创建表

create table tablename(
column_name_1 column_type_1 constraints,
column_name_2 column_type_2 constraints,
...
column_name_2 column_type_2 constraints,
);

举例:

mysql> create table emp(name varchar(10),hiredate date,sal decimal(10,2),dept int(2));
Query OK, 0 rows affected (0.04 sec)

查看表:

语法:desc tablename

举例:

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(10)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dept     | int(2)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

查看表的详细信息:

mysql> show create table emp \G;
*************************** 1. row ***************************
       Table: emp
Create Table: CREATE TABLE `emp` (
  `name` varchar(10) DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` decimal(10,2) DEFAULT NULL,
  `dept` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

4.删除表

语法:drop table tablename

举例:

mysql> drop table emp;
Query OK, 0 rows affected (0.01 sec)

5.修改表

(1)修改表类型:

语法:alter table tablename modify column_name column_type_new

举例:

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(10)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dep      | int(2)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table emp modify name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> alter table emp modify name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dep      | int(2)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

(2)增加表字段:

语法:alter table tablename add column column_name column_type;

举例:

mysql> alter table emp add column age int(3);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dep      | int(2)        | YES  |     | NULL    |       |
| age      | int(3)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

(3)删除表字段:

alter table tablename drop column column_name

举例:

mysql> alter table emp drop column age;
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dep      | int(2)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

(4)字段改名

语法:alter table tablename change old_column_name new_column_name column_type

举例:

mysql> alter table emp change dep dept int(3);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dept     | int(3)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

change和midify都可以修改表,change可以修改列的名称和重新定义列的类型,modify却不能修改列的名称。

(5)修改字段排列的顺序

语法:alter table tablename change/add/modify column_name column_type first/after column_name

举例:

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dept     | int(3)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql> alter table emp add column age int(2) after name;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| age      | int(2)        | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dept     | int(3)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

(6)修改表名

语法:alter table tablename rename new_tablename

举例:

mysql> alter table emp rename emp1;
Query OK, 0 rows affected (0.01 sec)
mysql> desc emp1;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| age      | int(2)        | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dept     | int(3)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

DML语句

DML是对数据库中的表的操作,是开发人员最长使用的。

1.插入记录

语法:insert into tablename(column_name1,column_name2,...column_namen) values(value1,value2,...valuen);

举例:

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | YES  |     | NULL    |       |
| age      | int(2)        | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| dept     | int(3)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into emp(name,age,hiredate,sal,dept) values(‘frank‘,22,‘2017-09-15‘,‘10000‘,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp  values(‘rose‘,21,‘2017-09-15‘,‘10000‘,1);    #也可以不指定字段名称,但是后面的顺序必须和字段保持一致
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp  values(‘jeff‘,23,‘2017-09-15‘,‘10000‘,2),(‘mei‘,21,‘2017-09-15‘,‘8000‘,3); #可以同时插入多条记录
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> insert into emp(name,sal) values(‘lisa‘,‘1000‘);   #没写的字段默认为NULL
Query OK, 1 row affected (0.00 sec)

查看表emp:

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 | 10000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 10000.00 |    2 |
| mei   |   21 | 2017-09-15 |  8000.00 |    3 |
| lisa  | NULL | NULL       |  1000.00 | NULL |
+-------+------+------------+----------+------+
5 rows in set (0.00 sec)

2.更新记录

(1)更新单个表

语法:update tablename set column_name1=value1,column_name2=value2,...column_namen=valuen  [where condition]

举例:

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 | 10000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 10000.00 |    2 |
| mei   |   21 | 2017-09-15 |  8000.00 |    3 |
+-------+------+------------+----------+------+
4 rows in set (0.00 sec)
mysql> update emp set sal=‘4000‘ where name=‘rose‘;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 |  4000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 10000.00 |    2 |
| mei   |   21 | 2017-09-15 |  8000.00 |    3 |
+-------+------+------------+----------+------+
4 rows in set (0.00 sec)

在MySQL中update可以同时更新多个表中数据:

语法:update tablename1 alias1 ,table2 alias2 set  update_condition where condition

举例:

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 |  4000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 10000.00 |    2 |
| mei   |   21 | 2017-09-15 |  8000.00 |    3 |
+-------+------+------------+----------+------+
4 rows in set (0.00 sec)

mysql> select * from dep;
+-------+---------+
| depno | depname |
+-------+---------+
|     1 | tecg    |
|     2 | sale    |
|     3 | fin     |
|     4 | IT      |
+-------+---------+
4 rows in set (0.00 sec)

mysql> update emp a,dep b set a.sal=a.sal*b.depno where a.dept=b.depno;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 4  Changed: 2  Warnings: 0

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 |  4000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 20000.00 |    2 |
| mei   |   21 | 2017-09-15 | 24000.00 |    3 |
+-------+------+------------+----------+------+
4 rows in set (0.00 sec)

3.删除记录

语法:delete from tablename [where condition];

举例:

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 |  4000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 20000.00 |    2 |
| mei   |   21 | 2017-09-15 | 24000.00 |    3 |
+-------+------+------------+----------+------+
4 rows in set (0.00 sec)

mysql> delete from emp where name=‘mei‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 |  4000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 20000.00 |    2 |
+-------+------+------------+----------+------+
3 rows in set (0.00 sec)

也可以同时删除多个表的记录:

语法:delete alias1,alias2 from tablename1 alias1,tablename2 alias2 where condition;

举例:

mysql> select * from emp;
+-------+------+------------+----------+------+
| name  | age  | hiredate   | sal      | dept |
+-------+------+------------+----------+------+
| frank |   22 | 2017-09-15 | 10000.00 |    1 |
| rose  |   21 | 2017-09-15 |  4000.00 |    1 |
| jeff  |   23 | 2017-09-15 | 20000.00 |    2 |
+-------+------+------------+----------+------+
3 rows in set (0.00 sec)
mysql> select * from dep;
+-------+---------+
| depno | depname |
+-------+---------+
|     1 | tecg    |
|     2 | sale    |
|     3 | fin     |
|     4 | IT      |
+-------+---------+
4 rows in set (0.00 sec)

mysql> delete a,b from emp a,dep b where a.dept=b.depno;
Query OK, 5 rows affected (0.00 sec)

mysql> select * from dep;
+-------+---------+
| depno | depname |
+-------+---------+
|     3 | fin     |
|     4 | IT      |
+-------+---------+
2 rows in set (0.00 sec)

mysql> select * from emp;
Empty set (0.00 sec)

4.查询记录

语法:select * from tablename [where condition];  #*表示把所有的记录都拿出来

举例:

mysql> select * from emp;
+-------+------+------------+---------+------+
| name  | age  | hiredate   | sal     | dept |
+-------+------+------------+---------+------+
| rose  |   22 | 2017-09-12 | 1000.00 |    1 |
| jeff  |   23 | 2017-09-12 | 2000.00 |    1 |
| frank |   23 | 2017-09-15 | 2000.00 |    2 |
| bob   |   24 | 2017-02-15 | 3000.00 |    3 |
+-------+------+------------+---------+------+
4 rows in set (0.00 sec)

(1)查询指定字段:

mysql> select name,age from emp;
+-------+------+
| name  | age  |
+-------+------+
| rose  |   22 |
| jeff  |   23 |
| frank |   23 |
| bob   |   24 |
+-------+------+
4 rows in set (0.00 sec)

(2)查询不重复的记录:

添加关键字distinct

mysql> select age from emp;
+------+
| age  |
+------+
|   22 |
|   23 |
|   23 |
|   24 |
+------+
4 rows in set (0.00 sec)

mysql> select distinct age from emp;
+------+
| age  |
+------+
|   22 |
|   23 |
|   24 |
+------+
3 rows in set (0.00 sec)

(2)条件查询

mysql> select name,age from emp where age > 23;
+------+------+
| name | age  |
+------+------+
| bob  |   24 |
+------+------+
1 row in set (0.00 sec)

(4)排序和限制

desc和asc是排序的关键字,desc表示按照字段继续降序排序,asc则表示按照字段升序排序,默认是升序。order by后面可以跟着对个排序字段。

举例:

mysql> select * from emp order by sal;   #默认是升序
+-------+------+------------+---------+------+
| name  | age  | hiredate   | sal     | dept |
+-------+------+------------+---------+------+
| rose  |   22 | 2017-09-12 | 1000.00 |    1 |
| jeff  |   23 | 2017-09-12 | 2000.00 |    1 |
| frank |   23 | 2017-09-15 | 2000.00 |    2 |
| bob   |   24 | 2017-02-15 | 3000.00 |    3 |
+-------+------+------------+---------+------+
4 rows in set (0.00 sec)
mysql> select * from emp order by sal desc;  #使用降序
+-------+------+------------+---------+------+
| name  | age  | hiredate   | sal     | dept |
+-------+------+------------+---------+------+
| bob   |   24 | 2017-02-15 | 3000.00 |    3 |
| jeff  |   23 | 2017-09-12 | 2000.00 |    1 |
| frank |   23 | 2017-09-15 | 2000.00 |    2 |
| rose  |   22 | 2017-09-12 | 1000.00 |    1 |
+-------+------+------------+---------+------+
4 rows in set (0.00 sec)

如果对排序之后只想显示其中的一部分,可以使用limit关键字,order by和llimit经常一起配合使用来进行记录的分页显示。

mysql> select * from emp order by sal desc limit 2;
+------+------+------------+---------+------+
| name | age  | hiredate   | sal     | dept |
+------+------+------------+---------+------+
| bob  |   24 | 2017-02-15 | 3000.00 |    3 |
| jeff |   23 | 2017-09-12 | 2000.00 |    1 |
+------+------+------------+---------+------+
2 rows in set (0.00 sec)

(5)聚合

语法:select [column_name1,colun_name2,...] fun_name from tablename [where condition] [group by column_name1,colun_name2,...[with rollup] [having where condition]]
fun_name:表示聚合操作,也就是聚合函数,常用的有sum(求和),count(计数),max(最大值),min(最小值)
group by:要进行分类聚合的字段
with rollup:表明是否对分类聚合后的结果进行再汇总
having:表示对分类后的结果再进行过滤

举例:

mysql> select count(1) from emp;
+----------+
| count(1) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

mysql> select dept,count(1) from emp group by dept;
+------+----------+
| dept | count(1) |
+------+----------+
|    1 |        2 |
|    2 |        1 |
|    3 |        1 |
+------+----------+
3 rows in set (0.00 sec)
mysql> select dept,count(1) from emp group by dept having count(1) > 1;
+------+----------+
| dept | count(1) |
+------+----------+
|    1 |        2 |
+------+----------+
1 row in set (0.00 sec)
mysql> select dept,count(1) from emp group by dept with rollup;
+------+----------+
| dept | count(1) |
+------+----------+
|    1 |        2 |
|    2 |        1 |
|    3 |        1 |
| NULL |        4 |
+------+----------+
4 rows in set (0.00 sec)
mysql> select sum(sal),max(sal),min(sal) from emp;
+----------+----------+----------+
| sum(sal) | max(sal) | min(sal) |
+----------+----------+----------+
|  8000.00 |  3000.00 |  1000.00 |
+----------+----------+----------+
1 row in set (0.00 sec)

(6)表连接

内连接:选出两张表中互相匹配的记录

mysql> select name,dept from emp,dep where emp.dept=dep.depno;
+------+------+
| name | dept |
+------+------+
| bob  |    3 |
+------+------+
1 row in set (0.00 sec)

外连接:左连接和右连接,外连接会选出其他不匹配的记录。

左连接:

mysql> select name,depname from emp left join dep on emp.dept=dep.depno;
+-------+---------+
| name  | depname |
+-------+---------+
| bob   | fin     |
| rose  | NULL    |
| jeff  | NULL    |
| frank | NULL    |
+-------+---------+
4 rows in set (0.00 sec)

右连接:

mysql> select name,depname from dep right join emp on emp.dept=dep.depno;
+-------+---------+
| name  | depname |
+-------+---------+
| bob   | fin     |
| rose  | NULL    |
| jeff  | NULL    |
| frank | NULL    |
+-------+---------+
4 rows in set (0.00 sec)

(7)子查询

子查询的关键字:in、not in、=、!=、exists、not exists;

mysql> select * from emp where dept in (select depno from dep);
+------+------+------------+---------+------+
| name | age  | hiredate   | sal     | dept |
+------+------+------------+---------+------+
| bob  |   24 | 2017-02-15 | 3000.00 |    3 |
+------+------+------------+---------+------+
1 row in set (0.00 sec)

(8)联合

关键字:union和union all,union去会对结果去重。

举例:

mysql> select * from emp;
+-------+------+------------+---------+------+
| name  | age  | hiredate   | sal     | dept |
+-------+------+------------+---------+------+
| rose  |   22 | 2017-09-12 | 1000.00 |    1 |
| jeff  |   23 | 2017-09-12 | 2000.00 |    1 |
| frank |   23 | 2017-09-15 | 2000.00 |    2 |
| bob   |   24 | 2017-02-15 | 3000.00 |    3 |
+-------+------+------------+---------+------+
4 rows in set (0.00 sec)

mysql> select * from dep;
+-------+---------+
| depno | depname |
+-------+---------+
|     3 | fin     |
|     4 | IT      |
+-------+---------+
2 rows in set (0.00 sec)

mysql> select dept from emp
    -> union all
    -> select depno from dep;
+------+
| dept |
+------+
|    1 |
|    1 |
|    2 |
|    3 |
|    3 |
|    4 |
+------+
6 rows in set (0.00 sec)
mysql> select dept from emp
    -> union
    -> select depno from dep;
+------+
| dept |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

DCL语句

DCL语句一般是数据库管理员用来对对象进行授权使用的,一般开发人员是很少用的,这里先做简单的介绍,后面的博客会更新相关内容:

创建用户:

mysql> create user ‘frank‘@‘%‘ identified by ‘123‘;
Query OK, 0 rows affected (0.00 sec)

授权:

mysql> grant all privileges on test.* to ‘frank‘@‘%‘ identified by ‘123‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

退出root登录frank:

C:\Windows\system32>mysql -ufrank -p
Enter password: ***
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

mysql>

帮助使用

查看所有可供查询的分类:

mysql> ? contents
You asked for help about help category: "Contents"
For more information, type ‘help <item>‘, where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Procedures
   Storage Engines
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility
mysql> ? data types   #进一步查看
You asked for help about help category: "Data Types"
For more information, type ‘help <item>‘, where <item> is one of the following
topics:
   AUTO_INCREMENT
   BIGINT
   BINARY
   BIT
   BLOB
   BLOB DATA TYPE
   BOOLEAN
   CHAR
   CHAR BYTE
   DATE
   DATETIME
   DEC
   DECIMAL
   DOUBLE
   DOUBLE PRECISION
   ENUM
   FLOAT
   INT
   INTEGER
   LONGBLOB
   LONGTEXT
   MEDIUMBLOB
   MEDIUMINT
   MEDIUMTEXT
   SET DATA TYPE
   SMALLINT
   TEXT
   TIME
   TIMESTAMP
   TINYBLOB
   TINYINT
   TINYTEXT
   VARBINARY
   VARCHAR
   YEAR DATA TYPE

快速查找:

举例:查找create table怎么使用?

mysql> ? create table
Name: ‘CREATE TABLE‘
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]
    [partition_options]

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    [(create_definition,...)]
    [table_options]
    [partition_options]
    [IGNORE | REPLACE]
    [AS] query_expression

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    { LIKE old_tbl_name | (LIKE old_tbl_name) }
......(略)

参考书籍 《深入浅出MySQL》 写的很不错,推荐!

时间: 2024-10-15 09:25:58

《深入浅出MySQL》之SQL基础的相关文章

MySQL之SQL基础

SQL是Structure Query Language(结构化查询语言)的缩写,它是关系型数据库的应用语言,由IBM在20世纪70年代开发,以实现关系型数据库中的信息检索. 在20世纪80年代初,美国国家标准局(ANSI)开始着手制定SQL标准,最早的ANSI标准于1986年完成,就被叫做SQL-86.正是由于SQL语言的标准化,所以大多数关系型数据库都支持SQL语言,它已经发展成为多种平台进行交互操作的底层会话语言. SQL的分类: DDL:数据定义语言,即是对数据库内部对象进行创建.删除.

MySQL教程 SQL基础查询

其实在数据库最经常用的当属查询操作 基本语法 SELECT [ALL | DISTINCT | DISTINCTROW ] 字段列表 AS 字段别名 [FROM 表名 WHERE 条件表示式 GROUP BY 字段名|表达式 [ASC | DESC], ... [WITH ROLLUP]] [HAVING 条件表达式] [ORDER BY 字段名|表达式 [ASC | DESC] , ...] [LIMIT {[offset,] row_count | row_count OFFSET offs

(2.7)Mysql之SQL基础——表的操作与查看

搜索关键字:mysql表操作 1.创建表(在innodb下) 1.1.create table table_name(column type) ENGINE=innodb default CHARSET=UTF8  COLLATE=utf8_general_ci COMMENT='this is a test~';  --常规默认方式 create table table_name(column type)  选项 1.2.create table table_name like old_tab

(2.8)Mysql之SQL基础——索引的分类与使用

按逻辑分类: 1.主键索引(聚集索引)(也是唯一索引,不允许有空值) 2.普通索引或单列索引 3.多列索引(复合索引) 4.唯一索引或非唯一索引(非唯一索引其实就是普通/多列索引) 5.空间索引 6.创建索引的基本形式 7.索引的操作 1.查看索引2.创建单列索引3.复合索引4.唯一索引(允许多个空值,每列唯一)5.主键索引(不允许空值,唯一)6.索引的删除7.删除自增auto_increment   6.创建索引的基本形式 create [unique|fulltext|spatial] in

《深入浅出MySQL数据库开发、优化于管理维护》基础篇学习笔记

MySQL数据库物理文件默认存放位置:C:\ProgramData\MySQL\MySQL Server 5.5\data\MySQL通过配置my.int的datadir属性来指定数据库的物理存放位置. 一.DDL语句:1.创建数据库:create database test; 2.删除数据库:drop database test; 3.描述表:desc emp; 4.删除表:drop table emp; 5.修改表:(1)修改表类型:alter table emp modify ename

转载 深入浅出mysql事务处理和锁机制

1. 事务处理和并发性 1.1. 基础知识和相关概念 1 )全部的表类型都可以使用锁,但是只有 InnoDB 和 BDB 才有内置的事务功能. 2 )使用 begin 开始事务,使用 commit 结束事务,中间可以使用 rollback 回滚事务. 3 )在默认情况下, InnoDB 表支持一致读. SQL 标准中定义了 4 个隔离级别: read uncommited , read commited , repeatable read , serializable . read uncomm

[SQL] SQL 基础知识梳理(一)- 数据库与 SQL

SQL 基础知识梳理(一)- 数据库与 SQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902856.html 序 目录 What's 数据库 数据库结构 SQL 概要 创建表 删除和更新表 1-1 What's 数据库 1.数据库(Database,DB):将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合.如:大型-银行存储的信息,小型-电话簿. 2.数据库管理系统(Batabase Management Syste

sql基础之DDL(Data Definition Languages)

好久没写SQL语句了,复习一下. DDL数据定义语言,DDL定义了不同的数据段.数据库.表.列.索引等数据库对象的定义.常用的DDL语句包括create.drop.alter等等. 登录数据:mysql -uroot -p sql 命令一般以英文分号或者\g结束,注意这个\不是/. 1.查看数据库 show databases;注意这个后面的s. 2.创建数据库create database wangking;注意这里没有s. 3.选择数据库use siyecao;列出数据库中的表show ta

【深入浅出Mysql】MySql存储引擎之MyISAM(二)

一.MyISAM 1.1 数据存储 MyISAM是默认的MySQL插件式存储引擎.它不支持事务和外键.优势:访问速度快,基本上以SELECT.INSERT为主的应用基本上都可以使用这个引擎来创建表.每个MyISAM在磁盘上存储称3个文件,其文件名和表名相同,扩展名不同,分别是: .frm(存储表自定义) .MYD(MYDATA,存储数据) .MYI(MYIndex,存储索引) MyISAM的数据文件和索引文件可以放置在不同的目录,平均分布IO,获得更快的速度.其中索引文件需要在创建表的时候通过D