mysql语法

mysql 语句

1、rpm -qa | grep mysql //检查是否有mysql的安装包

2、rpm -e mysql  // 普通删除模式

3、rpm -e --nodeps mysql  // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

4、wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm //下载rpm安装包

5、rpm -ivh mysql-community-release-el7-5.noarch.rpm //rpm安装

6、yum update //升级yum

7、yum install mysql-server //yum 安装mysql服务

8、chown mysql:mysql -R /var/lib/mysql //更改mysql的属主

9、mysqld --initialize //初始化mysql

10、systemctl start mysqld //启动mysql

11、systemctl status mysqld //查看mysql的运行状态

12、mysql 登录mysql数据库

13、mysqladmin -u root -p password 密码 //如果是新数据库,未设置密码,可使用

14、mysqladmin -u 用户 -p ‘旧密码‘ ‘新密码‘ //更改密码,在数据库外面可使用,无需进入数据库内

15、mysql -u root -p123123 //登录mysql

16、use 库名; //切换数据库

17、show databases; //查看数据库

18、show tables; //查看表

show full processlist;
show status like ‘%Max_used_connections%‘;
show status like ‘%Threads_connected%‘;#当前连接数
show status like ‘%table_lock%‘;#表锁定
show status like ‘innodb_row_lock%‘;#行锁定
show status like ‘%qcache%‘; #查询缓存情况
show variables like "%query_cache%";
SHOW STATUS LIKE ‘Qcache%‘;
show variables like "%binlog%";
show status like ‘Aborted_clients‘;#由于客户没有正确关闭连接已经死掉,已经放弃的连接数量
show variables like ‘%max_connections%‘;//查看最大连接数量
show variables like ‘%timeout%‘;#查看超时时间
show variables like ‘log_%‘; #查看日志是否启动

19、CREATE DATABASE 数据库名; //创建数据库

20、drop database 数据库名; //删除数据库

21、创建数据表:
CREATE TABLE IF NOT EXISTS `runoob_tbl`(
`runoob_id` INT UNSIGNED AUTO_INCREMENT,
`runoob_title` VARCHAR(100) NOT NULL,
`runoob_author` VARCHAR(40) NOT NULL,
`submission_date` DATE,
PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

22、DROP TABLE table_name ; //删除数据表

23、插入数据:
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN ); 如果数据是字符型,必须使用单引号或者双引号,如:"value"。

23、查询数据库:
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]

24、select * from user; //查看一个表的所有内容

25、以下是 SQL SELECT 语句使用 WHERE 子句从数据表中读取数据的通用语法:
SELECT field1, field2,...fieldN FROM table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....

26、UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法::
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

27、以下是 SQL DELETE 语句从 MySQL 数据表中删除数据的通用语法:
DELETE FROM table_name [WHERE Clause]

