MySQL基础学习手册

±-------------------+

5 rows in set (0.00 sec)

(3)选择数据库use

MariaDB [(none)]> use test1;

Database changed

MariaDB [test1]> use test2;

Database changed

MariaDB [test2]> use mysql;

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

(4)删除数据库

在操作中会有不少的错误,学会看报错信息是个解决问题的好方法,如下根据提示(for the right syntax to use near ‘test2’ at line 1)发现test2附近语法错误,检查后发现没有写databases,加上后便可以解决!

[root@a ~]# mysqladmin -uroot -pa drop test1

Dropping the database is potentially a very bad thing to do.

Any data stored in the database will be destroyed.

Do you really want to drop the ‘test1’ database [y/N] y

Database “test1” dropped

MariaDB [mysql]> drop test2;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘test2’ at line 1

MariaDB [mysql]> drop database test2;

Query OK, 0 rows affected (0.01 sec)

4.MySQL 数据类型


_**1.经常变化的字段用 varchar

2.知道固定长度的用 char

3.尽量用 varchar

4.超过 255 字符的只能用 varchar 或者 text

5.varchar 的地方不用 text

6.使用varchar必须带范围!**_

(1)数值类型放图了,可在菜鸟教程找到

在这里插入图片描述

(2)日期和时间类型

在这里插入图片描述(3)字符串类型

在这里插入图片描述

5.建、查、删表


1.建立、查询、删除表要在进入数据库的前提下进行操作

2.数据类型后面不填的话默认是最高位

3.如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。

4.delete 和 truncate 仅仅删除表数据,drop 连表数据和表结构一起删除,打个比方,delete 是单杀,truncate 是团灭,drop 是把电脑摔了。

5.使用varchar必须带范围!

6.表名不能是mysql中数据类型等关键字。

(1)建表

MariaDB [test1]> create table book(name varchar(255),price int,date date);

Query OK, 0 rows affected (0.01 sec)

两种方式都可以建表,看个人习惯!

MariaDB [test1]> create table book(

-> name varchar(255),

-> price int(11),

-> date date)

-> ;

ERROR 1050 (42S01): Table ‘book’ already exists

(2)查表

1.在数据库查看表

MariaDB [test1]> show tables;

±----------------+

| Tables_in_test1 |

±----------------+

| book |

±----------------+

1 row in set (0.00 sec)

2.查看表结构

MariaDB [test1]> desc book;

±------±--------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±--------±-----±----±--------±------+

| name | char(1) | YES | | NULL | |

| price | int(11) | YES | | NULL | |

| date | date | YES | | NULL | |

±------±--------±-----±----±--------±------+

3 rows in set (0.00 sec)

(3)删除表

MariaDB [test1]> drop table book;

Query OK, 0 rows affected (0.00 sec)

6.插入数据


**1.添加数据的时候可以规定列进行添加。

如果所有的列都要添加数据可以不规定列进行添加数据:**

MariaDB [test1]> insert into book(name,price,date) values (“a”,“8”,“2020-04-14”);

Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO runoob_tbl

-> VALUES

-> (0, “JAVA 教程”, “RUNOOB.COM”, ‘2016-05-06’);

2.INSERT 插入多条数据

INSERT INTO table_name (field1, field2,…fieldN) VALUES (valueA1,valueA2,…valueAN),(valueB1,valueB2,…valueBN),(valueC1,valueC2,…valueCN)…;

MariaDB [test1]> insert into book values(“水浒”,“88”,“2020-04-13”),(“红楼梦”,“99”,“2020-04-13”),(“西游记”,“100”,“2020-04-13”);

Query OK, 3 rows affected, 3 warnings (0.00 sec)

Records: 3 Duplicates: 0 Warnings: 3

二、Mysql的数据查询

===========================================================================

(1).不显示中文内容是因为字符编码搞的鬼,设置下就可以了

•vim /etc/my.cnf.d/client.cnf 文件,添加如下内容

[client]

default-character-set=utf8

•vim /etc/my.cnf.d/mysql-clients.cnf文件,添加如下内容

[mysql]

