mysql database commonly used commands and database operations command Detailed Summary

MySQL is commonly used commands for text subtotals, Commands, illustration, as reproduced or quoted, please put this article in connection prominently.

This article can be used as a common tool collection, as you praise a little help please support!

Since the SQL language belonging to class B, case-insensitive, for ease of reading, all SQL commands herein, all lowercase, do not spray.

 

Category commonly used commands:

Service level: Contains Start services, such as closed, popular to say that before entering MySQL, enter in a terminal, command and MySQL-related;

Database level: the establishment of a database that contains, or delete;

Data Sheet level: contains table creation, modification and other property, belonging to the pre-project design data tables related commands;

Level data: contains data add, delete, change, search, belongs to frequently used commands;

 

Description:

 

[Content parameter] - an optional parameter refers to

 

 

 

 

First, the service level commands (brief, does not involve the port, security, and other parameters)

1, start, stop, restart the MySQL service:

 

 

# win系统
>net start mysql    # 启动
>net stop mysql    # 停止

# ox系统,如果提示找不到命令,可直接进入安装目录的bin文件夹内运行命令
$ mysql.server start # 通过brew安装的MySQL 启动
$ mysql.server stop    # 停止
$ mysql.server restart    # 重启

# Linux系统,如果提示找不到命令,可直接进入安装目录的bin文件夹内运行命令
$ path_mysql/init.d/mysqld start    # 启动  path_mysql是指安装路径
$ path_mysql/init.d/mysqld stop    # 停止
$ path_mysql/init.d/mysqld restart     # 重启

# 通过service 操作
$ service mysqld start    # 启动
$ service mysqld stop    # 停止
$ service mysqld restart    # 重启
$ service mysql status    # 查看服务状态

 

 

 

 

 

2, the first installation, start the configuration service, win system reference here

# ox系统
$ mysql_secure_installation

 

3, the first landing (newly installed, not configured)

# win系统
>mysql -hlocalhost -uroot

# ox系统
$mysql -uroot

4, landing

mysql -u用户名 -p密码             # (密码)可省略,回车后再输入,如不省略,注意-p和密码间没有空格

例如:mysql -uroot -pmypasswd

5, export / import database files (to be updated)

 

 

 

Second, the database level

1, the operation command

 

command Logogram Specific meaning
? Displays help information
clear c Clear the current input statement
connect r Connecting to the server, and optional parameters for the host database
delimiter d Set the statement delimiter
ego G Send commands to the MySQL server, and displays the results
exit q Exit MySQL
go g Send commands to the MySQL server
help h Displays help information
notee t Do not write the output file
print p Print command
prompt R MySQL prompt change
quit q Exit MySQL
rehash # Hash complete reconstruction
source . Execute a SQL script file to a file name as an argument
status s MySQL acquired from the server state information
tee T Set the output file, and the information is added to all given output file
use in With another database
charset C Switch to another character set
warning W After each statement displays a warning
nowarning w Do not display warnings after each statement

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2, commonly used commands

 

 

mysql>show databases;    # 查看已有的数据库

mysql>create database 数据库名称 [dafault character set utf8];    # 创建[字符编码为utf8的]数据库

mysql>show create database 数据库名称;    # 显示新创建的数据库信息

mysql>alter database 数据库名称 default character set gbk  collate gbk_bin;    #将数据库的编码格式设为 gbk 同时注意最后是设为gbk_bin,如果改为utf8,则最后是utf8_bin

mysql>drop database  数据库名称;    # 删除数据库

mysql>use 数据库名称;    # 切换至数据库     同 u [数据库名称]    注意:使用简写命令时最后不要加 ;
mysql>select database(); # 显示当前所在的数据库

 

 

 

 

For example as follows:

 

 

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

mysql> show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> use test;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)


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

 

 

 

 

 

Third, the data table level

 

1, commonly used commands

 

mysql>show tables;    # 显示当前数据库下所有数据表

mysql>create table 表名 (
字段名1      数据类型   [完整性约束条件],
字段名2     数据类型   [完整性约束条件],
字段名3      数据类型   [完整性约束条件],
..........
)[engine=innodb];    # 创建数据表  [引擎类型,默认innodb]