--------实例:创建一个表---------
1、创建一个库:create database yu;
2、进入库:use yu;
3、创建一个表:
CREATE TABLE `employee_tbl` (
-> `id` int(11) NOT NULL,
-> `name` char(10) NOT NULL DEFAULT ‘‘,
-> `date` datetime NOT NULL,
-> `singin` tinyint(4) NOT NULL DEFAULT ‘0‘ COMMENT ‘登录次数‘,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4、查看表的结构:show columns from employee_tbl;

5、插入数据:INSERT INTO `employee_tbl` VALUES (‘1‘, ‘小明‘, ‘2016-04-22 15:25:33‘, ‘1‘), (‘2‘, ‘小王‘, ‘2016-04-20 15:25:47‘, ‘3‘),
(‘3‘, ‘小丽‘, ‘2016-04-19 15:26:02‘, ‘2‘), (‘4‘, ‘小王‘, ‘2016-04-07 15:26:14‘, ‘4‘), (‘5‘, ‘小明‘, ‘2016-04-11 15:26:40‘, ‘4‘),
(‘6‘, ‘小明‘, ‘2016-04-04 15:26:54‘, ‘2‘);

6、确认一下导入的数据:
mysql> select * from employee_tbl;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| 1 | 小明 | 2016-04-22 15:25:33 | 1 |
| 2 | 小王 | 2016-04-20 15:25:47 | 3 |
| 3 | 小丽 | 2016-04-19 15:26:02 | 2 |
| 4 | 小王 | 2016-04-07 15:26:14 | 4 |
| 5 | 小明 | 2016-04-11 15:26:40 | 4 |
| 6 | 小明 | 2016-04-04 15:26:54 | 2 |
+----+--------+---------------------+--------+

7、使用 GROUP BY 语句 将数据表按名字进行分组,并统计每个人有多少条记录::
mysql> SELECT name, COUNT(*) FROM employee_tbl GROUP BY name;
+--------+----------+
| name | COUNT(*) |
+--------+----------+
| 小丽 | 1 |
| 小明 | 3 |
| 小王 | 2 |
+--------+----------+
3 rows in set (0.00 sec)

8、使用 WITH ROLLUP
WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。
例如我们将以上的数据表按名字进行分组,再统计每个人登录的次数:
mysql> SELECT name, SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP;
+--------+--------------+
| name | singin_count |
+--------+--------------+
| 小丽 | 2 |
| 小明 | 7 |
| 小王 | 7 |
| NULL | 16 |
+--------+--------------+
4 rows in set (0.00 sec)


9、我们可以使用 coalesce 来设置一个可以取代 NUll 的名称,coalesce 语法:
select coalesce(a,b,c);
参数说明:如果a==null,则选择b;如果b==null,则选择c;如果a!=null,则选择a;如果a b c 都为null ,则返回为null(没意义)。
以下实例中如果名字为空我们使用总数代替:
mysql> SELECT coalesce(name, ‘总数‘), SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP;
+--------------------------+--------------+
| coalesce(name, ‘总数‘) | singin_count |
+--------------------------+--------------+
| 小丽 | 2 |
| 小明 | 7 |
| 小王 | 7 |
| 总数 | 16 |
+--------------------------+--------------+
4 rows in set (0.00 sec)

10、插入一条数据:
mysql> insert into employee_tbl ( id,name,date,singin ) values ( ‘7‘,‘小华‘,‘2016-04-22 15:25:33‘,‘6‘ );
Query OK, 1 row affected (0.01 sec)

mysql> select * from employee_tbl;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| 1 | 小明 | 2016-04-22 15:25:33 | 1 |
| 2 | 小王 | 2016-04-20 15:25:47 | 3 |
| 3 | 小丽 | 2016-04-19 15:26:02 | 2 |
| 4 | 小王 | 2016-04-07 15:26:14 | 4 |
| 5 | 小明 | 2016-04-11 15:26:40 | 4 |
| 6 | 小明 | 2016-04-04 15:26:54 | 2 |
| 7 | 小华 | 2016-04-22 15:25:33 | 6 |
+----+--------+---------------------+--------+
7 rows in set (0.00 sec)

8、更新语句
mysql> update employee_tbl set singin=‘5‘ where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee_tbl;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| 1 | 小明 | 2016-04-22 15:25:33 | 5 |
| 2 | 小王 | 2016-04-20 15:25:47 | 3 |
| 3 | 小丽 | 2016-04-19 15:26:02 | 2 |
| 4 | 小王 | 2016-04-07 15:26:14 | 4 |
| 5 | 小明 | 2016-04-11 15:26:40 | 4 |
| 6 | 小明 | 2016-04-04 15:26:54 | 2 |
| 7 | 小华 | 2016-04-22 15:25:33 | 6 |
+----+--------+---------------------+--------+
7 rows in set (0.00 sec)

9、查看某一行数据
mysql> select * from employee_tbl where id=1;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| 1 | 小明 | 2016-04-22 15:25:33 | 5 |
+----+--------+---------------------+--------+
1 row in set (0.00 sec)

10、删除某一行数据:
mysql> delete from employee_tbl where id=1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from employee_tbl;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| 2 | 小王 | 2016-04-20 15:25:47 | 3 |
| 3 | 小丽 | 2016-04-19 15:26:02 | 2 |
| 4 | 小王 | 2016-04-07 15:26:14 | 4 |
| 5 | 小明 | 2016-04-11 15:26:40 | 4 |
| 6 | 小明 | 2016-04-04 15:26:54 | 2 |
| 7 | 小华 | 2016-04-22 15:25:33 | 6 |
+----+--------+---------------------+--------+
6 rows in set (0.00 sec)

mysql语法

上一篇:MySQL5.7配置SSL加密


下一篇:SQL:经典面试50题