MySQL操作命令

一.MySQL连接命令

mysql
-u:指定用户
-p:指定密码
-S:指定socket文件
-P:指定端口
-h:指定主机域
-e:指定sql语句

mysql> status s 查看状态
mysql> select * from mysql.userG  格式化成key:value的形式
mysql> T /tmp/a.log  记录操作日志
mysql> c   结束操作
mysql> . /root/world.sql source 导入sql文件
mysql> u test       切换数据库
mysql> q   退出

mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+

mysqladmin 命令

#检测MySQL是否存活
[root@elk01 ~]# mysqladmin -uroot -p123 ping
mysqld is alive
#查看状态
[root@elk01 ~]# mysqladmin -uroot -p123 status
#关闭数据库
[root@elk01 ~]#  mysqladmin -uroot -p123 shutdown
#查看参数
[root@elk01 ~]# mysqladmin -uroot -p123 variables
#删库
[root@elk01 ~]# mysqladmin -uroot -p123 drop test
#建库
[root@elk01 ~]# mysqladmin -uroot -p123 create oldboy
#修改密码
[root@elk01 ~]# mysqladmin -uroot -p123 password '1'

二.SQL语句

1.什么是SQL

结构化的查询语句

2.SQL的种类

DDL:数据定义语言

库对象:库名字、库属性
开发规范:库名小写

# 创建数据库
mysql> create database school;
# 删除数据库
mysql> drop database oldboy;
# 改变数据库属性
mysql> alter database oldboy charset gbk;

表对象:列名、列属性、约束

# 创建表
mysql> create table student(
sid INT,
sname VARCHAR(20),
sage TINYINT,
sgender ENUM('m','f'),
cometime DATETIME);

数据类型

int:  整数-231~231-1

varchar:  字符类型(变长)

char:  字符类型(定长)

tinyint:  整数 -128-128

enum:  枚举类型

datetime:  时间类型 年 月 日 时 分 秒

#创建表加其他属性
mysql> create table student(
sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学号',
sname VARCHAR(20) NOT NULL COMMENT '学生姓名',
sage TINYINT UNSIGNED COMMENT '学生年龄',
sgender ENUM('m','f')  NOT NULL DEFAULT 'm' COMMENT '学生性别',
cometime DATETIME NOT NULL COMMENT '入学时间')charset utf8 engine innodb;

数据属性

not null:  非空

primary key:  主键(唯一且非空的)

auto_increment:  自增(此列必须是:primary key或者unique key)

unique key:  单独的唯一的

default:  默认值

usigned:   非负数

comment:  注释

# 修改表定义
# 添加到最后
mysql> alter table student1 add renyachun varchar(10);
# 插入列 到最前面
mysql> alter table student1 add huwentao varchar(10) first;
# 删除列
mysql> alter table student1 drop renyachun;
# 插入列在...之后
mysql> alter table student1 add renyachun varchar(10) after huwentao;
# 修改列名
mysql> alter table student1 rename st1;
#修改列及定义(列属性)
mysql> alter table student1 modify huwentao varchar(20);

删除表

#删除表 
mysql> drop table student;

DCL:数据控制语言

针对权限进行控制

grant & revoke

#授权root@10.0.0.51用户所有权限(非炒鸡管理员)
mysql> grant all privileges on *.* to test@'%' identified by '123';
mysql> grant all on *.* to root@'10.0.0.51' identified by '123';


#怎么去授权一个炒鸡管理员呢?
mysql> grant all on *.* to root@'10.0.0.51' identified by '123' with grant option;

# 收回select权限
mysql> revoke select on *.* from test1@'%';
# 查看权限
mysql> show grants for root@'10.0.0.51';

DML:数据操作语言

操作表的数据行信息

insert