default-character-set=utf8

•vim /etc/my.cnf 文件,添加如下内容

[mysqld]

character-set-server=utf8

default-storage-engine=INNODB

•重启服务

systemctl restart mariadb

1.查询表内数据


1.SQL中用select来查看数据,select后面跟是要查看表格的列头,查几个跟几个列头,如果都要查可以用通配符,代表所有的意思。

2.as可以对查询的内容进行重命名

MariaDB [test1]> select * from book;

±-----±------±-----------+

| name | price | date |

±-----±------±-----------+

| ? | 88 | 2020-04-13 |

| ? | 99 | 2020-04-13 |

| ? | 100 | 2020-04-13 |

±-----±------±-----------+

6 rows in set (0.00 sec)

更改后

MariaDB [test1]> select * from book;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

±----------±------±-----------+

3 rows in set (0.00 sec)

2.where子句


1.如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中。

2.你可以使用 AND 或者 OR 指定一个或多个条件。

3.查询语句中你可以使用一个或者多个表,表之间使用逗号, 分割,并使用WHERE语句来设定查询条件。

在这里插入图片描述

MariaDB [test1]> select * from book where name=“水浒”;

±-------±------±-----------+

| name | price | date |

±-------±------±-----------+

| 水浒 | 88 | 2020-04-13 |

±-------±------±-----------+

1 row in set (0.00 sec)

MariaDB [test1]> select * from book where price=100 and date=“2020-04-13”;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 西游记 | 100 | 2020-04-13 |

±----------±------±-----------+

1 row in set (0.00 sec)

MariaDB [test1]> select * from book where price=100 or date=“2020-04-13”;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

±----------±------±-----------+

3 rows in set (0.00 sec)

3.UPDATE 更新数据


_**1.你可以同时更新一个或多个字段。

2.你可以在一个单独表中同时更新数据。

3.你可以在 WHERE 子句中指定任何条件。**_

MariaDB [test1]> update book set name=“水浒传” where price=88;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [test1]> select * from book;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒传 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

±----------±------±-----------+

3 rows in set (0.00 sec)

4.DELETE 删除数据


_**1.如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。

2.你可以在 WHERE 子句中指定任何条件

3.您可以在单个表中一次性删除记录。

4.delete 和 truncate 仅仅删除表数据,drop 连表数据和表结构一起删除,打个比方,delete 是单杀,truncate 是团灭,drop 是把电脑摔了。**_

MariaDB [a]> select * from book;

±-----±------±-----------+

| name | price | date |

±-----±------±-----------+

| ? | 66 | 2020-04-13 |

| ? | 88 | 2020-04-13 |

| ? | 99 | 2020-04-13 |

| ? | 100 | 2020-04-13 |

| ? | 88 | 2020-04-13 |

| a | 8 | 2020-04-14 |

±-----±------±-----------+

6 rows in set (0.00 sec)

MariaDB [a]> delete from book where price=8;

Query OK, 1 row affected (0.00 sec)

MariaDB [a]> select * from book;

±-----±------±-----------+

| name | price | date |

±-----±------±-----------+

| ? | 66 | 2020-04-13 |

| ? | 88 | 2020-04-13 |

| ? | 99 | 2020-04-13 |

| ? | 100 | 2020-04-13 |

| ? | 88 | 2020-04-13 |

±-----±------±-----------+

5 rows in set (0.00 sec)

MariaDB [a]> delete from book;

Query OK, 5 rows affected (0.00 sec)

MariaDB [a]> select * from book;

Empty set (0.00 sec)

5.LIKE 子句


_**1.like 匹配/模糊匹配,会与 % 和 _ 结合使用。

2.在 where like 的条件查询中,SQL 提供了四种匹配方式。

%:表示任意 0 个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。

_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句。

[]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。

[^] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。

