📄 sql.sgml
字号:
together that agree on both attributes S.SNO and S.SNAME: <programlisting> S.SNO | S.SNAME | SE.PNO -------+---------+-------- 1 | Smith | 1 | 2 -------------------------- 2 | Jones | 4 -------------------------- 3 | Adams | 1 | 3 -------------------------- 4 | Blake | 2 | 3 | 4 </programlisting> </para> <para> In our example we got four groups and now we can apply the aggregate operator COUNT to every group leading to the total result of the query given above. </para> </example> </para> <para> Note that for the result of a query using GROUP BY and aggregate operators to make sense the attributes grouped by must also appear in the target list. All further attributes not appearing in the GROUP BY clause can only be selected by using an aggregate function. On the other hand you can not use aggregate functions on attributes appearing in the GROUP BY clause. </para> </sect3> <sect3> <title>Having</title> <para> The HAVING clause works much like the WHERE clause and is used to consider only those groups satisfying the qualification given in the HAVING clause. The expressions allowed in the HAVING clause must involve aggregate functions. Every expression using only plain attributes belongs to the WHERE clause. On the other hand every expression involving an aggregate function must be put to the HAVING clause. <example> <title id="having-example">Having</title> <para> If we want only those suppliers selling more than one part we use the query: <programlisting> SELECT S.SNO, S.SNAME, COUNT(SE.PNO) FROM SUPPLIER S, SELLS SE WHERE S.SNO = SE.SNO GROUP BY S.SNO, S.SNAME HAVING COUNT(SE.PNO) > 1; </programlisting> and get: <programlisting> SNO | SNAME | COUNT -----+-------+------- 1 | Smith | 2 3 | Adams | 2 4 | Blake | 3 </programlisting> </para> </example> </para> </sect3> <sect3> <title>Subqueries</title> <para> In the WHERE and HAVING clauses the use of subqueries (subselects) is allowed in every place where a value is expected. In this case the value must be derived by evaluating the subquery first. The usage of subqueries extends the expressive power of <acronym>SQL</acronym>. <example> <title id="subselect-example">Subselect</title> <para> If we want to know all parts having a greater price than the part named 'Screw' we use the query: <programlisting> SELECT * FROM PART WHERE PRICE > (SELECT PRICE FROM PART WHERE PNAME='Screw'); </programlisting> </para> <para> The result is: <programlisting> PNO | PNAME | PRICE -----+---------+-------- 3 | Bolt | 15 4 | Cam | 25 </programlisting> </para> <para> When we look at the above query we can see the keyword SELECT two times. The first one at the beginning of the query - we will refer to it as outer SELECT - and the one in the WHERE clause which begins a nested query - we will refer to it as inner SELECT. For every tuple of the outer SELECT the inner SELECT has to be evaluated. After every evaluation we know the price of the tuple named 'Screw' and we can check if the price of the actual tuple is greater. </para><para> If we want to know all suppliers that do not sell any part (e.g. to be able to remove these suppliers from the database) we use: <programlisting> SELECT * FROM SUPPLIER S WHERE NOT EXISTS (SELECT * FROM SELLS SE WHERE SE.SNO = S.SNO); </programlisting> </para> <para> In our example the result will be empty because every supplier sells at least one part. Note that we use S.SNO from the outer SELECT within the WHERE clause of the inner SELECT. As described above the subquery is evaluated for every tuple from the outer query i.e. the value for S.SNO is always taken from the actual tuple of the outer SELECT. </para> </example> </para> </sect3> <sect3> <title>Union, Intersect, Except</title> <para> These operations calculate the union, intersect and set theoretic difference of the tuples derived by two subqueries. <example> <title id="union-example">Union, Intersect, Except</title> <para> The following query is an example for UNION: <programlisting> SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNAME = 'Jones' UNION SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNAME = 'Adams'; </programlisting>gives the result: <programlisting> SNO | SNAME | CITY -----+-------+-------- 2 | Jones | Paris 3 | Adams | Vienna </programlisting> </para> <para> Here an example for INTERSECT: <programlisting> SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 1 INTERSECT SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 2; </programlisting> gives the result: <programlisting> SNO | SNAME | CITY -----+-------+-------- 2 | Jones | ParisThe only tuple returned by both parts of the query is the one having $SNO=2$. </programlisting> </para> <para> Finally an example for EXCEPT: <programlisting> SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 1 EXCEPT SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 3; </programlisting> gives the result: <programlisting> SNO | SNAME | CITY -----+-------+-------- 2 | Jones | Paris 3 | Adams | Vienna </programlisting> </para> </example> </para> </sect3> </sect2> <sect2 id="datadef"> <title>Data Definition</title> <para> There is a set of commands used for data definition included in the <acronym>SQL</acronym> language. </para> <sect3 id="create"> <title id="create-title">Create Table</title> <para> The most fundamental command for data definition is the one that creates a new relation (a new table). The syntax of the CREATE TABLE command is: <synopsis> CREATE TABLE <replaceable class="parameter">table_name</replaceable> (<replaceable class="parameter">name_of_attr_1</replaceable> <replaceable class="parameter">type_of_attr_1</replaceable> [, <replaceable class="parameter">name_of_attr_2</replaceable> <replaceable class="parameter">type_of_attr_2</replaceable> [, ...]]); </synopsis> <example> <title id="table-create">Table Creation</title> <para> To create the tables defined in <xref linkend="supplier-fig" endterm="supplier-fig"> the following <acronym>SQL</acronym> statements are used: <programlisting> CREATE TABLE SUPPLIER (SNO INTEGER, SNAME VARCHAR(20), CITY VARCHAR(20)); </programlisting> <programlisting> CREATE TABLE PART (PNO INTEGER, PNAME VARCHAR(20), PRICE DECIMAL(4 , 2)); </programlisting> <programlisting> CREATE TABLE SELLS (SNO INTEGER, PNO INTEGER); </programlisting> </para> </example> </para> </sect3> <sect3> <title>Data Types in <acronym>SQL</acronym></title> <para> The following is a list of some data types that are supported by <acronym>SQL</acronym>: <itemizedlist> <listitem> <para> INTEGER: signed fullword binary integer (31 bits precision). </para> </listitem> <listitem> <para> SMALLINT: signed halfword binary integer (15 bits precision). </para> </listitem> <listitem> <para> DECIMAL (<replaceable class="parameter">p</replaceable>[,<replaceable class="parameter">q</replaceable>]): signed packed decimal number of <replaceable class="parameter">p</replaceable> digits precision with assumed <replaceable class="parameter">q</replaceable> of them right to the decimal point.(15 ≥ <replaceable class="parameter">p</replaceable> ≥ <replaceable class="parameter">q</replaceable>q ≥ 0). If <replaceable class="parameter">q</replaceable> is omitted it is assumed to be 0. </para> </listitem> <listitem> <para> FLOAT: signed doubleword floating point number. </para> </listitem> <listitem> <para> CHAR(<replaceable class="parameter">n</replaceable>): fixed length character string of length <replaceable class="parameter">n</replaceable>. </para> </listitem> <listitem> <para> VARCHAR(<replaceable class="parameter">n</replaceable>): varying length character string of maximum length <replaceable class="parameter">n</replaceable>. </para> </listitem> </itemizedlist> </para> </sect3> <sect3> <title>Create Index</title> <para> Indices are used to speed up access to a relation. If a relation <classname>R</classname> has an index on attribute <classname>A</classname> then we can retrieve all tuples <replaceable>t</replaceable> having <replaceable>t</replaceable>(<classname>A</classname>) = <replaceable>a</replaceable> in time roughly proportional to the number of such tuples <replaceable>t</replaceable> rather than in time proportional to the size of <classname>R</classname>. </para> <para> To create an index in <acronym>SQL</acronym> the CREATE INDEX command is used. The syntax is: <programlisting> CREATE INDEX <replaceable class="parameter">index_name</replaceable>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -