mysql 5.7 增删改查及别名的用法

时间:2021-07-10 12:22:42

1.启动和停止服务

一)启动和停止

#启动服务:
$sudo service mysql start
#停止服务:
$sudo service mysql stop

二)创建和选择数据库

【创建数据库】

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

mysql> create database testdb default character set 'utf8';
Query OK, 1 row affected (0.00 sec)

mysql 5.7  增删改查及别名的用法

status命令查看数据库状态:

mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7., for Linux (i686) using EditLine wrapper Connection id:
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7. MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8

UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: hours min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.010

【选择数据库】

mysql> use testdb;
Database changed

【查看当前登录的数据库】

mysql> select database() mydb;
+--------+
| mydb |
+--------+
| testdb |
+--------+
row in set (0.00 sec)
mysql> desc t_users;
+----------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+-------------------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| name | varchar() | NO | | NULL | |
| owner | varchar() | NO | | NULL | |
| descinfo | varchar() | YES | | NULL | |
| sex | char() | NO | | | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
| addtime | datetime | YES | | CURRENT_TIMESTAMP | |
+----------+-------------+------+-----+-------------------+----------------+
rows in set (0.00 sec)

【删除数据库】

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

【创建用户和分配权限】

mysql> grant all privileges  on testdb.* to 'test2'@'localhost' identified by '';
Query OK, rows affected, warning (0.00 sec) mysql> flush privileges;
Query OK, rows affected (0.00 sec) mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| % | root | *9AAE89836D1D658A0BED88FEB96A3DFF84C94D77 |
| % | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| % | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | test | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | test2 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+---------------+-------------------------------------------+
rows in set (0.00 sec)

【创建表】

从源文件引入创建:假设有个sql.txt

DROP TABLE IF EXISTS t_users;
create table t_users (
id int() not null auto_increment,
name varchar() not null comment '姓名',
owner varchar() not null,
descinfo varchar(),
sex char() not null default '',
birth date comment '生日',
death date,
addtime datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
primary key (id)
);

然后执行:

mysql> source ./sql.txt;
Query OK, rows affected, warning (0.00 sec) Query OK, rows affected (0.02 sec) mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| t_users |
+------------------+
row in set (0.00 sec) mysql> desc t_users;
+----------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+-------------------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| name | varchar() | NO | | NULL | |
| owner | varchar() | NO | | NULL | |
| descinfo | varchar() | YES | | NULL | |
| sex | char() | NO | | | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
| addtime | datetime | YES | | CURRENT_TIMESTAMP | |
+----------+-------------+------+-----+-------------------+----------------+
rows in set (0.00 sec) mysql> insert into t_users (name,owner,descinfo,birth,death) values ('php webs','test webs','test ok next boooks',curdate(),curdate());
Query OK, row affected (0.01 sec) mysql> select t.* from t_users t;
+----+----------+-----------+---------------------+-----+------------+------------+---------------------+
| id | name | owner | descinfo | sex | birth | death | addtime |
+----+----------+-----------+---------------------+-----+------------+------------+---------------------+
| | php webs | test webs | test ok next boooks | | -- | -- | -- :: |
+----+----------+-----------+---------------------+-----+------------+------------+---------------------+
row in set (0.00 sec)

2.删掉时候用别名

delete t from t_users t where t.title='John';

3.查询及排序

默认asc升序 降序desc

SELECT name, birth FROM pet ORDER BY birth

结果:

mysql 5.7  增删改查及别名的用法

查询当前时间和mysql版本函数 curdate() 和 current_date:

mysql> select version(),curdate();
+-----------+------------+
| version() | curdate() |
+-----------+------------+
| 5.7. | -- |
+-----------+------------+
row in set (0.00 sec)
mysql> select version(),current_date;
+-----------+--------------+
| version() | current_date |
+-----------+--------------+
| 5.7. | -- |
+-----------+--------------+
row in set (0.00 sec)

now(): 函数查询当前时间

mysql> select version(),now();
+-----------+---------------------+
| version() | now() |
+-----------+---------------------+
| 5.7. | -- :: |
+-----------+---------------------+
row in set (0.00 sec)

TIMESTAMPDIFF (year,birth,curdate()):日期计算使用 函数计算 查询年龄:

mysql> SELECT name, birth, CURDATE(),TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age FROM pet;

结果:

mysql 5.7  增删改查及别名的用法

 is not null 查询不为空的数据:

mysql> select t.* from t_users t where t.title is not null;
+----+------------+--------------+-----------------+
| id | title | author | submission_date |
+----+------------+--------------+-----------------+
| | 学习 PHP | 菜鸟教程 | -- |
| | PHP webs | Doe | -- |
+----+------------+--------------+-----------------+
rows in set (0.00 sec)

day()  和month()函数的使用:查询指定日期的数据:

mysql> select t.* from t_users t where day(t.submission_date)=;
+----+------------+--------------+-----------------+
| id | title | author | submission_date |
+----+------------+--------------+-----------------+
| | 学习 PHP | 菜鸟教程 | -- |
+----+------------+--------------+-----------------+
row in set (0.01 sec) mysql> select t.* from t_users t where month(t.submission_date)=;
+----+------------+--------------+-----------------+
| id | title | author | submission_date |
+----+------------+--------------+-----------------+
| | 学习 PHP | 菜鸟教程 | -- |
| | PHP webs | Doe | -- |
+----+------------+--------------+-----------------+
rows in set (0.00 sec)

mod(n,m) 取余函数的使用

查询下一个月:

mysql> select mod(month(curdate()),)+ as nextmonth;
+-----------+
| nextmonth |
+-----------+
| |
+-----------+
row in set (0.00 sec)

如:想要查询下一个月过生日的人;

