MySQL基础学习手册,Java大厂高级面试题灵魂100问

MySQL基础学习手册,Java大厂高级面试题灵魂100问,第1张

MySQL基础学习手册,Java大厂高级面试题灵魂100问

2.登录Mysql


(1)查看服务是否开启

[root@a ~]# netstat -tulnp |grep mysql

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

2208/mysqld

(2)安全安装mysql

[root@a ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS script IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current

password for the root user. If you’ve just installed MariaDB, and

you haven’t set the root password yet, the password will be blank,

so you should just press enter here.

Enter current password for root (enter for none):

OK, successfully used password, moving on…

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables…

… Success!

…此处省略安装内容…

(3)登录mysql

[root@a ~]# mysql -uroot -pa

Welcome to the MariaDB monitor. Commands end with ; or g.

Your MariaDB connection id is 16

Server version: 5.5.64-MariaDB MariaDB Server

Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

MariaDB [(none)]>

3.MySQL建,查,选,删库


1.mysql命令规则:要在最后加上;(英文输入法分号)表示命令结束

2.在linux中用mysql可以用tab键补全

**3.mysql内命令不区分大小写,比如CREATE=create

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

(1)创建数据库create

[root@a ~]# mysqladmin -uroot -pa create test2

MariaDB [(none)]> create database test1;

Query OK, 1 row affected (0.00 sec)

(2)查询数据库show

MariaDB [(none)]> show databases;

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| test1 |

| test2 |

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

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值


《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享


_**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 的自动提交模式:

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5687666.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存