Sql ch 4


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Sql ch 4

  1. 1. SQL Basics – Ch 4 – Relational Databases SQL – RELATIONAL DATABASES1. What is a SQL statement? An SQL statement requests an specific action from the DBMS. This action may be creating a new table, inserting a new record, retrieving data, deleting a record, or modifying a database. The main structure of an SQL statement is shown below: Every SQL statement begins with a keyword that describes what the statement does. CREATE, INSERT, DELETE, and COMMIT are typical verbs. The statement continues with one or more clauses. A clause may specify the data to be acted upon by the statement or provide more detail about what the statement is supposed to do. Every clause also begins with a keyword, such as WHERE, FROM, INTO, and HAVING. Some clauses are optional; others are required. Many clauses contain table or column names; some may contain additional keywords, constants, or expressions. Keywords will always be shown in uppercase e..g., SELECT, FROM, etc Variable items such as table name, are shown in lowercase letters.2. What are the different types of SQL statements? SQL statements can be divided into the following categories:. (i) Data Manipulation Statements - SELECT, INSERT, DELETE, UPDATE (ii) Data Definition statements – CREATE TABLE, DROP TABLE, ALTER TABLE, CREATE VIEW, DROP VIEW, CREATE INDEX, DROP INDEX, CREATE SCHEMA, DROP SCHEMA, CREATE DOMAIN, ALTER DOMAIN, DROP DOMAIN (iii) Access Control – GRANT, REVOKEProf. Mukesh N. Tekwani Page 1
  2. 2. SQL Basics– Ch 4 – Relational Databases (iv) Transaction Control – COMMIT, ROLLBACK, SET TRANSACTION (v) Programmatic SQL statements – DECLARE, EXPLAIN, OPEN, CLOSE, FETCH, PREPARE, EXECUTE, DESCRIBE.3. What are table names and column names? (i) The objects in a SQL-based database are identified by assigning them unique names. Names are used in SQL statements to identify the database object on which the statement should act. (ii) The most fundamental named objects in a relational database are table names (which identify tables), column names (which identify columns), and user names (which identify users of the database). (iii) The original ANSI/ISO standard specified that SQL names must contain 1 to 18 characters, must begin with a letter, and may not contain any spaces or special punctuation characters. The SQL2 standard increased the maximum to 128 characters. Table Name: When we specify a table name in a SQL statement, SQL assumes that you are referring to one of your own tables (that is, a table that you created). The table names must be short and descriptive. Column Name: When we specify a column name in a SQL statement, SQL can normally determine from the context which column we want. However, if the statement involves two columns with the same name from two different tables, we must use a qualified column name to uniquely identify the column we want. A qualified column name specifies the name of the table containing the column and the name of the column, separated by a period (.). For example, the column named SALES in the SALESREPS table has the qualified column name: SALESREPS.SALES Qualified column names can generally be used in a SQL statement wherever a simple (unqualified) column name can appear.4 Describe the different data types used in SQL. The ANSI SQL standard specifies the various data types that can be stored in a SQL-based database and manipulated by the SQL language. These data types are: (i) Integer - Columns holding this type of data store counts, quantities, ages, and so on. Integer columns are also used to contain I.D. numbers, such as customer, employee, and order numbers. INT or INTEGER (ii) Decimal numbers. Columns with this data type store numbers that have fractional parts and must be calculated exactly, such as rates and percentages. They are also used to store money amounts.Page 2 mukeshtekwani@hotmail.com
  3. 3. SQL Basics – Ch 4 – Relational Databases (iii) Floating point numbers. Columns with this data type are used to store scientific numbers that can be calculated approximately, such as weights and distances. Floating point numbers can represent a larger range of values than decimal numbers but can produce round-off errors in computations. FLOAT (precision) (iv) Fixed-length character strings. Columns holding this type of data store names of people and companies, addresses, descriptions, and so on. CHAR(len) or CHARATER (len) (v) Variable-length character strings. This data type allows a column to store character strings that vary in length from row to row, up to some maximum length. VARCHAR (len) or CHARACTER VARYING (len) or CHAR VARYING (len) (vi) Money amounts. Many SQL products support a MONEY or CURRENCY type, which is usually stored as a decimal or floating point number. (vii) Dates and times. SQL can also support date/time values. Various combinations of dates, times, timestamps, time intervals, and date/time arithmetic are supported. DATE , TIME (viii) Boolean data. Some SQL products support logical (TRUE or FALSE) values as an explicit type, and some permit logical operations (comparison, AND/OR, and so on) on the stored data within SQL statements. (ix) Long text. Many SQL-based databases support columns that store long text strings (typically up to 32,000 or 65,000 characters, and in some cases even larger). This allows the database to store entire documents, product descriptions, technical papers, resumes, and similar unstructured text data. The DBMS usually restricts the use of these columns in interactive queries and searches. (x) Unstructured byte streams. Several DBMS products allow unstructured, variable-length sequences of bytes to be stored and retrieved. Columns containing this data are used to store compressed video images, executable code, and other types of unstructured data.5 What are constants in SQL? Explain the different types of constants used in SQL. In some SQL statements a numeric, character, or date data value must be expressed in textProf. Mukesh N. Tekwani Page 3
  4. 4. SQL Basics– Ch 4 – Relational Databases form. For example, in this INSERT statement, which adds a salesperson to the database: INSERT INTO SALESREPS (EMPL_NUM, NAME, QUOTA, HIRE_DATE, SALES) VALUES (115, Dennis Irving, 175000.00, 21-JUN-90, 0.00) the value for each column in the newly inserted row is specified in the VALUES clause. Constant data values are also used in expressions, such as in this SELECT statement: SELECT CITY FROM OFFICES WHERE TARGET > (1.1 * SALES) + 10000.00 Numeric Constants: Integer and decimal constants are written as ordinary decimal numbers in SQL statements, with an optional leading plus or minus sign. 21 -375 2000.00 +497500.8778 We must not put a comma between the digits of a numeric constant. Floating point constants (also called approximate numeric literals) are specified using the E notation. Here are some valid SQL floating point constants: 1.5E3 -3.14159E1 2.5E-7 0.783926E21 String Constants: These contain character data enclosed within single quotes mark. For example, New Delhi. New York Western If a single quote is to be included in the constant text, it is written within the constant as two consecutive single quote characters. Thus this constant value: I cant becomes the seven-character string "I cant". Date and Time Constants: In SQL constant values for dates, times, and time intervals are specified as string constants. Here are some examples of legal SQL Server date constants: March 15, 1990 Mar 15 1990 3/15/1990 3-15-90 1990 MAR 156 What are symbolic constants in SQL? SQL language includes special symbolic constants that return data values maintained by the DBMS itself. For example, the symbolic constant CURRENT_DATE gives the valuePage 4 mukeshtekwani@hotmail.com
  5. 5. SQL Basics – Ch 4 – Relational Databases of the current date and can be used in queries such as the following, which lists the salespeople whose hire date is still in the future. SELECT NAME, HIRE_DATE FROM SALESREPS WHERE HIRE_DATE > CURRENT_DATE A symbolic constant can appear in a SQL statement anywhere that an ordinary constant of the same data type could appear.7 How are expressions used in SQL statements? Expressions are used in the SQL to calculate values that are retrieved from a database and to calculate values used in searching the database. For example, this query calculates the sales of each office as a percentage of its target: SELECT CITY, TARGET, SALES, (SALES/TARGET) * 100 FROM OFFICES and this query lists the offices whose sales are more than $50,000 over target: SELECT CITY FROM OFFICES HERE SALES > TARGET + 50000.00 The ANSI SQL standard specifies four arithmetic operations that can be used in expressions: addition , subtraction), multiplication, and division.8 Explain automatic data type conversion in SQL statements. The ANSI standard specifies automatic data type conversion from integers to decimal numbers, and from decimal numbers to floating point numbers. We can thus mix these data types in a numeric expression.9 What are built-in functions in SQL? Most SQL implementations include a number of useful built-in functions. These facilities often provide data type conversion facilities. For example, built-in MONTH() and YEAR() functions take a DATE or TIMESTAMP value as their input and return an integer that is the month or year portion of the value. This query lists the name and month of hire for each salesperson in the sample database: SELECT NAME, MONTH(HIRE_DATE)Prof. Mukesh N. Tekwani Page 5
  6. 6. SQL Basics– Ch 4 – Relational Databases FROM SALESREPS and this one lists all salespeople hired in 1988: SELECT NAME, MONTH(HIRE_DATE) FROM SALESREPS WHERE YEAR(HIRE_DATE) = 1988 Built-in functions also are often used for data reformatting. TO_CHAR() function, for example, takes a DATE data type and a format specification as its arguments and returns a string containing a formatted version of the date. In the results produced by this query: SELECT NAME, TO_CHAR(HIRE_DATE,DAY MONTH DD, YYYY) FROM SALESREPS The hire dates will all have the format "Wednesday June 14, 1989" because of the built-in function.10 Explain the term “primary key”. (i) The rows of a relational table are unordered, and so we cannot select a specific row by its position in the table. There is no "first row," "last row," or "thirdth row" of a table. (ii) In a well-designed relational database every table has some column or combination of columns whose values uniquely identify each row in the table. This column (or columns) is called the primary key of the table. (iii) E.g., in a list of students, we cannot use the lastname field as the primary key since two or more students may have the same lastname. We may combine the lastname and firstname and create a primary key. But in practice, "Roll number," such as is often chosen as primary keys. In a table of orders placed by customers, the ORDERNO can be used as a primary key. (iv) The primary key has a different unique value for each row in a table, so no two rows of a table with a primary key are exact duplicates of one another. A table where every row is different from all other rows is called a relation in mathematical terms. The name "relational database" comes from this term, because relations (tables with distinct rows) form the basis of a relational database. The following figure shows a table with two columns being used for a primary key. This key is called a composite key.Page 6 mukeshtekwani@hotmail.com
  7. 7. SQL Basics – Ch 4 – Relational Databases11 Explain the term “foreign key”. (i) A column in one table whose value matches the primary key in some other table is called a foreign key. (ii) In the figure shown below, the REP_OFFICE column is a foreign key for the OFFICES table. Although REP_OFFICE is a column in the SALESREPS table, the values that this column contains are office numbers. They match values in the OFFICE column, which is the primary key for the OFFICES table. Together, a primary key and a foreign key create a parent/child relationship between the tables that contain them. (iii) Just as a combination of columns can serve as the primary key of a table, a foreign key can also be a combination of columns. In fact, the foreign key will always be a compound (multi-column) key when it references a table with a compound primary key. The number of columns and the data types of the columns in the foreign key and the primary key must be identical to one another. (iv) A table can contain more than one foreign key if it is related to more than one other table. Figure below shows the three foreign keys in the ORDERS table of the sample database:Prof. Mukesh N. Tekwani Page 7
  8. 8. SQL Basics– Ch 4 – Relational Databases The CUST column is a foreign key for the CUSTOMERS table, relating each order to the customer who placed it. The REP column is a foreign key for the SALESREPS table, relating each order to the salesperson who took it. The MFR and PRODUCT columns together are a composite foreign key for the PRODUCTS table, relating each order to the product being ordered. (v) Foreign keys are an important part of the relational model because they create relationships among tables in the database.12 What are Codd’s twelve rules for databases? Rule 1: The information rule. All information in a relational database is represented explicitly at the logical level and in exactly one way—by values in tables. Rule 2: Guaranteed access rule. Each and every datum (atomic value) in a relational database is guaranteed to be logically accessible by using a combination of table name, primary key value, and column name. This rule stresses the importance of primary key to locate data. The table name locates the correct table, the column name finds the correct column, and the primary key value finds the row containing an individual data item of interest. Rule 3: Systematic treatment of null values. Null values (distinct from an empty character string or a string of blank characters and distinct from zero or any other number) are supported in a fully relational DBMS for representing missing information and inapplicable information in a systematic way, independent of the data type. Rule 3 requiresPage 8 mukeshtekwani@hotmail.com
  9. 9. SQL Basics – Ch 4 – Relational Databases support for missing data through NULL values. Rule 4: Dynamic online catalog based on the relational model. The database description is represented at the logical level in the same way as ordinary data, so that authorized users can apply the same relational language to its interrogation as they apply to the regular data. Rule 5: Comprehensive data sublanguage rule. A relational system may support several languages. However, there must be at least one language whose statements can be expressed, by some well-defined syntax. This language should also support the following activities: • Data definition • View definition • Data manipulation (interactive and by program) • Integrity constraints • Authorization • Transaction boundaries (begin, commit, and rollback) What this rule means is that the RDBMS should have its own extension of SQL. This extension should support the above activities Rule 6: View updating rule. All views that are theoretically updateable are also updateable by the system. Rule 7: High-level insert, update, and delete. The capability of handling a base relation or a derived relation as a single operand applies not only to the retrieval of data but also to the insertion, update, and deletion of data. The RDBMS should not only support retrieval of data as relational sets, but should also support insertion, deletion, and updation of data as a relational set. If the database uses a single record at a time and procedural techniques to manipulate the data, then it cannot be called a relational database. Rule 8: Physical data independence. Application programs are not disturbed if any changes are made in either storage representations or access methods. Rule 9: Logical data independence. Application programs and terminal activities remain logically unimpaired when information preserving changes of any kind that theoretically permit unimpairment are made to the base tables. Thus, this rule allows dynamic changes in the logical database design by splitting or joining base tables, provided it does not lead to loss of information. Rule 10: Integrity independence. Integrity constraints specific to a particular relational database must be definable in the relational data sublanguage and storable in the catalog,Prof. Mukesh N. Tekwani Page 9
  10. 10. SQL Basics– Ch 4 – Relational Databases not in the application programs. Every database must support the following rules: Entity integrity: No component of a primary key can have a NULL value. Referential Integrity: For every unique ‘non-null’ foreign key value, there should be a matching primary key. Rule 11: Distribution independence. A relational DBMS has distribution independence. It means that if a distributed database than it should be possible to execute all operations on it without being restricted by the physical locations of data. Rule 12: Non-subversion rule. A low-level language (i.e. a language working on the principle of a single record at a time), must in no way be used to bypass the integrity rules and constraints imposed in the higher level (multiple records at a time).Page 10 mukeshtekwani@hotmail.com