学习MySQL官⽅⽂档(⼀)
前提:看这篇⽂档之前我已经装了⼀台虚拟机,并在虚拟机上装好了mysql服务,还装了Navicat并连接上虚拟机的mysql服务
1、连接到服务器和从服务器断开连接
shell> mysql -h host -u user -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25338 to server version: 5.7.29-standard
Type'help;' or '\h'for help.Type'\c' to clear the buffer.
mysql>
host是要连接的服务器的ip,如果是在运⾏MySQL的同⼀台计算机上登录,则可以省略主机,只需使⽤以下命令:
shell> mysql -u user -p
如果在尝试登录时收到诸如ERROR 2002(HY000)之类的错误消息 :⽆法通过套接字’/tmp/mysql.sock’(2)连接到本地MySQL服务器,则意味着该MySQL服务器守护程序(Unix)或服务(Windows)未运⾏。
从服务器断开连接
mysql> QUIT
Bye
2、使⽤⼏种查询来熟悉mysql的⼯作⽅式
mysql> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION()| CURRENT_DATE |
+-----------+--------------+
| 5.7.28    | 2019-10-22  |
+-----------+--------------+
1 row in set(0.01 sec)
mysql> SELECT SIN(PI()/4),(4+1)*5;
+--------------------+---------+
| SIN(PI()/4)|(4+1)*5 |
+--------------------+---------+
| 0.7071067811865475 |      25 |
+--------------------+---------+
1 row in set(0.00 sec)
3、创建和使⽤数据库
查询服务器上有哪些数据库:
mysql> SHOW DATABASES;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| menagerie          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set(0.00 sec)
切换数据库
mysql> use menagerie
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
如果没有权限访问数据库,则需要向管理员申请,管理员需要执⾏语句:
mysql> GRANT ALL ON menagerie.* TO 'your_mysql_name'@'your_client_host';
其中your_mysql_name是分配给你的MySQL⽤户名,your_client_host是连接服务器的主机。创建数据库:
mysql> CREATE DATABASE menagerie;
每次登陆mysql服务器之后都需要切换到需要使⽤的数据库,也可以在登录语句中直接添加库:
shell> mysql -h host -u user -p menagerie
Enter password: ********
查看当前选择的数据库:
mysql>  SELECT DATABASE()
-> ;
+------------+
| DATABASE()|
+------------+
| menagerie  |
+------------+
1 row in set(0.00 sec)
4、查看数据库中的表:
mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| pet                |
+---------------------+
1 row in set(0.00 sec)
创建表:
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
查看表结构:
mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field  |Type| Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| NAME    | varchar(20)| YES  || NULL    ||
| OWNER  | varchar(20)| YES  || NULL    ||
| species | varchar(20)| YES  || NULL    ||
| sex    | char(1)| YES  || NULL    ||
| birth  | date        | YES  || NULL    ||
| death  | date        | YES  || NULL    ||
+---------+-------------+------+-----+---------+-------+
6 rows in set(0.00 sec)
5、向表中添加数据: LOAD DATA和 INSERT两种⽅式
⽅式⼀:创建⼀个⽂本⽂件 ,每⾏包含⼀个记录,其值由制表符分隔,并按CREATE TABLE语句中列的列出顺序给出,然后将⽂本⽂件加载到 pet表中:
mysql> LOAD DATA LOCAL INFILE '/' INTO TABLE pet;
⽅式⼆:使⽤如下INSERT语句添加新记录 :
mysql> INSERT INTO pet
VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
6、从表中检索数据
SELECT语句⽤于从表中提取信息。该语句的⼀般形式为:
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
what_to_select指⽰您要查看的内容(列表,也可以⽤ * 可以表⽰“ 所有列”)。which_table表⽰表名。WHERE ⼦句是可选的。如果存在,则 conditions_to_satisfy指定⾏必须满⾜的⼀个或多个条件才有资格进⾏检索。
查询所有数据:
mysql>SELECT*FROM pet;
UPDATE语句修复错误的记录 :
mysql>UPDATE pet SET birth ='1989-08-31'WHERE name ='Bowser';
带条件的基本查询:
mysql>SELECT*FROM pet WHERE name ='Bowser';
字符串⽐较通常不区分⼤⼩写,因此您可以将名称指定为’bowser’, 'BOWSER’等等。查询结果相同
使⽤AND 逻辑运算符组合条件查询:
mysql>SELECT*FROM pet WHERE species ='dog'AND sex ='f';
使⽤OR 逻辑运算符组合条件查询:
mysql>SELECT*FROM pet WHERE species ='snake'OR species ='bird';
AND并且 OR可以混合使⽤,AND优先级⾼于 OR。如果同时使⽤这两个运算符,则最好使⽤括号来明确指⽰应如何对条件进⾏分组:
mysql>SELECT*FROM pet WHERE(species ='cat'AND sex ='m')
OR(species ='dog'AND sex ='f');
查询指定列:
mysql>SELECT name, birth FROM pet;
去重查询列
mysql>SELECT DISTINCT owner FROM pet;
带条件查询列:
mysql>SELECT name, species, birth FROM pet
WHERE species ='dog'OR species ='cat';
排序查询:
mysql>SELECT name, birth FROM pet ORDER BY birth;
注意:在字符类型列上的排序通常以不区分⼤⼩写的⽅式执⾏,所以如果想区分⼤⼩写排序,可以这样:ORDER BY BINARY col_name
默认的排序顺序是升序,如果要降序排序,请将DESC关键字添加到要排序 的列的名称上:
mysql>SELECT name, birth FROM pet ORDER BY birth DESC;
对多个列进⾏排序,同时也可以对不同的列采⽤不同的排序⽅式(升或降),⽐如:
mysql>SELECT name, species, birth FROM pet
ORDER BY species, birth DESC;
DESC关键字仅适⽤于列名紧接其前(birth); 它不会影响species列的排序顺序(没写默认是ASC)。
⽇期计算:
TIMESTAMPDIFF()功能:求两个⽇期的差值。它的参数是要表⽰结果的单位,以及两个⽇期。以下查询为每只宠物显⽰出⽣⽇期,当前⽇期和年龄(以岁为单位)。 别名(age)是⽤来表⽰输出列标签。CURDATE()表⽰当前⽇期。
mysql>SELECT name, birth, CURDATE(),
TIMESTAMPDIFF(YEAR,birth,CURDATE())AS age
FROM pet;
还可以添加排序:
mysql>SELECT name, birth, CURDATE(),
TIMESTAMPDIFF(YEAR,birth,CURDATE())AS age
FROM pet ORDER BY name;
查询使⽤death IS NOT NULL⽽不是,death <> NULL,因为NULL是⼀个特殊值,⽆法使⽤常规⽐较运算符进⾏⽐较:
mysql>SELECT name, birth, death,
TIMESTAMPDIFF(YEAR,birth,death)AS age
FROM pet WHERE death IS NOT NULL ORDER BY age;
MySQL提供了⽤于提取⽇期的很多函数,如 YEAR(), MONTH()和 DAYOFMONTH()。这⾥先看下MONTH()是怎么做的
mysql>SELECT name, birth,MONTH(birth)FROM pet;
+----------+------------+--------------+
| name    | birth      |MONTH(birth)|
+----------+------------+--------------+
| Fluffy  |1993-02-04|2|
mysql存储文档| Claws    |1994-03-17|3|
| Buffy    |1989-05-13|5|
| Fang    |1990-08-27|8|
| Bowser  |1989-08-31|8|
| Chirpy  |1998-09-11|9|
| Whistler |1997-12-09|12|
| Slim    |1996-04-29|4|
| Puffball |1999-03-30|3|
+----------+------------+--------------+
查询5⽉出⽣的宠物:
mysql>SELECT name, birth FROM pet WHERE MONTH(birth)=5;
+-------+------------+
| name  | birth      |
+-------+------------+
| Buffy |1989-05-13|
+-------+------------+
如果当前⽉份为⼗⼆⽉,查出下个⽉⽣⽇的宠物,不能仅在⽉份号(12)上加上⼀个,可以编写⼀个函数查询⽆论当前⽉份是什么都可以使⽤的语句, DATE_ADD()函数可以将时间间隔(参数1)添加到给定的⽇期(参数2),所以sql可以写成:
mysql>SELECT name, birth FROM pet
WHERE MONTH(birth)=MONTH(DATE_ADD(CURDATE(),INTERVAL1MONTH));
另⼀种⽅法: MOD()取余,MOD(MONTH(CURDATE()), 12)返回的是0~11的值,所以要+1,⽐如当
前⽉是12,算出来余数为0,+1就是⼀⽉,是当⽉的下个⽉
mysql>SELECT name, birth FROM pet
WHERE MONTH(birth)=MOD(MONTH(CURDATE()),12)+1;
如果使⽤⽇期⽆效,则计算将失败并产⽣警告:
mysql>SELECT'2018-10-31'+INTERVAL1DAY;
+-------------------------------+
|'2018-10-31'+INTERVAL1DAY|
+-------------------------------+
|2018-11-01|
+-------------------------------+
mysql>SELECT'2018-10-32'+INTERVAL1DAY;
+-------------------------------+
|'2018-10-32'+INTERVAL1DAY|
+-------------------------------+
|NULL|
+-------------------------------+
mysql>SHOW WARNINGS;
+---------+------+----------------------------------------+
|Level| Code | Message                                |
+---------+------+----------------------------------------+
| Warning |1292| Incorrect datetime value: '2018-10-32'|
+---------+------+----------------------------------------+

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。