今天更新 SQL 高频面试题英语版。音频点击下方获取。
【SQL】SQL 高频面试题英语版(1)
【SQL】SQL 高频面试题英语版(2)
A database is an organized collection of data, stored and retrieved digitally from a remote or local computer system. Databases can be vast and complex, and such databases are developed using fixed design and modeling approaches.
数据库是有组织的数据集合,从远程或本地计算机系统以数字方式存储和检索。数据库可能庞大而复杂,并且此类数据库是使用固定设计和建模方法开发的。
DBMS stands for Database Management System. DBMS is a system software responsible for the creation, retrieval, updation, and management of the database. It ensures that our data is consistent, organized, and is easily accessible by serving as an interface between the database and its end-users or application software.
DBMS 代表数据库管理系统。DBMS 是一个系统软件,负责数据库的创建、检索、更新和管理。它通过充当数据库与其最终用户或应用程序软件之间的接口,确保我们的数据是一致的、有组织的并且易于访问。
RDBMS stands for Relational Database Management System. The key difference here, compared to DBMS, is that RDBMS stores data in the form of a collection of tables, and relations can be defined between the common fields of these tables. Most modern database management systems like MySQL, Microsoft SQL Server, Oracle, IBM DB2, and Amazon Redshift are based on RDBMS.
RDBMS 代表关系数据库管理系统。与 DBMS 相比,这里的关键区别在于 RDBMS 以表集合的形式存储数据,并且可以在这些表的公共字段之间定义关系。大多数现代数据库管理系统(如 MySQL、Microsoft SQL Server、Oracle、IBM DB2 和 Amazon Redshift)都基于 RDBMS。
SQL stands for Structured Query Language. It is the standard language for relational database management systems. It is especially useful in handling organized data comprised of entities (variables) and relations between different entities of the data.
SQL 代表结构化查询语言。它是关系数据库管理系统的标准语言。它在处理由实体(变量)和数据的不同实体之间的关系组成的有组织的数据时特别有用。
SQL is a standard language for retrieving and manipulating structured databases. On the contrary, MySQL is a relational database management system, like SQL Server, Oracle or IBM DB2, that is used to manage SQL databases.
SQL 是用于检索和操作结构化数据库的标准语言。相反,MySQL 是一个关系数据库管理系统,如 SQL Server、Oracle 或 IBM DB2,用于管理 SQL 数据库。
A table is an organized collection of data stored in the form of rows and columns. Columns can be categorized as vertical and rows as horizontal. The columns in a table are called fields while the rows can be referred to as records.
表是以行和列的形式存储的有组织的数据集合。列可以分类为垂直,行可以分类为水平。表中的列称为字段,而行可以称为记录。
Constraints are used to specify the rules concerning data in the table. It can be applied for single or multiple fields in an SQL table during the creation of the table or after creating using the ALTER TABLE command. The constraints are:
约束用于指定有关表中数据的规则。它可以在创建表期间或使用 ALTER TABLE 命令创建后应用于 SQL 表中的单个或多个字段。约束是:
The PRIMARY KEY constraint uniquely identifies each row in a table. It must contain UNIQUE values and has an implicit NOT NULL constraint.
A table in SQL is strictly restricted to have one and only one primary key, which is comprised of single or multiple fields (columns).
PRIMARY KEY 约束唯一标识表中的每一行。它必须包含 UNIQUE 值并具有隐式 NOT NULL 约束。
SQL 中的表被严格限制为只有一个主键,主键由单个或多个字段(列)组成。
CREATE TABLE Students ( /* Create table with a single field as primary key */
ID INT NOT NULL
Name VARCHAR(255)
PRIMARY KEY (ID)
);
CREATE TABLE Students ( /* Create table with multiple fields as primary key */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL,
CONSTRAINT PK_Student
PRIMARY KEY (ID, FirstName)
);
ALTER TABLE Students /* Set a column as primary key */
ADD PRIMARY KEY (ID);
ALTER TABLE Students /* Set multiple columns as primary key */
ADD CONSTRAINT PK_Student /*Naming a Primary Key*/
PRIMARY KEY (ID, FirstName);
A UNIQUE constraint ensures that all values in a column are different. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined per table. The code syntax for UNIQUE is quite similar to that of PRIMARY KEY and can be used interchangeably.
UNIQUE 约束确保列中的所有值都不同。这为列提供了唯一性,并有助于唯一地标识每一行。与主键不同,每个表可以定义多个唯一约束。UNIQUE 的代码语法与 PRIMARY KEY 的代码语法非常相似,可以互换使用。
CREATE TABLE Students ( /* Create table with a single field as unique */
ID INT NOT NULL UNIQUE
Name VARCHAR(255)
);
CREATE TABLE Students ( /* Create table with multiple fields as unique */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL
CONSTRAINT PK_Student
UNIQUE (ID, FirstName)
);
ALTER TABLE Students /* Set a column as unique */
ADD UNIQUE (ID);
ALTER TABLE Students /* Set multiple columns as unique */
ADD CONSTRAINT PK_Student /* Naming a unique constraint */
UNIQUE (ID, FirstName);
A FOREIGN KEY comprises of single or collection of fields in a table that essentially refers to the PRIMARY KEY in another table. Foreign key constraint ensures referential integrity in the relation between two tables.
The table with the foreign key constraint is labeled as the child table, and the table containing the candidate key is labeled as the referenced or parent table.
FOREIGN KEY 由表中的单个字段或字段集合组成,这些字段本质上是指另一个表中的 PRIMARY KEY。外键约束确保两个表之间关系的参照完整性。
具有外键约束的表被标记为子表,包含候选键的表被标记为引用表或父表。
CREATE TABLE Students ( /* Create table with foreign key - Way 1 */
ID INT NOT NULL
Name VARCHAR(255)
LibraryID INT
PRIMARY KEY (ID)
FOREIGN KEY (Library_ID) REFERENCES Library(LibraryID)
);
CREATE TABLE Students ( /* Create table with foreign key - Way 2 */
ID INT NOT NULL PRIMARY KEY
Name VARCHAR(255)
LibraryID INT FOREIGN KEY (Library_ID) REFERENCES Library(LibraryID)
);
ALTER TABLE Students /* Add a new foreign key */
ADD FOREIGN KEY (LibraryID)
REFERENCES Library (LibraryID);
The SQL Join clause is used to combine records (rows) from two or more tables in a SQL database based on a related column between the two.
SQL Join 子句用于根据两者之间的相关列组合来自 SQL 数据库中两个或多个表的记录(行)。
There are four different types of JOINs in SQL:
SQL 中有四种不同类型的 JOIN:
A self JOIN is a case of regular join where a table is joined to itself based on some relation between its own column(s). Self-join uses the INNER JOIN or LEFT JOIN clause and a table alias is used to assign different names to the table within the query.
自联接是常规联接的一种情况,其中表根据其自身列之间的某种关系连接到自身。自联接使用 INNER JOIN 或 LEFT JOIN 子句,表别名用于为查询中的表分配不同的名称。
Cross join can be defined as a cartesian product of the two tables included in the join. The table after join contains the same number of rows as in the cross-product of the number of rows in the two tables. If a WHERE clause is used in cross join then the query will work like an INNER JOIN.
交叉连接可以定义为连接中包含的两个表的笛卡尔积。连接后的表包含与两个表中行数的叉积相同的行数。如果在交叉连接中使用 WHERE 子句,则查询将像 INNER JOIN 一样工作。
A database index is a data structure that provides a quick lookup of data in a column or columns of a table. It enhances the speed of operations accessing data from a database table at the cost of additional writes and memory to maintain the index data structure.
数据库索引是一种数据结构,可以快速查找表的一列或多列中的数据。它提高了从数据库表访问数据的操作速度,代价是额外的写入和内存以维护索引数据结构。
CREATE INDEX index_name /* Create Index */
ON table_name (column_1, column_2);
DROP INDEX index_name; /* Drop Index */
There are different types of indexes that can be created for different purposes:
可以为不同目的创建不同类型的索引:
Unique indexes are indexes that help maintain data integrity by ensuring that no two rows of data in a table have identical key values. Once a unique index has been defined for a table, uniqueness is enforced whenever keys are added or changed within the index.
唯一索引是通过确保表中没有两行数据具有相同键值来帮助维护数据完整性的索引。一旦为表定义了唯一索引,只要在索引中添加或更改键,就会强制执行唯一性。
CREATE UNIQUE INDEX myIndex
ON students (enroll_no);
Non-unique indexes, on the other hand, are not used to enforce constraints on the tables with which they are associated. Instead, non-unique indexes are used solely to improve query performance by maintaining a sorted order of data values that are used frequently.
另一方面,非唯一索引不用于对与其关联的表实施约束。相反,非唯一索引仅用于通过维护经常使用的数据值的排序顺序来提高查询性能。
Clustered indexes are indexes whose order of the rows in the database corresponds to the order of the rows in the index. This is why only one clustered index can exist in a given table, whereas, multiple non-clustered indexes can exist in the table.
The only difference between clustered and non-clustered indexes is that the database manager attempts to keep the data in the database in the same order as the corresponding keys appear in the clustered index.
Clustering indexes can improve the performance of most query operations because they provide a linear-access path to data stored in the database.
聚集索引是数据库中行的顺序与索引中的行的顺序相对应的索引。这就是为什么给定的表中只能存在一个聚集索引,而表中可以存在多个非聚集索引。
聚簇索引和非聚簇索引之间的唯一区别是数据库管理器尝试将数据库中的数据保持在与相应键出现在聚簇索引中相同的顺序。
集群索引可以提高大多数查询操作的性能,因为它们为存储在数据库中的数据提供了线性访问路径。
As explained above, the differences can be broken down into three small factors -
如上所述,差异可以分为三个小因素 -
Data Integrity is the assurance of accuracy and consistency of data over its entire life-cycle and is a critical aspect of the design, implementation, and usage of any system which stores, processes, or retrieves data. It also defines integrity constraints to enforce business rules on the data when it is entered into an application or a database.
数据完整性是数据在其整个生命周期内的准确性和一致性的保证,并且是任何存储、处理或检索数据的系统的设计、实施和使用的关键方面。它还定义了完整性约束,以便在将数据输入应用程序或数据库时对数据实施业务规则。
A query is a request for data or information from a database table or combination of tables. A database query can be either a select query or an action query.
查询是对来自数据库表或表组合的数据或信息的请求。数据库查询可以是选择查询或操作查询。
A subquery is a query within another query, also known as a nested query or inner query. It is used to restrict or enhance the data to be queried by the main query, thus restricting or enhancing the output of the main query respectively. For example, here we fetch the contact information for students who have enrolled for the maths subject:
子查询是另一个查询中的查询,也称为嵌套查询或内部查询。用于限制或增强主查询要查询的数据,从而分别限制或增强主查询的输出。例如,在这里我们获取注册数学科目的学生的联系信息:
SELECT name, email, mob, address
FROM myDb.contacts
WHERE roll_no IN (
SELECT roll_no
FROM myDb.students
WHERE subject = 'Maths');
There are two types of subquery - Correlated and Non-Correlated.
有两种类型的子查询 - 关联和非关联。
-- 关联子查询
select d.*
from dept d
where exists (
select 1
from emp e
where e.deptno = d.deptno
);
-- 非关联子查询
select d.*
from dept d
where d.deptno = (select max(e.deptno) from emp e);
SELECT operator in SQL is used to select data from a database. The data returned is stored in a result table, called the result-set.
SQL 中的 SELECT 运算符用于从数据库中选择数据。返回的数据存储在结果表中,称为结果集。
Some common SQL clauses used in conjuction with a SELECT query are as follows:
与 SELECT 查询结合使用的一些常见 SQL 子句如下:
SELECT *
FROM myDB.students
WHERE graduation_year = 2019
ORDER BY studentID DESC;
SELECT COUNT(studentId), country
FROM myDB.students
WHERE country != "INDIA"
GROUP BY country
HAVING COUNT(studentID) > 5;