Introduction to SQL¶
:material-circle-edit-outline: 约 1279 个字 :fontawesome-solid-code: 94 行代码 :material-clock-time-two-outline: 预计阅读时间 14 分钟
- SQL : define the structure of the data, modify data in the database, and specify security constraints.
Overview of the SQL Query Language¶
- Data-definition language (DDL):The SQL DDL provides commands for defining relation schemas, deleting relations, and modifying relation schemas.
- Data-manipulation language (DML): The SQL DML provides the ability to query information from the database and to insert tuples into, delete tuples from, and modify tuples in the database.
- Integrity
- View Definition
- Transaction Control
-
Embedded SQL and Dynamic SQL
-
Authorization
3.2 SQL Data Definition¶
3.2.1 Basic Types¶
- CHAR(n): A fixed-length string of n characters. The full form, CHARACTER(n), is also supported.
- VARCHAR(n): A variable-length string with a maximum size of n characters. The full form, CHARACTER VARYING(n), is also supported.
- INT: A 4-byte integer. The full form, integer, is also supported.
-
SMALLINT: A small integer.
-
NUMERIC(p,d): A fixed-point number with p digits(plus a sign), d of which are to the right of the decimal point.
-
REAL, DOUBLE PRECISION: Floating-point numbers.
-
FLOAT(n): A floating-point number with at least n bits of precision.
-
NULL: The null value.
Note
Note
Tip
3.2.2 Basic Schema Definition¶
- create table: We define an SQL relation by using the create table command.
code
- integrity constraints:
- primary key: the primary -key attributes
- foreign key + references: Some database systems, including MySQL, require an alternative syntax, “foreign key (dept name) references department(dept name)”, where the referenced attributes in the referenced table are listed explicitly.
- not null:
<attribute> <type> not null
he constraint excludes the null value from the domain of that attribute.
Attention
drop table
: delete a relation
Comparison
drop table r
deletes the whole schema, including all tuples in the relation r. delete from r
deletes all the tuples, yet retains relation ralter table
: add attributes to an existing relation. All tuples in the relation are assigned null as the value for the new attribute.alter table r add A D;
A is the name of the attribute to be added, and D is the type of the added attribute. Thus,alter table r drop A;
deletes attribute A from relation r. To be aware, many database systems do not support dropping of attributes.
3.3 Basic Structure of SQL Queries¶
The basic structure of an SQL query consists of three clauses: select, from, and where. A query takes as its input the relations listed in the from clause, operates on them as specified in the where and select clauses, and then produces a relation as the result.
3.3.1 Queries on a Single Relation¶
- select distinct: In practice, duplicate elimination is time-consuming. Therefore, SQL allows duplicates in database relations as well as in the results of SQL expressions.1 Thus, the preceding SQL query lists each department name once for every tuple in which it appears in the instructor relation.
-
select all: SQL allows us to use the keyword all to specify explicitly that duplicates are not removed
-
The select clause may also contain arithmetic expressions involving the operators
+
,−
,∗
, and/
operating on constants or attributes of tuples. -
where: SQL allows the use of the logical connectives and, or, and not in the where clause. The operands of the logical connectives can be expressions involving the comparison operators <, <=, >, >=, =, and <>. SQL allows us to use the comparison operators to compare strings and arithmetic expressions, as well as special types, such as date types.
3.3.2 Queries on Multiple Relations¶
-
Natural Join:
\(instructor \bowtie teaches\)
Danger in natural join
natural join may provide more constraints -
Rename:
the
as
clause
-
Keyword as is optional and may be omitted.
instructor as T = instructor T
Keyword as must be omitted in Oracle -
string operations
SQL includes a string-matching operator for comparisons on character strings. The operator “like” uses patterns that are described using two special characters: *
%
: matches any substring of zero or more characters.
*_
: matches any single character.
- Ordering the display of tuples:
- order by: in alphabetical order
order by name desc
* can sort on multiple attributes
order by dept_name, name
-
Specific where clause predicates
between
tuple comparison
-
set operations
union
intersectSQL
except
Each of the above operations automatically eliminates duplicates -
To retain all duplicates use the corresponding multiset versions union all, intersect all and except all.
-
Null Values:
- It is possible for tuples to have a null value, denoted by null, for some of their attributes
The result of any arithmetic expression involving null is null
The predicate is null can be used to check for null values.
- (unknown or true) = true, (unknown or false) = unknown, (unknown or unknown) = unknown
- (unknown and true) = unknown, (unknown and false) = false, (unknown and unknown) = unknown
- (not unknown) = unknown
P is unknown
evaluates to true if predicate P evaluates to unknown.-
Result of where clause predicate is treated as false if it evaluates to unknown
-
Aggregate Functions:
group by
having: after group by -
NULL:
all aggregate operations except count(*) ignore tuples with null values on the aggregated attribute.
What if collection has only null values?
* count(*) returns 0 * all other aggregates return null
- Nested Subqueries:
- A subquery is a select-from-where expression that is nested within another query.
in:
- some:
SQL | |
---|---|
=some is equivalent to the keyword in
yet not some is not equalvalent to not in
-
all:
SQL salary of the instructor is greater than all the salaries of instructors in the Biology department.
*!all is equalvalent to !in -
test for empty relations
- exists:
in a nested subquery, an attribute can be fetched from the outer query.SQL
- exists:
- nstructor.ID and S.course_id are attributes that are not in the relation of the subquery, they are called correlation variables
-
not exists to realize division
-
test for absence of duplicate tuples
SQL evaluates to 'true' on an empty set
-
subqueries in the from clause
-
with clause:
The with clause provides a way of defining a temporary view whose definition is available only to the query in which the with clause occurs.
SQL | |
---|---|
-
complex queries using with clause:
-
scalar subquery:
Modification of the Database¶
- deletion:
delete from
delete from instructor where dept_name='Finance';
- insert one row:
- updates