SlideShare a Scribd company logo
1 of 10
Download to read offline
SQL Basics – Ch 4 – Relational Databases

                    SQL – RELATIONAL DATABASES
1.     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, REVOKE

Prof. Mukesh N. Tekwani                                                                  Page 1
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
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 text

Prof. Mukesh N. Tekwani                                                                    Page 3
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 can''t'   becomes the seven-character string "I can't".
      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 15

6     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 value

Page 4                                                                   mukeshtekwani@hotmail.com
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
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
SQL Basics – Ch 4 – Relational Databases




11     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
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 requires

Page 8                                                                mukeshtekwani@hotmail.com
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
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

More Related Content

What's hot (20)

SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
Database queries
Database queriesDatabase queries
Database queries
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Assignment#05
Assignment#05Assignment#05
Assignment#05
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Relational database management system
Relational database management systemRelational database management system
Relational database management system
 
Sql
SqlSql
Sql
 

Similar to Sql ch 4 (20)

12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
chapter-14-sql-commands.pdf
chapter-14-sql-commands.pdfchapter-14-sql-commands.pdf
chapter-14-sql-commands.pdf
 
Ankit
AnkitAnkit
Ankit
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
sql-commands.pdf
sql-commands.pdfsql-commands.pdf
sql-commands.pdf
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql commands
Sql commandsSql commands
Sql commands
 
PO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - SqlPO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - Sql
 
SQL Interview Questions For Experienced
SQL Interview Questions For ExperiencedSQL Interview Questions For Experienced
SQL Interview Questions For Experienced
 
PT- Oracle session01
PT- Oracle session01 PT- Oracle session01
PT- Oracle session01
 
Sql
SqlSql
Sql
 
Database Management Lab -SQL Queries
Database Management Lab -SQL Queries Database Management Lab -SQL Queries
Database Management Lab -SQL Queries
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
DBMS Part-3.pptx
DBMS Part-3.pptxDBMS Part-3.pptx
DBMS Part-3.pptx
 
Module02
Module02Module02
Module02
 
SQL
SQLSQL
SQL
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEM
 

More from Mukesh Tekwani

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelMukesh Tekwani
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfMukesh Tekwani
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - PhysicsMukesh Tekwani
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion Mukesh Tekwani
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Mukesh Tekwani
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversionMukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversionMukesh Tekwani
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Mukesh Tekwani
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prismMukesh Tekwani
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surfaceMukesh Tekwani
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomMukesh Tekwani
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesMukesh Tekwani
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEMukesh Tekwani
 

More from Mukesh Tekwani (20)

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdf
 
Circular motion
Circular motionCircular motion
Circular motion
 
Gravitation
GravitationGravitation
Gravitation
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - Physics
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversion
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prism
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surface
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atom
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lenses
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
 
XML
XMLXML
XML
 

Sql ch 4

  • 1. SQL Basics – Ch 4 – Relational Databases SQL – RELATIONAL DATABASES 1. 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, REVOKE Prof. Mukesh N. Tekwani Page 1
  • 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. 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 text Prof. Mukesh N. Tekwani Page 3
  • 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 can''t' becomes the seven-character string "I can't". 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 15 6 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 value Page 4 mukeshtekwani@hotmail.com
  • 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. 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. SQL Basics – Ch 4 – Relational Databases 11 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. 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 requires Page 8 mukeshtekwani@hotmail.com
  • 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. 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