查询内容包含通配符时,由于通配符的缘故,导致我们查询特殊字符 “%”、“”、“[” 的语句无法正常实现,而把特殊字符用 “[ ]” 括起便可正常查询。**

MariaDB [test1]> select * from book where name like ‘水%’;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒传 | 88 | 2020-04-13 |

±----------±------±-----------+

1 row in set (0.00 sec)

MariaDB [test1]> select * from book where name like ‘%%’;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒传 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

±----------±------±-----------+

3 rows in set (0.00 sec)

MariaDB [test1]> select * from book where name like ‘’;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 红楼梦 | 99 | 2020-04-13 |

±----------±------±-----------+

1 row in set (0.00 sec)

6.排序


MariaDB [test1]> select * from book order by price desc;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 西游记 | 100 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 水浒传 | 88 | 2020-04-13 |

±----------±------±-----------+

3 rows in set (0.00 sec)

MariaDB [test1]> select * from book order by price asc;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒传 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

±----------±------±-----------+

3 rows in set (0.00 sec)

7.分组


_**1.GROUP BY 语句根据一个或多个列对结果集进行分组。

常和 COUNT, SUM, AVG,等函数一起使用。**_

2.GROUP by 的用法很多要多加练习!

3.as可以对查询的内容进行重命名

4.WITH ROLLUP(rollup的意思就是归纳!) 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。其中记录 NULL 表示所有人的登录次数。我们可以使用 coalesce 来设置一个可以取代 NUll 的名称select coalesce(a,b,c);

MariaDB [test1]> select * from book;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒传 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

| 水浒传 | 90 | 2020-01-01 |

±----------±------±-----------+

4 rows in set (0.00 sec)

MariaDB [test1]> select name,count(*) from book group by name;

±----------±---------+

| name | count(*) |

±----------±---------+

| 水浒传 | 2 |

| 红楼梦 | 1 |

| 西游记 | 1 |

±----------±---------+

3 rows in set (0.00 sec)

MariaDB [test1]> select name,sum(price) as total from book group by name with rollup;

±----------±------+

| name | total |

±----------±------+

| 水浒传 | 178 |

| 红楼梦 | 99 |

| 西游记 | 100 |

| NULL | 377 |

±----------±------+

4 rows in set (0.00 sec)

MariaDB [test1]> select coalesce(name,‘total’) as name,sum(price) as total from book group by name with rollup;

±----------±------+

| name | total |

±----------±------+

| 水浒传 | 178 |

| 红楼梦 | 99 |

| 西游记 | 100 |

| total | 377 |

±----------±------+

4 rows in set, 1 warning (0.00 sec)

8.多表查询


_**1. 以上内容比较简单容易理解,但是在真正的应用中经常需要从多个数据表中读取数据。

2. INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。

LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。

RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。**_

3.在这里插入图片描述

4.在这里插入图片描述

5.在这里插入图片描述

MariaDB [test1]> select * from book;

±----------±------±-----------+

| name | price | date |

±----------±------±-----------+

| 水浒传 | 88 | 2020-04-13 |

| 红楼梦 | 99 | 2020-04-13 |

| 西游记 | 100 | 2020-04-13 |

| 水浒传 | 90 | 2020-01-01 |

±----------±------±-----------+

4 rows in set (0.00 sec)

MariaDB [test1]> select * from num;

±----------±---------+

| name | ordernum |

±----------±---------+

| 水浒传 | 1 |

| 红楼梦 | 3 |

| 西游记 | 2 |

±----------±---------+

3 rows in set (0.00 sec)

#内连接

MariaDB [test1]> select book.name,book.price,book.date,num.ordernum from book inner join num where book.name=num.name;

±----------±------±-----------±---------+

| name | price | date | ordernum |

±----------±------±-----------±---------+

| 水浒传 | 88 | 2020-04-13 | 1 |

| 红楼梦 | 99 | 2020-04-13 | 3 |

| 西游记 | 100 | 2020-04-13 | 2 |

| 水浒传 | 90 | 2020-01-01 | 1 |

±----------±------±-----------±---------+

4 rows in set (0.00 sec)

#左连接,左表没有对应的项的情况,会显示null

MariaDB [test1]> select book.name,num.ordernum from book left join num on book.name=num.name;

±-------------±---------+

| name | ordernum |

±-------------±---------+

| 水浒传 | 1 |

| 红楼梦 | 3 |

| 西游记 | 2 |

| 三国演艺 | NULL |

±-------------±---------+

4 rows in set (0.00 sec)

#右连接,以右表为主,右表多出的将不显示

MariaDB [test1]> select book.name,num.ordernum from book right join num on book.name=num.name;

±----------±---------+

| name | ordernum |

±----------±---------+

| 水浒传 | 1 |

| 红楼梦 | 3 |

| 西游记 | 2 |

±----------±---------+

3 rows in set (0.00 sec)

9.处理NULL值


_**1.我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作。

2. IS NULL: 当列的值是 NULL,此运算符返回 true。

IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。

<=>: 比较操作符(不同于 = 运算符),当比较的的两个值相等或者都为 NULL 时返回 true。**_

MariaDB [test1]> create table nulll(name varchar(255) not null, price int);

Query OK, 0 rows affected (0.00 sec)

MariaDB [test1]> insert into nulll values(“吴承恩”,“max”),(“罗贯中”,“maxx”),(“雪芹”,null),(“施耐庵”,null);

Query OK, 4 rows affected, 2 warnings (0.00 sec)

Records: 4 Duplicates: 0 Warnings: 2

MariaDB [test1]> select * from nulll;

±----------±------+

| name | price |

±----------±------+

| 吴承恩 | 0 |

| 罗贯中 | 0 |

| 曹雪芹 | NULL |

| 施耐庵 | NULL |

±----------±------+

4 rows in set (0.00 sec)

#原先的方式无法查看null值

MariaDB [test1]> select * from nulll where price=null;

Empty set (0.00 sec)

#采用is null查看空值对应数据

MariaDB [test1]> select * from nulll where price is null;

±----------±------+

| name | price |

±----------±------+

| 曹雪芹 | NULL |

| 施耐庵 | NULL |

±----------±------+

2 rows in set (0.00 sec)

#采用is not null查看非空值对应数据

MariaDB [test1]> select * from nulll where price is not null;

±----------±------+

| name | price |

±----------±------+

| 吴承恩 | 0 |

| 罗贯中 | 0 |

±----------±------+

2 rows in set (0.00 sec)

10.正则表达式


_**1.MySQL可以通过 LIKE …% 来进行模糊匹配。

2.MySQL 同样也支持其他正则表达式的匹配, MySQL中使用 REGEXP 操作符来进行正则表达式匹配。**_

在这里插入图片描述

#查看name字段以水开头的所有数据

MariaDB [test1]> SELECT name FROM book WHERE name REGEXP ‘^水’;

±----------+

| name |

±----------+

| 水浒传 |

±----------+

1 row in set (0.00 sec)

#查看name字段所有以记结尾的数据

MariaDB [test1]> SELECT name FROM book WHERE name REGEXP ‘记$’;

±----------+

| name |

±----------+

| 西游记 |

±----------+

1 row in set (0.00 sec)

#查看name字段以水开头或者结尾以记结尾的所有数据

MariaDB [test1]> SELECT name FROM book WHERE name REGEXP ‘^水|记$’;

±----------+

| name |

±----------+

| 水浒传 |

| 西游记 |

±----------+

2 rows in set (0.00 sec)

三、Mysql高级操作

==========================================================================

1.Mysql事务


1.MySQL 事务主要用于处理操作量大,复杂度高的数据。

2.一般来说,事务是必须满足4个条件(ACID)::原子性(Atomicity,或称不可分割性)、一致性(Consistency)、隔离性(Isolation,又称独立性)、持久性(Durability)。

原子性:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。

一致性:在事务开始之前和事务结束以后,数据库的完整性没有被破坏。这表示写入的资料必须完全符合所有的预设规则,这包含资料的精确度、串联性以及后续数据库可以自发性地完成预定的工作。

隔离性:数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。事务隔离分为不同级别,包括读未提交(Read uncommitted)、读提交(read committed)、可重复读(repeatable read)和串行化(Serializable)。

持久性:事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。

3.MYSQL 事务处理主要有两种方法:

1、用 BEGIN, ROLLBACK, COMMIT来实现

BEGIN 开始一个事务

ROLLBACK 事务回滚

COMMIT 事务确认

2、直接用 SET 来改变 MySQL 的自动提交模式:

SET AUTOCOMMIT=0 禁止自动提交

SET AUTOCOMMIT=1 开启自动提交

mysql> use RUNOOB;

Database changed

mysql> CREATE TABLE runoob_transaction_test( id int(5)) engine=innodb; # 创建数据表

Query OK, 0 rows affected (0.04 sec)

mysql> select * from runoob_transaction_test;

Empty set (0.01 sec)

mysql> begin; # 开始事务

Query OK, 0 rows affected (0.00 sec)

mysql> insert into runoob_transaction_test value(5);

Query OK, 1 rows affected (0.01 sec)

mysql> insert into runoob_transaction_test value(6);

Query OK, 1 rows affected (0.00 sec)

mysql> commit; # 提交事务

Query OK, 0 rows affected (0.01 sec)

mysql> select * from runoob_transaction_test;

±-----+

| id |

±-----+

| 5 |

| 6 |

±-----+

2 rows in set (0.01 sec)

mysql> begin; # 开始事务

Query OK, 0 rows affected (0.00 sec)

mysql> insert into runoob_transaction_test values(7);

Query OK, 1 rows affected (0.00 sec)

mysql> rollback; # 回滚

Query OK, 0 rows affected (0.00 sec)

mysql> select * from runoob_transaction_test; # 因为回滚所以数据没有插入

±-----+

| id |

±-----+

| 5 |

| 6 |

±-----+

2 rows in set (0.01 sec)

2.alter用法


1.删除,添加或修改表字段

MariaDB [test1]> alter table book add a int;

Query OK, 4 rows affected (0.01 sec)

Records: 4 Duplicates: 0 Warnings: 0

#查看字段信息

MariaDB [test1]> show columns from book;

±------±-------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±-------------±-----±----±--------±------+

| name | varchar(255) | YES | | NULL | |

| price | int(11) | YES | | NULL | |

| date | date | YES | | NULL | |

| a | int(11) | YES | | NULL | |

±------±-------------±-----±----±--------±------+

4 rows in set (0.01 sec)

#用add添加字段

MariaDB [test1]> alter table book add b int;

Query OK, 4 rows affected (0.00 sec)

Records: 4 Duplicates: 0 Warnings: 0

MariaDB [test1]> show columns from book;

±------±-------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±-------------±-----±----±--------±------+

| name | varchar(255) | YES | | NULL | |

| price | int(11) | YES | | NULL | |

| date | date | YES | | NULL | |

| b | int(11) | YES | | NULL | |

最后

各位读者,由于本篇幅度过长,为了避免影响阅读体验,下面我就大概概括了整理了

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
rnings: 0

#查看字段信息

MariaDB [test1]> show columns from book;

±------±-------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±-------------±-----±----±--------±------+

| name | varchar(255) | YES | | NULL | |

| price | int(11) | YES | | NULL | |

| date | date | YES | | NULL | |

| a | int(11) | YES | | NULL | |

±------±-------------±-----±----±--------±------+

4 rows in set (0.01 sec)

#用add添加字段

MariaDB [test1]> alter table book add b int;

Query OK, 4 rows affected (0.00 sec)

Records: 4 Duplicates: 0 Warnings: 0

MariaDB [test1]> show columns from book;

±------±-------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±-------------±-----±----±--------±------+

| name | varchar(255) | YES | | NULL | |

| price | int(11) | YES | | NULL | |

| date | date | YES | | NULL | |

| b | int(11) | YES | | NULL | |

最后

各位读者,由于本篇幅度过长,为了避免影响阅读体验,下面我就大概概括了整理了

[外链图片转存中…(img-5vuSsyUC-1725700278096)]

[外链图片转存中…(img-TXRD7hKs-1725700278097)]

[外链图片转存中…(img-mJQ0xEJO-1725700278097)]

[外链图片转存中…(img-rZATSawr-1725700278097)]

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值