mysql>show create table 表名; # 显示数据表的基本信息,含字段及其属性、引擎、字符集等

mysql>desc 表名;    # 显示数据表信息,显示字段及其属性

mysql>alter table 原表名 rename to 新表名;    # 修改数据表名

mysql>alter table 表名 change 原字段名 新字段名 新数据属性;    # 修改表中字段

mysql>alter table 表名 modify 字段名 数据类型;    # 修改表中字段属性

mysql>alter table 表名 add 新字段名 数据类型 [约束条件];    # 添加新字段

mysql>alter table 表名 drop 字段名;    # 删除字段  (慎重!!!一般使用逻辑删除)

mysql>drop table 表名;   # 删除数据表    (慎重!!!)

 

 

 

2, illustrated

 

 

mysql> show tables;           # 显示所有数据表

+------------------+
| Tables_in_school |
+------------------+
| class            |
| graduate         |
| student          |
| teacher          |
+------------------+
4 rows in set (0.00 sec)
 
mysql> create table test(             # 新建数据表,engine=innodb可省略
    -> id int(100) unsigned not null primary key auto_increment,
    -> age int(100)
    -> )engine=innodb;
Query OK, 0 rows affected (0.03 sec)
 
mysql> show create table test;           # 显示数据表详细信息
+-----------------+
| Table | Create Table                                                                                                                                                     |
+----------+
| test  | CREATE TABLE `test` (
  `id` int(100) unsigned NOT NULL AUTO_INCREMENT,
  `age` int(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------------+
1 row in set (0.01 sec)
 
mysql> desc test;                        # 显示数据表字段信息
+-------+-------------------+------+-----+---------+----------------+
| Field | Type              | Null | Key | Default | Extra          |
+-------+-------------------+------+-----+---------+----------------+
| id    | int(100) unsigned | NO   | PRI | NULL    | auto_increment |
| age   | int(100)          | YES  |     | NULL    |                |
+-------+-------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 
mysql> alter table test rename to test1;              # 重命名数据表
Query OK, 0 rows affected (0.01 sec)

 mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| class            |
| graduate         |
| student          |
| teacher          |
| test1            |
+------------------+
5 rows in set (0.00 sec)
mysql> alter table test1 change age birthday date not null;      # 修改数据表字段,含字段名和字段属性
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test1;
+----------+-------------------+------+-----+---------+----------------+
| Field    | Type              | Null | Key | Default | Extra          |
+----------+-------------------+------+-----+---------+----------------+
| id       | int(100) unsigned | NO   | PRI | NULL    | auto_increment |
| birthday | date              | NO   |     | NULL    |                |
+----------+-------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 
mysql> alter table test1 modify birthday datetime;            # 修改字段属性
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test1;
+----------+-------------------+------+-----+---------+----------------+
| Field    | Type              | Null | Key | Default | Extra          |
+----------+-------------------+------+-----+---------+----------------+
| id       | int(100) unsigned | NO   | PRI | NULL    | auto_increment |
| birthday | datetime          | YES  |     | NULL    |                |
+----------+-------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


mysql> alter table test1 drop birthday;                 # 删除字段
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
 

mysql> desc test1;
+-------+-------------------+------+-----+---------+----------------+
| Field | Type              | Null | Key | Default | Extra          |
+-------+-------------------+------+-----+---------+----------------+
| id    | int(100) unsigned | NO   | PRI | NULL    | auto_increment |
+-------+-------------------+------+-----+---------+----------------+
1 row in set (0.00 sec)


mysql> drop table test1;                             # 删除数据表
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| class            |
| graduate         |
| student          |
| teacher          |
+------------------+
4 rows in set (0.00 sec)

 

 

 

 

Fourth, the data level

 

1, commonly used commands

 

mysql>insert into 表名 [字段名1,字段名2,....]  values
(值1,值2,.......),
(值1,值2,.......),
(值1,值2,.......)
........
;     # 如使用字段,则添加的每条记录的值必须与字段一一对应;如不指定字段,则属于按位置添加数据,每条记录的值须同数据表中所有字段一一对应

mysql>update 表名 set 
字段名1=值1,
字段名2=值2,
....  
[where 条件];    # 更新/更改数据:[条件限制,如不加条件,则默认所有记录的该字段均修改]

mysql>alert table 表1名 add foreign key(字段1名) references 表2名(字段2名);    # 为表1的字段1添加外键,连接表2的字段2,外键可选约束条件作为参数

mysql>alert table 表名 drop foreign key 字段名;    # 删除外键

mysql>create [unique | fulltext | spatial] index 索引名 on 表名(字段名[asc | desc]);    # 创建索引,索引会提高查找速度,但是会增加物理开销

mysql>drop index 索引名 on 表名;    # 删除索引

mysql>delete from 表名 [where 条件];# 删除表中数据[条件限制,如无条件,则清空数据表]

mysql>truncate table 表名;    # 清空数据表

mysql>select 字段名1,字段名2,...... from 表名;    # 查看表中数据,如查看所有数据使用  select * from 表名 ,这是数据查看的基本形式,后面的例子中会有拓展

 

 

 

2, illustrated

 

 

mysql> alter table student add foreign key(class) references class(cid);     # 添加外键,如果表中有数据,则会添加失败

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> create index sid_index on student(sid);          # 创建索引

Query OK, 0 rows affected (0.02 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> show create table student;

+-----------------+

| Table   | Create Table                                                                                                                        

+-----------------+

| student | CREATE TABLE `student` (

  `sid` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `name` varchar(10) NOT NULL,

  `gender` char(1) NOT NULL,

  `age` int(3) unsigned NOT NULL,

  `birthday` datetime NOT NULL,

  `phone` varchar(11) DEFAULT NULL,

  `class` int(10) unsigned NOT NULL,

  `grade` int(10) unsigned NOT NULL,

  PRIMARY KEY (`sid`),

  KEY `sid_index` (`sid`),    # 索引在这里

  KEY `class` (`class`),

  CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class`) REFERENCES `class` (`cid`)            # 这里显示外键的名称:student_ibfk_1,删除的时候要用这个字段

) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 |

+-----------+

1 row in set (0.00 sec)

 

mysql> insert into class values           # 添加数据,先添加class表数据,注意:如果存在外键,则外键所指向的数据表不能为空,否则无法添加数据

    -> (1,'10级1班',1,45,'东教学楼1层'),

    -> (2,'09级1班',2,48,'东教学楼2层'),

    -> (3,'09级2班',2,50,'东教学楼2层'),

    -> (4,'08级1班',3,51,'东教学楼3层'),

    -> (5,'08级2班',4,49,'东教学楼3层');

Query OK, 5 rows affected (0.01 sec)

Records: 5  Duplicates: 0  Warnings: 0

 

mysql> insert into student values             # 再添加student表数据

    -> (1,'李杰','男',21,'1990-1-2',13012345678,1,1),

    -> (2,'王倩','女',22,'1989-4-21',15212345678,1,1),

    -> (3,'张大力','男',20,'1991-2-12',NULL,2,2),

    -> (4,'王晓红','女',20,'1991-7-5',18900001111,2,2),

    -> (5,'周大宝','男',21,'1990-12-23',NULL,3,2),

    -> (6,'王志刚','男',22,'1989-10-10',NULL,3,2),

    -> (7,'刘明','男',21,'1990-11-30',15912345678,4,3),

    -> (8,'郭芙蓉','女',20,'1991-6-17',NULL,4,3),

    -> (9,'李飞','男',23,'1988-12-3',13212345678,5,3),

    -> (10,'苏洁','女',25,'1986-5-16',NULL,5,3);

Query OK, 10 rows affected (0.00 sec)

Records: 10  Duplicates: 0  Warnings: 0

 

mysql> select * from student;                #  查询student 中所有数据

+-----+-----------+--------+-----+---------------------+-------------+-------+-------+

| sid | name      | gender | age | birthday            | phone       | class | grade |

+-----+-----------+--------+-----+---------------------+-------------+-------+-------+

|   1 | 李杰      | 男     |  21 | 1990-01-02 00:00:00 | 13012345678 |     1 |     1 |

|   2 | 王倩      | 女     |  22 | 1989-04-21 00:00:00 | 15212345678 |     1 |     1 |

|   3 | 张大力    | 男     |  20 | 1991-02-12 00:00:00 | NULL        |     2 |     2 |

|   4 | 王晓红    | 女     |  20 | 1991-07-05 00:00:00 | 18900001111 |     2 |     2 |

|   5 | 周大宝    | 男     |  21 | 1990-12-23 00:00:00 | NULL        |     3 |     2 |

|   6 | 王志刚    | 男     |  22 | 1989-10-10 00:00:00 | NULL        |     3 |     2 |

|   7 | 刘明      | 男     |  21 | 1990-11-30 00:00:00 | 15912345678 |     4 |     3 |

|   8 | 郭芙蓉    | 女     |  20 | 1991-06-17 00:00:00 | NULL        |     4 |     3 |

|   9 | 李飞      | 男     |  23 | 1988-12-03 00:00:00 | 13212345678 |     5 |     3 |

|  10 | 苏洁      | 女     |  25 | 1986-05-16 00:00:00 | NULL        |     5 |     3 |

+-----+-----------+--------+-----+---------------------+-------------+-------+-------+

10 rows in set (0.00 sec)

 

mysql> select sid,name,gender,age as '年龄' from student where class=3 and grade=2;   

# 条件查询,查询class=3 & grade=2的学生sid、name、gender、age,并将age重命名

+-----+-----------+--------+--------+

| sid | name      | gender | 年龄   |

+-----+-----------+--------+--------+

|   5 | 周大宝    | 男     |     21 |

|   6 | 王志刚    | 男     |     22 |

+-----+-----------+--------+--------+

2 rows in set (0.01 sec)

 

 

# 查询王倩同学的上课地点,显示姓名、性别及地点

mysql> select s.name,s.gender,c.location         # 使用数据表简称获取响应的字段,如果该字段在所有使用的数据表中唯一,则可以不带表名,直接写字段

    -> from student as s,class as c            # 多表查询时,通常将每个表重命名为简称

    -> where s.class=c.cid                # 多表查询的连接依据

    -> and s.name='王倩';                  # 附加查询条件

+--------+--------+------------------+

| name   | gender | location         |

+--------+--------+------------------+

| 王倩   | 女     | 东教学楼1层      |

+--------+--------+------------------+

1 row in set (0.01 sec)

 

# 查看2年级的男女数量

mysql> select gender,count(*) from student where grade=2 group by gender;  # 使用group by实现分组 count()是聚合函数

+--------+----------+

| gender | count(*) |

+--------+----------+

| 女     |        1 |

| 男     |        3 |

+--------+----------+

2 rows in set (0.01 sec)

 

mysql> select gender,count(*) from student where grade=2 group by gender having gender='男'; 

# group by 后面可以加条件语句having,having可以使用聚合函数,而where是不可以的

 

+--------+----------+

| gender | count(*) |

+--------+----------+

| 男     |        3 |

+--------+----------+

1 row in set (0.01 sec)

 

 

 

# 查询人数大于2的年级中,显示年级、人数、各年级的学生平均年龄、男生数量,并安装平均年龄倒序显示

mysql> select grade,count(*),avg(age),count(gender='男')

    -> from student

    -> group by grade

    -> having count(*)>2 and count(gender='男')

    -> order by avg(age) desc;

+-------+----------+----------+---------------------+

| grade | count(*) | avg(age) | count(gender='男')  |

+-------+----------+----------+---------------------+

|     3 |        4 |  22.2500 |                   4 |

|     2 |        4 |  20.7500 |                   4 |

+-------+----------+----------+---------------------+

2 rows in set (0.00 sec)

 

 

 

 

 

 

 

 

A common query structure:

 

Copy the code

查询语句                                                     执行顺序

select [distinct] * | 字段1,字段2,..| 聚合函数               ⑤

from 表1 as a,表2 as b .....               ①
join on a.id=b.id .......                  ②

where 字段|isfull [= > < is not in like between and or ]  值 (子查询).....      ③

group by 字段                  ④

having  条件  [聚合函数]           ⑥

order by 字段 | [聚合函数]   [asc | desc]    ⑦

limit [offset] 数量;          ⑧
Published 53 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/mysql012/article/details/104110302