Docsity

Dbms notes Study material-ssit-unit-3, Lecture notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
UNIT-3-DBMS-22AM5.2
SSIT-TUMKUR
CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
Syntax:-CREATE TABLE EMPLOYEE
Subject: DATABASE MANAGEMENT SYSTEM
Subject Code: 22AM502
UNIT-3
Basic SQL: Data Definition and Data Types, Specifying constraints in SQL, Basic retrieval queries in
SQL, Insert, Delete and Update statements in SQL. More complex SQL Queries, Specifying Constraints
as Assertions and Actions as Triggers Views (Virtual Tables) in SQL, Schema change statements in SQL.
(Text 1: 6.1 to 6.4) (Text 1: 7.1 to 7.4)
6.1
SQL Data Definition and Data Types
6.1.1
Schema and Catalog Concepts in SQL
• For example, the following statement creates a schema called COMPANY owned by the
user with authorization identifier ‘Jsmith’. Note that each statement in SQL ends with a
semicolon.
• catalog—a named collection of schemas.
6.1.2
The CREATE TABLE Command in SQL
• The CREATE TABLE command is used to specify a new relation by giving it a name and
specifying its attributes and initial constraints.
Asst. Prof. KAVYA R
AI&ML Department
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Dbms notes Study material-ssit-unit-3 and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’; Syntax:-CREATE TABLE EMPLOYEE

Subject: DATABASE MANAGEMENT SYSTEM

Subject Code: 22AM

UNIT- 3

Basic SQL : Data Definition and Data Types, Specifying constraints in SQL, Basic retrieval queries in SQL, Insert, Delete and Update statements in SQL. More complex SQL Queries, Specifying Constraints as Assertions and Actions as Triggers Views (Virtual Tables) in SQL, Schema change statements in SQL. (Text 1: 6.1 to 6.4) (Text 1: 7.1 to 7.4)

6.1 SQL Data Definition and Data Types

6.1.1 Schema and Catalog Concepts in SQL

  • For example , the following statement creates a schema called COMPANY owned by the user with authorization identifier ‘Jsmith’. Note that each statement in SQL ends with a semicolon.
  • catalog —a named collection of schemas. 6.1.2 The CREATE TABLE Command in SQL
  • The CREATE TABLE command is used to specify a new relation by giving it a name and specifying its attributes and initial constraints. Asst. Prof. KAVYA R AI&ML Department

6.1.3 Attribute Data Types and Domains in SQL

  • The basic data types available for attributes include numeric, character string, bit string, Boolean, date, and time.
  1. Numeric data types include integer numbers of various sizes (INTEGER or INT, and SMALLINT) and floating-point (real) numbers of various precision (FLOAT or REAL, and DOUBLE PRECISION).
  2. Character-string data types are either fixed length—CHAR(n) or CHARACTER(n), where n is the number of characters—or varying length— VARCHAR(n) or CHAR VARYING(n) or CHARACTER VARYING(n), where n is the maximum number of characters. Asst. Prof. KAVYA R AI&ML Department
  • The CHECK clause can also be used to specify more general constraints using the CREATE ASSERTION statement of SQL.

6.3 Basic Retrieval Queries in SQL

The basic form of the SELECT statement, sometimes called a mapping or a

select-from-where block , is formed of the three clauses SELECT, FROM, and

WHERE and has the following form:

SCHEMA DIAGRAM OF COMPANY DATABASE

Asst. Prof. KAVYA R AI&ML Department

Asst. Prof. KAVYA R AI&ML Department

  • A query that involves only selection and join conditions plus projection attributes is known as a select-project-join query.
  • The next example is a select-project-join query with two join conditions Query 2. For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. Q2: SELECT FROM Pnumber, Dnum, Lname, Address, Bdate PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Plocation = ‘Stafford.
  • The join condition Dnum = Dnumber relates a project tuple to its controlling depart ment tuple, whereas the join condition Mgr_ssn = Ssn relates the controlling depart ment tuple to the employee tuple who manages that department.
  • Each tuple in the result will be a combination of one project, one department (that controls the proj ect), and one employee (that manages the department).
  • The projection attributes are used to choose the attributes to be displayed from each combined tuple. The result of query Q2 is shown in Figure 6.3(c). Asst. Prof. KAVYA R AI&ML Department

Q1A: SELECT

FROM

WHERE

Fname, EMPLOYEE.Name, Address EMPLOYEE, DEPARTMENT DEPARTMENT.Name = ‘Research’ AND DEPARTMENT.Dnumber = EMPLOYEE.Dnumber; Q1′: SELECT FROM WHERE EMPLOYEE.Fname, EMPLOYEE.LName, EMPLOYEE.Address EMPLOYEE, DEPARTMENT DEPARTMENT.DName = ‘Research’ AND DEPARTMENT.Dnumber = EMPLOYEE.Dno;

