[Interview] Database Basics

0, problem outline

1. Basic knowledge

什么叫视图?游标是什么 ?
drop、delete与truncate的区别
什么是触发器?
CURD是什么?C是什么?
MySQL有哪些类型?
int和varchar;int占多少位?

2. Basic operation

left Join、right join、inner join、outer join的区别?(*4
数据库去重操作;
SQL的group by、order by作用;having和where区别?

Third, the paradigm

数据库第三大范式(*5)之间的理解和比较;满足此条件设计一个数据库。
设计学生成绩管理系统,符合第三范式要求,并绘出UML图。

Four, affairs

1、数据库事务(*2)含义?

2、ACID(*2

3、数据库隔离级别(*7

4、每个级别如何解决上个级别的问题,每个隔离级别优缺点,如何解决?哪些导致脏读(*2),如何预防脏读?哪些不可重复读(*2),哪些导致幻读(*2)?
 - 追问1:MySQL如何解决幻读(*2)问题的?
 - 追问2:底层原理(*2

 5、如何保证一致性。

1. Basic knowledge

1. What is a view? What is a cursor?

view:

  • 1) A view is a result set composed of multiple basic tables according to certain conditions. Generally speaking, it can only be selected, and operations such as update, insert, and delete cannot be performed because it is a virtual memory table.

  • 2) View VS table <==> Shortcut VS file

Cursor: Data buffer, storing SQL statement execution results, from which users can obtain records one by one for processing.

2. The difference between drop, delete and truncate

3. What is a trigger?

4. What is CURD? What is C?

Create-increase , Retrieve-read, Update-update, Delete-delete.

5. What are the types of MySQL?

Mainly divided into: numeric type, date and time and string type.
1
note:

  • The unsigned upper limit is twice the original value; precise digital scenes such as currency precision must use the DECIMAL type;
  • Time classification: year, month, day-DATE, year, month, day, hour, minute and second-DATETIME, hour, minute, second-TIME; TIMESTAMP is related to time zone.
  • String: CHAR is fixed to indicate the length when it is created, and VARCHAR is a variable-length string.

6. Int and varchar; how many digits does int occupy?

Types of size Signed range Unsigned range
int 4 bytes − 2 32 -2^{32} 232 ~ 2 31 − 1 2^{31} − 1 2311 0 0 0 ~ 2 32 − 1 2^{32} − 1 2321
varchar(M) 0~65536 bytes 0~M byte variable length character string

2. Basic operation

1、left Join、right join、inner join、outer join的区别?(*4)

Form A:

aId aName
1 a1
4 a4

Form B:

bId bName
1 b1
5 b5
# 关联查询语句
select * from A (**) join B  on A.aID = B.bID;
A: left
B: right
C: inner
D: outer

Query results
(1) A: left join

aId aName bId bName
1 a1 1 b1
4 a4 null null

(2) B: right join

aId aName bId bName
1 a1 1 b1
null null 5 b5

(3) C: inner join

aId aName bId bName
1 a1 1 b1

(4) D: outer join

aId aName bId bName
1 a1 1 b1
4 a4 null null
null null 5 b5

2. Database deduplication operation;

  • 1) rowid: Oracle commonly used, other fields need to be declared.
# 删除表1和表2 中name相同的所有数据
delete from table1 a
where rowid !=( select max(rowid) from table2 b
				where  a.name1 = b.name1 and  a.name2 = b.name2 ……
			  )
  • 2) group by: mainly used for grouping statistics, generally used in aggregate functions
# 删除表中num列所有重复的数据
delete from student
Group by num
having count(num)>1
  • 3) distinct: generally used for relatively small tables for deduplication and filter out redundant duplicate records
select distinct name from student

3. The role of SQL group by and order by; the difference between having and where?

1)group by VS order by

  • Function: group by: grouping, often used in combination with aggregate functions; order by: sorting;

2)having VS where

  • Object: where acts on the base table or view, from which to select tuples that meet the conditions; having acts on the group, from which selects the group that meets the conditions.

Third, the paradigm

1. Understanding and comparison between the third paradigm of database (*5); design a database to meet this condition;

1NF Inseparable
2NF Unique identification, completely dependent on the primary key
3NF No transitive dependency

……

2. Design the student achievement management system to meet the requirements of the third paradigm, and draw UML diagrams.

……

Four, affairs

1. What does database transaction (*2) mean?

The inseparable database operation sequence of database transactions is also the basic unit of database concurrency control. The execution result must change the database from a consistent state to another consistent state.

Added: MySQL transaction support

Storage engine Does it support
MyISAM Not supported, read only
InnoDB Support, ACID, row-level lock, concurrency
Berkeley DB stand by

2、ACID(*2)

Atomicity Atomicity
Consistency consistency
Isolation Isolation
Durability Endurance

3. Database isolation level (*7)

读未提交:我能读到别人没提交事务的数据。
读已提交:我事务中读到的数据,别人能改。
可重复读:我事务中读到的数据,别人能插。
串行:我的事务没提交,别人就别想动数据。

这4种隔离级别,并行性能依次降低,安全性依次提高。

随着事务中记录开放程度紧缩,安全级别随之提升。
Isolation level

4、每个级别如何解决上个级别的问题,每个隔离级别优缺点,如何解决?哪些导致脏读(*2),如何预防脏读?哪些不可重复读(*2),哪些导致幻读(*2)?

类型 定义 优点 问题
Read Uncommitted 允许读另一个还没commit的数据 提高性能 脏读
Read Committed 只允许已经事务中commit的记录可见 解决脏读 不可重复读
Repeable Read 直到事务commit或rollback才可见,但其他事务insert/delete对该事务可见 可重复读 幻读
Serializable 只允许事务串行执行 最高级别 代价大
追问1:MySQL如何解决幻读(*2)问题的?

1、多版本并发控制(MVCC)、next-key 锁、序列化级别 ……

2、更多可看【面试】数据库进阶篇(二)

追问2:底层原理(*2)

……

5、如何保证一致性。

数据库层面:MVCC–RR、日志、2PL;应用层面:……

参考

【数据类型】
1、Mysql数据库中有哪些数据类型?
2、Mysql都支持哪些数据类型?
3、MySQL 数据类型

[Basic operation]
1. Talking about the usage and difference between group by and order by
2. Mysql: Illustrate the difference between inner join, left join, right join, full outer join, union, union all
3. Several methods of deduplication of database Summary
4. Simple solution for MySQL database deduplication

[Transactions]
1. How does mysql ensure atomicity, consistency, and durability?
2. Understanding the 4 isolation levels of the database.
3. The third interview/written test-a collection of database interview questions.
4. Detailed explanations, examples and solutions of mysql phantom reading Method
5. How does MySQL solve the phantom reading?
6. 20 | What is phantom reading and what is wrong with phantom reading?

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/111590654