Chapter 4 Intermediate SQL
4.1 Connection expression
SQL provides other forms of join operations, through which the results can include tuples excluded by natural joins.
4.1.1 Connection conditions
SQL is used on
to set common predicates on the relationships that participate in the connection, and is where
similar, but the results are where
different in the outer join operation .
on
The usage is as follows.
select * from student join takes on student.ID = takes.ID;
The above on
conditions indicate that if the attribute of the tuple from student is the same as the attribute of the tuple ID
from takesID
, then they are connected as the resulting tuple.
Although the above result is logically the same as the following statement
select * from student natural join takes;
But the on
statement does not take out duplicate tuples, which means that there will be two ID
attributes in the result relationship , student.ID
andtakes.ID
In fact, the equivalent statement is
select * from student, takes where student.ID = takes.ID;
4.1.2 Outer connection
The introduction of outer joins
Consider the query "find out all students' elective information", if you use the following query statement.
select * from student natural join takes;
At this time, for those students who have not selected courses, that is, there is no corresponding ID in the takes, then the result of this student's course selection will not be displayed, and we hope to set the course selection information as if the student does not choose a course null
. At this time, we have introduced outer joins .
In general, the results of outer joins and inner joins are set on some attributes null
to retain tuples that did not match the results of inner joins.
There are three types of external connections, left external connection, right external connection and full external connection.
- Left outer connection. First make inner joins, and then include the tuples that are not included in the relationship on the left, and set the items in the relationship on the right for these tuples
null
. - Right outer connection. First make inner joins, and then include the tuples that are not included in the right-hand relation, and set the items in the left-hand relation of these tuples
null
. - Full external connection. The union of left outer join and right outer join.
After using the outer join, the query "find out all students' course selection information" can be expressed as follows.
select *
from student left outer join takes;
After using the left outer join, even if there are some students who do not choose courses, they are also included in the result, but the value of their course selection information is set to null
.
Similarly, right outer join and full outer join are expressed as right outer join
sumfull outer join
Note that on and where behave differently on outer joins. In outer joins, on acts on the result of inner joins, that is, it has no effect on tuples that are filled with null and then inserted. But where works on the results obtained after inner connection and null insertion. For example, the query above is equivalent to
select *
from student left outer join takes on student.ID = takes.ID;
But the following query
select *
from student natural left outer join takes
where student.ID = takes.ID;
There is no tuple that contains a null attribute.
4.2 View
4.2.1 View definition
In many cases, we do not want all users to see the logical model of the entire relationship. For security reasons, certain data needs to be hidden from users. Therefore, views are introduced in SQL. The view is defined by a query statement, but it does not contain data in advance, but is calculated by the query statement that defines it during use.
SQL uses create view
to define the view
create view view_name as <query expression>
view_name
Is the name of the view, which <query expression>
is any valid query statement.
For example, you can define a view, "List all the courses offered by the Department of Physics in the fall semester of 2009"
create view physics_fall_2009 as
select course, course_id, sec_id, building, room_number
from section, course
where course.course_id = section.course_id and
course.dept_name = 'Physics' and
section.semester = 'Fall' and
section.year = '2009';
4.2.2 Using views in SQL queries
The view can be used as a relationship, for example, "Find all Physics courses offered in the Waston building in the fall semester of 2009"
select course_id
from physics_fall_2009
where building = 'Watson';
4.2.3 Materialized View
Certain databases allow the storage of view relationships, but they guarantee that if the actual relationship used to define the view is changed, the view is also modified. Such views are called materialized views.
4.2.4 View update
Although views are a useful tool for queries, if we use them to express updates, inserts, or deletes, they can cause serious problems. The difficulty is that the database modification expressed by the view must be translated into the modification of the actual relationship in the database logical model. Therefore, if the view is called updatable , it needs to meet the following restrictions
- There is only one database relationship in the from clause.
- The select clause contains only the attribute name of the relationship, and does not contain any expressions, aggregate functions, and distinct declarations.
- Any attribute that does not appear in the select clause can take a null value.
- The query part does not include group by and having clauses.
4.3 Affairs
A transaction consists of a sequence of query and/or update statements. The SQL standard stipulates that when a SQL statement is executed, a transaction is implicitly opened. One of the following two SQL statements will end a transaction.
- commit work. Commit the current transaction, which means that the updates made by the transaction are persisted in the database. After the transaction is committed, a new transaction starts automatically.
- rollback work. Rolling back the current transaction, that is, undoing all SQL statements in the transaction to update the database. In this way, the database is restored to the state before the first statement of the transaction was executed.
If a transaction has not completed commit work, its impact will be rolled back. During the execution of the transaction, if a breakpoint or system crash occurs, the rollback will be automatically executed when the database is restarted. If the transaction has completed the commit work, its impact cannot be undone by rolling back.
The completion of the transaction is atomic. That is, a transaction either commits its actions after completing all steps, or rolls back all its actions if it cannot complete all its actions.
In many SQL statements, each SQL statement constitutes a transaction by default and is automatically submitted after execution. If a transaction is to complete multiple SQL statements, you need to turn off automatic commit.
4.4 integrity constraints
Integrity constraints ensure that the modification of data by authorized users will not destroy the consistency of the data.
4.4.1 Constraints on a single relationship
Constraints on a single relationship include
- not null. E.g,
name varchar(20) not null
- unique. For example,
unique(Aj1, Aj2, ..., Ajn)
,unique
the attribute group definition element constituting the candidate code. - check(<predicate>). For
check(semester in ('Fall', 'Winter', 'Spring', 'Summer'))
example, .check
Clauses to ensure that the attribute value meets the conditions.
The above three sentences are create table
added on the above.
4.4.2 Affirmation
An assertion is a predicate, which expresses a condition that we hope the database can always meet. Domain constraints and referential integrity constraints are special forms of assertions. The definition of assertion is generally in the following form
create assertion <assertion-name> check <predicate>;
Note that there is not yet a widely used database system that supports the assertion structure.
4.5 SQL data types and modes
4.5.1 Date and time types of SQL
SQL supports date type data, as shown below
date # 日历日期,包括年、月和日,如'2001-04-25'
time # 一天中的时间,包括时、分和秒,如'09:30:00',可以用time(p)来指定秒的小数点后的数字数,用time with time zone来将时区和时间一起存储
timestamp # date和time的组合,如'2001-04-25 10:29:01.45'。可以用变量timestamp(p)来指定秒的小数点后的数字数,用timestamp with time zone来将时区和时间一起存储
We can use case e as t
formal expressions to convert a string e to type t, where t is one data, time, timestamp
of the types, and the string must also conform to the correct format.
We can use to extract a single domain extract (field from d)
from the value d of date
or time
, where the domain can be year, month, day, hour, minute, second
any of them. Time zone information can be extracted with timezone_hour
and timezone_minute
.
There are other functions to get the current date and time. current_date
Returns the current date, current_time
returns the current time (with time zone), and local_time
returns the current local time (without time zone). timestamp is returned by current_timestamp
and local_timestamp
.
4.5.2 Default value
SQL allows default
to specify the default value used in the creation of the table , as shown below
create table student(
...
tot_cred numeric(3, 0) default 0,
...
);
4.5.3 Create Index
Index (index) can help the database to efficiently find those tuples in the relationship that take the given index attribute. For example, if we create an index on the attribute ID of the student relationship, the database system does not need to scan other tuples in the relationship, and can directly find any record with a specified ID value like 22201 or 44553.
Commonly used databases generally use the following statements to define indexes
create index studentID_index on student(ID);
The above statement creates an index named studentID_index on the attribute ID of the student relationship.
4.5.4 Large Object Type
SQL provides large object types for character data clob
and large object data types for binary data blob
, as shown below
book_revoew clob (10kb)
image blob (10MB)
4.5.5 User-defined types
The SQL standard defines usable create type
and create domain
to define types and fields respectively, but these structural forms have not been fully supported by most database implementations.
4.5.6 Extension of create table
SQL provides create table like
support to create a new table based on the structure of a table, as shown below.
create table temp like student;
But the above statement just creates a new table temp with exactly the same structure as the student, and its content is empty.
We can also create a new table based on the query results, as shown below.
create table tl as (
select * from instructor where dept_name = 'music'
)
with data;
If data is not added, no data will be inserted. The attributes of the new table are derived from the attributes of the query results. Of course, you can also specify the attribute name after the name of the new table.
4.6 Authorization
We can restrict users' permissions on the database and provide permissions through authorization. There are two types of authority authorization, data authorization (select, update, insert, delete) and database authorization (allowing users to create, modify, and delete relationships). When the user executes an unauthorized permission, the system will refuse to execute it.
4.6.1 Grant and withdrawal of permissions
Granted use of permissions grant
, as shown below
grant <权限列表>
on <关系名或视图>
to <用户或角色>
E.g
grant select on department to Amit, Satashi;
This statement grants users Amit and Satashi select permission on the department relationship.
Sometimes we hope that the permissions can be specific to some attributes, as shown below
grant update(budget)
on department
to Amit, Satashi;
This statement grants users Amit and Satashi the update permission on the department relationship, but only the budget is updated. We can also specify attributes in granting insert permissions, and the remaining unspecified attributes are either designated as null or designated as default values.
Granting permissions is pulic
equivalent to the authorization of all current users and future users.
We can use the revoke
revocation authority, the usage statement and grant
time are exactly the same, as shown below
revoke update(budget)
on department
to Amit, Satashi;
The permissions granted to a user can also be granted to other users by the user. At this time, the recovery of the permissions will be more complicated.
4.6.2 Role
In SQL, we can grant permissions to roles . At this point, when a new user is created, we directly grant the role to the user, so that the new user directly obtains the permissions that the role has, saving the trouble of separately authorizing the new user.
Therefore, the permissions of a user or role include two parts
- Permission granted directly.
- The permissions brought by the granted role.
Create a role to use create role
, as shown below
create role instructor;
Then we can grant permissions to the role
grant select
on department
to instructor;
Then roles can be granted to users or roles as shown below
grant dean to Amit;
create role dean;
grant instructor to dean;
4.6.5 Transfer of authority
We can grant
append when executing the command with grant option
to allow the authorized user to grant the authorization to other users again, as shown below.
grant select on department to Amit with grant option;
The transfer of authority from one user to another can be represented as an authorization graph, but the behavior of mutual authorization can lead to loops in the authorization graph.
The necessary and sufficient condition for a user to have authority is that there is a path from the root node of the authorization graph to the vertex representing the user.
4.6.6 Withdrawal of permissions
When the authority of a user is withdrawn, the authority granted to other users will also be recovered by the default cascade, but it can be revoke
added later restrict
to prevent the cascade from being recovered.