6.3.2 Ambiguous Attribute Names, Aliasing, Renaming, and Tuple Variables

  • In SQL, the same name can be used for two (or more) attributes as long as the attributes are in different tables.
  • If this is the case, and a multitable query refers to two or more attributes with the same name, we must qualify the attribute name with the relation name to prevent ambiguity.
  • This is done by prefixing the relation’s name to the attribute name and separating the two by a period. To illustrate this, suppose that in Figures 5.5 and 5.
  • the Dno and Lname attributes of the EMPLOYEE relation were called Dnumber and Name, and the Dname attribute of DEPARTMENT was also called Name; then, to prevent ambiguity,
  • Query Q1 would be rephrased as shown in Q1A. We must prefix the attributes Name and Dnumber in Q1A to specify which ones we are referring to, because the same attribute names are used in both relations:
  • Fully qualified attribute names can be used for clarity even if there is no ambiguity in attribute names. Q1 can be rewritten as Q1′ below with fully qualified attribute names.
  • We can also rename the table names to shorter names by creating an alias for each table name to avoid repeated typing of long table names (see Q8 below).
  • The ambiguity of attribute names also arises in the case of queries that refer to the same relation twice, as in the following example. Asst. Prof. KAVYA R AI&ML Department

Q1B: SELECT

FROM

WHERE

E.Fname, E.LName, E.Address EMPLOYEE AS E, DEPARTMENT AS D D.DName = ‘Research’ AND D.Dnumber = E.Dno; For example, we could specify query Q1 as in Q1B

6.3.3 Unspecified WHERE Clause and Use of the Asterisk

  • We discuss two more features of SQL here. A missing WHERE clause indicates no condition on tuple selection; hence, all tuples of the relation specified in the FROM clause qualify and are selected for the query result.
  • If more than one relation is specified in the FROM clause and there is no WHERE clause, then the CROSS PRODUCT—all possible tuple combinations—of these relations is selected.
  • For example, Query 9 selects all EMPLOYEE Ssns (Figure 6.3(e)) , and Query 10 selects all combinations of an EMPLOYEE Ssn and a DEPARTMENT Dname, regardless of whether the employee works for the department or not (Figure 6.3(f)). Asst. Prof. KAVYA R AI&ML Department

Queries 9 and 10. Select all EMPLOYEE Ssns (Q9) and all combinations of EMPLOYEE Ssn and DEPARTMENT Dname (Q10) in the database. Q9: SELECT FROM Ssn EMPLOYEE; Q10: SELECT Ssn, Dname FROM EMPLOYEE, DEPARTMENT;

  • It is extremely important to specify every selection and join condition in the WHERE clause; if any such condition is overlooked, incorrect and very large relations may result.
  • Notice that Q10 is similar to a CROSS-PRODUCT operation followed by a PROJECT operation in relational algebra (see Chapter 8).
  • If we specify all the attributes of EMPLOYEE and DEPARTMENT in Q10, we get the actual CROSS PRODUCT (except for duplicate elimination, if any).
  • To retrieve all the attribute values of the selected tuples, we do not have to list the attribute names explicitly in SQL; we just specify an asterisk ()* , which stands for all the attributes.
  • The * can also be prefixed by the relation name or alias; for example, EMPLOYEE.* refers to all attributes of the EMPLOYEE table. Asst. Prof. KAVYA R AI&ML Department
  • In general, a query with SELECT DISTINCT eliminates duplicates,
  • whereas a query with SELECT ALL does not. Specifying SELECT with neither ALL nor DISTINCT—as in our previous examples—is equivalent to SELECT ALL.
  • For example, Q11 retrieves the salary of every employee; if several employees have the same salary, that salary value will appear as many times in the result of the query, as shown in Figure 6.4(a).
  • If we are interested only in distinct salary values, we want each value to appear only once, regardless of how many employees earn that salary. By using the keyword DISTINCT as in Q11A, we accomplish this, as shown in Figure 6.4(b). Query 11. Retrieve the salary of every employee (Q11) and all distinct salary values (Q11A).
  • SQL has directly incorporated some of the set operations from mathematical set theory, which are also part of relational algebra (see Chapter 8).
  • There are set union (UNION), set difference (EXCEPT), and set intersection (INTERSECT) operations.
  • The relations resulting from these set operations are sets of tuples; that is, duplicate tuples are eliminated from the result.
  • These set operations apply only to type- compatible relations, so we must make sure that the two relations on which we apply the operation have the same attributes and that the attributes appear in the same order in both relations. The next example illustrates the use of UNION. Asst. Prof. KAVYA R AI&ML Department

Query 4. Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project.

  • The first SELECT query retrieves the projects that involve a ‘Smith’ as manager of the department that controls the project, and
  • the second retrieves the projects that involve a ‘Smith’ as a worker on the project.
  • Notice that if several employees have the last name ‘Smith’, the project names involving any of them will be retrieved.
  • Applying the UNION operation to the two SELECT queries gives the desired result.
  • SQL also has corresponding multiset operations, which are followed by the key word ALL (UNION ALL, EXCEPT ALL, INTERSECT ALL).
  • Their results are multisets (duplicates are not eliminated). The behavior of these operations is illustrated by the examples in Figure 6.5.
  • Basically, each tuple—whether it is a duplicate or not— is considered as a different tuple when applying these operations.

