MySQL的执行顺序
from - on - join - where - group by - having - select - distinct - union - order by
数据库操作
要对数据库进行操作,首先需要进入数据库控制台。在终端中数据mysql -u root -p 回车(需安装数据库),然后输入密码进入数据库控制台(没有密码会直接进入)。
android@c1624:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.62-0ubuntu0.14.04.1 (Ubuntu)
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>
创建数据库
使用create语句创建数据库:create database 数据库名
;
mysql> create database mysqldemo;
Query OK, 1 row affected (0.03 sec)
删除数据库
在删除数据库过程中,务必要十分谨慎,因为在执行删除命令后,所有数据将会消失。
语法:drop database 数据库名
;
mysql> drop database mysqldemo;
Query OK, 0 rows affected (0.21 sec)
查看数据库
可以通过 show databases; 查看已存在的数据库。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| mysqldemo |
+--------------------+
4 rows in set (0.06 sec)
使用数据库
想要操作某个数据库(如创建表等),都需要先使用该数据库。use database 数据库名
;
mysql> use mysqldemo;
Database changed
导出数据库
导出数据库的结构和数据到指定文件中。mysqldump -u root -p 数据库名
> 文件名
android@c1624:~$ mysqldump -u root -p mysqldemo > mysqldemo.sql
Enter password:
导出数据库的结构到指定文件中。mysqldump -u root -p -d 数据库名
> 文件名
android@c1624:~$ mysqldump -u root -p -d mysqldemo > mysqldemo.sql
Enter password:
- -d 结构(–no-data:不导出任何数据,只导出数据库表结构)
- -t 数据(–no-create-info:只导出数据,而不添加CREATE TABLE 语句)
- -n (–no-create-db:只导出数据,而不添加CREATE DATABASE 语句)
- -R (–routines:导出存储过程以及自定义函数)
- -E (–events:导出事件)
- –triggers (默认导出触发器,使用–skip-triggers屏蔽导出)
- -B (–databases:导出数据库列表,单个库时可省略)
导入数据库
导入数据库需要进入到mysql终端中。使用数据库,执行source命令。source 文件名
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> source mysqldemo.sql
Query OK, 0 rows affected (0.01 sec)
表结构操作
表操作需要先选择数据库之后才能进行。
创建表
创建MySQL数据表需要以下信息:
- 表名
- 表字段名
- 定义每个表字段
建表语法:CREATE TABLE table_name (column_name column_type);
mysql> create table user(
-> id int primary key auto_increment,
-> name varchar(10) not null);
Query OK, 0 rows affected (0.13 sec)
primary key表示该字段为主键,auto_increment表示自增长(如插入数据时没有插入该字段数据,则该字段值会自动增长)。
修改表
增加字段
增加一个字段:
ALTER TABLE [表名] ADD [字段名] [字段类型] [是否为空];
mysql> alter table user add sex char(1) not null;
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改字段
修改某个表的字段名称及指定为空或非空:
ALTER TABLE [表名] change [原字段名] [新字段名] [字段类型] [是否为空];
mysql> alter table user change sex age int not null;
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
ALTER TABLE [表名] modify [字段名] [字段类型] [是否为空];
mysql> alter table user modify name varchar(20) not null;
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
在指定的字段后面增加一个字段:
ALTER table [表名] ADD [新字段名] [字段类型] [是否为空] [注释] after [已存在的字段名];
mysql> alter table user add sex char(1) not null comment "性别" after name;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看表结构
可以通过desc查看表结构:desc [表名];
mysql> desc user;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | char(1) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)
删除表
MySQL中删除数据表是非常容易操作的, 但是你再进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失。
语法:DROP TABLE [表名] ;
mysql> drop table user;
Query OK, 0 rows affected (0.07 sec)
表操作
插入数据
MySQL 表中使用 INSERT INTO SQL语句来插入数据。语法:
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
mysql> insert into user(id,name,sex,age) values (1,"hu",'F',21);
Query OK, 1 row affected, 1 warning (0.07 sec)
如果数据是字符型,必须使用单引号或者双引号,如:”value”。
也可使用,
将多条数据隔开,进行多条插入。
mysql> insert into user(id,name,sex,age) values (1,"hu",'F',21),(2,"li",'F',22),(3,"wang","F",23);
Query OK, 3 rows affected, 3 warnings (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 3
修改数据
MySQL 表中使用 UPDATE SQL语句来插入数据。语法:
UPDATE table_name SET field1 = value1,field2 = value2 ... [WHERE Clause]
mysql> update user set age = 23,sex = "M" where id = 2;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
- 你可以同时更新一个或多个字段。
- 你可以在 WHERE 子句中指定任何条件。
- 你可以在一个单独表中同时更新数据。
若不指定条件 where
则表示修改表中的所有记录。
删除数据
MySQL 表中使用 DELETE SQL语句来插入数据。语法:
DELETE FROM table_name [WHERE Clause];
mysql> delete from user where id = 3;
Query OK, 1 row affected (0.06 sec)
- 如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
- 你可以在 WHERE 子句中指定任何条件
- 您可以在单个表中一次性删除记录。
查询数据
MySQL 数据库使用SQL SELECT语句来查询数据。基本语法:
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
- 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
- SELECT 命令可以读取一条或者多条记录。
- 你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
- 你可以使用 WHERE 语句来包含任何条件。
- 你可以使用 LIMIT 属性来设定返回的记录数。
- 你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
mysql> select name,sex,age from user;
+------+-----+-----+
| name | sex | age |
+------+-----+-----+
| hu | F | 21 |
| li | M | 23 |
+------+-----+-----+
2 rows in set (0.00 sec)