#基础用法,插入数据
mysql> insert into student values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#规范用法,插入数据
mysql> insert into student(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#插入多条数据
mysql> insert into student(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456),
('linux02',2,NOW(),'zhangsi',21,'f',NOW(),111,1234567);

update

#不规范
mysql> update student set sgender='f';
#规范update修改
mysql> update student set sgender='f' where sid=1;
#如果非要全表修改
mysql> update student set sgender='f' where 1=1;

delete

#不规范
mysql> delete from student;
#规范删除(危险)
mysql> delete from student where sid=3;
#DDL删除表
mysql> truncate table student;

伪删除

使用update代替delete

1)额外添加一个状态列
mysql> alter table student add status enum(1,0) default 1;
2)使用update
mysql> update student set status='0' where sid=1;
3)应用查询存在的数据
mysql> select * from student where status=1;

DQL:数据查询语句

#常用用法
mysql> select countrycode,district from city;
#查询单列
mysql> select countrycode from city;
#行级查询
mysql> select countrycode,district from city limit 2;
mysql> select id,countrycode,district from city limit 2,2;
#条件查询
mysql> select name,population from city where countrycode='CHN';
#多条件查询
mysql> select name,population from city where countrycode='CHN' and district='heilongjiang';
#模糊查询
mysql> select name,population,countrycode from city where countrycode like '%H%' limit 10;
#排序查询(顺序)
mysql> select id,name,population,countrycode from city order by countrycode limit 10;
#排序查询(倒叙)
mysql> select id,name,population,countrycode from city order by countrycode desc limit 10;
#范围查询(>,<,>=,<=,<>)
mysql> select * from city where population>=1410000;
#范围查询OR语句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范围查询IN语句
mysql> select * from city where countrycode in ('CHN','USA');

 select的高级用法(多表查询)

表结构如下

mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+

mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+


mysql> desc country;
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Code | char(3) | NO | PRI | | |
| Name | char(52) | NO | | | |
| Continent | enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') | NO | | Asia | |
| Region | char(26) | NO | | | |
| SurfaceArea | float(10,2) | NO | | 0.00 | |
| IndepYear | smallint(6) | YES | | NULL | |
| Population | int(11) | NO | | 0 | |
| LifeExpectancy | float(3,1) | YES | | NULL | |
| GNP | float(10,2) | YES | | NULL | |
| GNPOld | float(10,2) | YES | | NULL | |
| LocalName | char(45) | NO | | | |
| GovernmentForm | char(45) | NO | | | |
| HeadOfState | char(60) | YES | | NULL | |
| Capital | int(11) | YES | | NULL | |
| Code2 | char(2) | NO | | | |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+

mysql> desc countrylanguage;
+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| CountryCode | char(3) | NO | PRI | | |
| Language | char(30) | NO | PRI | | |
| IsOfficial | enum('T','F') | NO | | F | |
| Percentage | float(4,1) | NO | | 0.0 | |
+-------------+---------------+------+-----+---------+-------+

 

1.1 传统连接(只能内连接,)

#世界上小于100人的人口城市是哪个国家的?
select city.name,city.countrycode,country.name 
from city,country 
where city.countrycode=country.code 
and city.population<100;

1.2 NATURAL JOIN(自连接的表要有共同的列名字)

SELECT city.name,city.countrycode ,countrylanguage.language ,city.population
FROM  city NATURAL  JOIN  countrylanguage 
WHERE population > 1000000
ORDER BY population;

建议:使用join语句时,小表在前,大表在后

1.4外连接

select city.name,city.countrycode,country.name 
from city left join country 
on city.countrycode=country.code 
and city.population<100;

1.5 UNION(合并查询)

#范围查询OR语句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范围查询IN语句
mysql> select * from city where countrycode in ('CHN','USA');
替换为:
mysql> select * from city where countrycode='CHN' 
union  all
select * from city where countrycode='USA' limit 10

uninon: 去重复合并

union all: 不去重复

使用情况: union<union all

原文地址:https://www.cnblogs.com/gongcheng-/p/10133566.html