mysql> select t.* from t_users where t.month(birth) =  mod(month(curdate()),)+ ;

1 is null 返回false;

1 is not null 返回 true;

mysql> select  is not null;
+---------------+
| is not null |
+---------------+
| |
+---------------+
row in set (0.00 sec)

mysql 5.7  增删改查及别名的用法

利用count()函数统计数据 以owner分组 group by

mysql> select owner,count(name) from t_users where name like 'C%' group by owner;
+----------+-------------+
| owner | count(name) |
+----------+-------------+
| C webs | |
| GOGGGOOK | |
| GOOOK | |
+----------+-------------+
rows in set (0.00 sec)

max(col):查询某列的最大值

mysql> select max(id) from t_users;
+---------+
| max(id) |
+---------+
| |
+---------+
row in set (0.00 sec)

mysql 5.7  增删改查及别名的用法

mysql 5.7  增删改查及别名的用法

mysql 5.7  增删改查及别名的用法

【统计每天访问量】

mysql> select t.* from t1 t;
+------+-------+------+
| year | month | day |
+------+-------+------+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
+------+-------+------+
rows in set (0.01 sec) mysql> select year,month,bit_count(bit_or(<<day)) as days from t1 group by year,month;
+------+-------+------+
| year | month | days |
+------+-------+------+
| | | |
| | | |
+------+-------+------+
rows in set (0.00 sec)

4.Mysql使用正则表达式匹配模糊查询 like的使用

Mysql提供了一个标准的SQL模式匹配,和基于扩展的正则表达式的模式匹配Unix工具(如vi,grep,sed)一样。
SQL模式匹配可以使用:

“_“来匹配任意单个字符,

”%“可以用来匹配任意数量(包含0个字符)的字符。

在MySQL中,SQL模式匹配的大小写默认是不敏感的,以下有一些例子,当你在使用SQL模式时,不要使用=或<>,而是使用LIKE或NOTLIKE。

要找到P%开头的名字:

mysql> select t.* from t_users t where t.title like 'P%'
-> ;
+----+----------+--------+-----------------+
| id | title | author | submission_date |
+----+----------+--------+-----------------+
| | PHP webs | Doe | -- |
+----+----------+--------+-----------------+
row in set (0.00 sec)

查询以%P结尾的名字:

mysql> select t.* from t_users t where t.title like '%PHP';
+----+------------+--------------+-----------------+
| id | title | author | submission_date |
+----+------------+--------------+-----------------+
| | 学习 PHP | 菜鸟教程 | -- |
+----+------------+--------------+-----------------+
row in set (0.00 sec)

使用“_”匹配 4个字符的名字:

mysql> select t.* from t_users t where t.title like '____';
+----+-------+--------+-----------------+
| id | title | author | submission_date |
+----+-------+--------+-----------------+
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
+----+-------+--------+-----------------+
rows in set (0.00 sec)

5.利用正则表达式REGEXP 和 NOT REGEXP

MySQL提供的其他模式匹配类型是使用扩展的正则表达式,当你使用这个类型来测试一个匹配,
要使用REGEXP和NOT REGEXP操作(或者RLIKE和NOT RLIKE,他们是同意詞)。
下面的列表描述了一些扩展正则表达式的特征:

"."匹配任意单个字符.

[a-z]:单个字符匹配类"[...]"匹配括号里的任意字符,例如,"[abc]"匹配"a","b",或"c",要指定字符范围,可以使用"-",
例如,[a-z] 匹配任意字母,而[0-9]匹配任意数字。

"*" 匹配0个或多个在它前面的东西。例如,"x*" 匹配任意个x字符,"[0-9]*"匹配任意个数字,".*"匹配任意个字符。
一个REGEXP模式匹配成功的条件是,模式在测试值中的任意地方匹配即可。
(这与LIKE模式匹配不同,LIKE模式匹配成功需要匹配整个值)。
"^" 开头作为模式匹配或用
"$" 作为模式的结尾。

为了验证扩展的正则表达式是如何工作的,在这使用REGEXP对前面使用LIKE查询进行重写。

查询以P开头:

mysql> select * from t_users where title regexp '^P';
+----+----------+--------+-----------------+
| id | title | author | submission_date |
+----+----------+--------+-----------------+
| | PHP webs | Doe | -- |
+----+----------+--------+-----------------+
row in set (0.00 sec)

查询以s结尾:

mysql> select * from t_users where title regexp 's$';
+----+----------+--------+-----------------+
| id | title | author | submission_date |
+----+----------+--------+-----------------+
| | PHP webs | Doe | -- |
+----+----------+--------+-----------------+
row in set (0.00 sec)

查询title包含e字符的:

mysql> select * from t_users where title regexp 'e';
+----+------------+----------+-----------------+
| id | title | author | submission_date |
+----+------------+----------+-----------------+
| | PHP webs | Doe | -- |
| | nexJohn | Doe | -- |
| | nexJohn123 | Doe baok | -- |
+----+------------+----------+-----------------+
rows in set (0.00 sec)

利用...匹配查询4个字符的名称:如查询以J开头的 并且是...表示三个字符

如:要找到只包含3个字符的名字,使用“^"和“$"来匹配名字的开头和结尾,且放3个“."在中间

mysql> select * from t_users where title regexp '^J...$';
+----+-------+--------+-----------------+
| id | title | author | submission_date |
+----+-------+--------+-----------------+
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
+----+-------+--------+-----------------+
rows in set (0.00 sec)

利用{n} ;n表示重复次数来替换上面的语句查询:

mysql> select * from t_users where title regexp '^.{4}$';
+----+-------+--------+-----------------+
| id | title | author | submission_date |
+----+-------+--------+-----------------+
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
| | John | Doe | -- |
+----+-------+--------+-----------------+
rows in set (0.00 sec)