6.3.5 Substring Pattern Matching and Arithmetic Operators

  • The first feature allows comparison conditions on only parts of a character string, using the LIKE comparison operator.
  • This can be used for string pattern matching.
  • Partial strings are spec ified using two reserved characters: % replaces an arbitrary number of zero or more characters, and the underscore (_) replaces a single character. For example, consider the following query Query 12. Retrieve all employees whose address is in Houston, Texas. Asst. Prof. KAVYA R AI&ML Department
  • For string data types, the concatenate operator || can be used in a query to append two string values.
  • For date, time, timestamp, and interval data types, operators include incrementing (+) or decrementing (−) a date, time, or timestamp by an interval.
  • In addition, an interval value is the result of the difference between two date, time, or timestamp values.
  • Another comparison operator, which can be used for convenience, is BETWEEN, which is illustrated in Query 14. Query 14. Retrieve all employees in department 5 whose salary is between $30,000 and $40,000.
  • The condition (Salary BETWEEN 30000 AND 40000) in Q14 is equivalent to the con dition ((Salary >= 30000) AND (Salary <= 40000)).

6.3.6 Ordering of Query Results

Query 15. Retrieve a list of employees and the projects they are working on, ordered by department and, within each department, ordered alphabetically by last name, then first name.

  • The default order is in ascending order of values.
  • We can specify the keyword DESC if we want to see the result in a descending order of values.
  • The keyword ASC can be used to specify ascending order explicitly. For example, if we want descending alphabetical order on Dname and ascending order on Lname, Fname, the ORDER BY clause of Q15 can be written as

ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC

Asst. Prof. KAVYA R AI&ML Department

6.3.7 Discussion and Summary of Basic SQL Retrieval Querie

  • A simple retrieval query in SQL can consist of up to four clauses, but only the first two SELECT and FROM—are mandatory.
  • The clauses are specified in the following order, with the clauses between square brackets [ … ] being optional:
  • The SELECT clause lists the attributes to be retrieved,
  • FROM clause specifies all relations (tables) needed in the simple query.
  • WHERE clause identifies the conditions for selecting the tuples from these relations, including join conditions if needed.
  • ORDER BY specifies an order for displaying the results of a query.

6.4 INSERT, DELETE, and UPDATE Statements in SQL

6.4.1 The INSERT Command

Asst. Prof. KAVYA R AI&ML Department

Asst. Prof. KAVYA R AI&ML Department

FROM 7.1 TO 7.

Subject: DATABASE MANAGEMENT SYSTEM

Subject Code: 22AM

UNIT- 3

Basic SQL : Data Definition and Data Types, Specifying constraints in SQL, Basic retrieval queries in SQL, Insert, Delete and Update statements in SQL. More complex SQL Queries, Specifying Constraints as Assertions and Actions as Triggers Views (Virtual Tables) in SQL, Schema change statements in SQL. (Text 1: 6.1 to 6.4) (Text 1: 7.1 to 7.4)

More complex SQL Queries

7.1.1 Comparisons Involving NULL and Three-Valued Logic

  • NULL is used to represent a missing value, but that it usually has one of three different interpretations
  • Consider the following examples to illustrate each of the meanings of NULL.
    1. Unknown value. A person’s date of birth is not known, so it is represented by NULL in the database. An example of the other case of unknown would be NULL for a person’s home phone because it is not known whether the person has a home phone.
    2. Unavailable or withheld value. A person has a home phone but does not want it to be listed, so it is withheld and represented as NULL in the database.
    3. Not applicable attribute. An attribute Last College Degree would be NULL for a person who has no college degrees because it does not apply to that person.
  • When a record with NULL in one of its attributes is involved in a comparison operation, the result is UNKNOWN (it may be TRUE or it may be FALSE).
  • Hence, SQL uses a three-valued logic with values TRUE, FALSE, and UNKNOWN instead of the standard two-valued (Boolean) logic with values TRUE or FALSE.
  • It is therefore necessary to define the results (or truth values) of three-valued logical expressions when the logical connectives AND, OR, and NOT are used. Table 7.1 shows the resulting values.

Asst. Prof. KAVYA R AI&ML Department

  • In select-project-join queries, the general rule is that only those combinations of tuples that evaluate the logical expression in the WHERE clause of the query to TRUE are selected.
  • Tuple combinations that evaluate to FALSE or UNKNOWN are not selected. However, there are exceptions to that rule for certain operations, such as outer joins
  • SQL allows queries that check whether an attribute value is NULL. Rather than using = or <> to compare an attribute value to NULL, SQL uses the comparison operators IS or IS NOT.
  • Query 18 illustrates NULL com parison by retrieving any employees who do not have a supervisor. 7.1.2 Nested Queries, Tuples, and Set/Multiset Comparisons
  • In Q4A, the first nested query selects the project numbers of projects that have an employee with last name ‘Smith’ involved as manager,
  • whereas the second nested query selects the project numbers of projects that have an employee with last name ‘Smith’ involved as worker.
  • In the outer query, we use the OR logical connective to retrieve a PROJECT tuple if the PNUMBER value of that tuple is in the result of either nested query.