SlideShare a Scribd company logo
1 of 9
Download to read offline
SQL – Ch 12 – Creating a Database

                               12. CREATING A DATABASE
1    What are the DDL statements of SQL?
     The statements of SQL that permit the creation of a database, table, view are called the SQL Data Definition
     Language or DDL. These statements are: CREATE, DROP, and ALTER.

     Using DDL statements, we can:
     • Create a new database
     • Define and create a new table
     • Remove a table that's no longer needed
     • Change the definition of an existing table
     • Define a virtual table (or view) of data
     • Establish security controls for a database
     • Build an index to make table access faster
     • Control the physical storage of data by the DBMS

     CREATE statement: It defines and creates a database object, such as a database or a table.
     DROP statement: It removes an existing database object.
     ALTER statement: It changes the definition of a database object, e.g. modifying the structure of a table.

     How do these DDL statements help the user?
     The DDL statements hide the low-level details of how data is physically stored in the database. They
     manipulate abstract database objects, such as tables and columns.

2    Explain the process of creation of a database.
     A database is created with the CREATE DATABASE statement.

     Syntax:
     CREATE DATABASE database_name

     Example : Create a database called ABCCO
     CREATE DATABASE ABCCO

3    Explain the CREATE TABLE statement.
     1. The CREATE TABLE statement is used to create a new table in a database.
     2. The columns of the newly created table are defined in the body of the CREATE TABLE statement.
     3. The column definitions appear in a comma-separated list enclosed in parentheses.
     4. The order of the column definitions determines the left-to-right order of the columns in the table.
     5. Every column in a table must have a unique name, but columns in other tables may have the
         same name
     6. The data type of the column, identifies the kind of data that the column stores.
     7. If a column must contain certain data, we use the NOT NULL clause..
     8. We can specify an optional default value for the column. The DBMS uses this value when an
         INSERT statement for the table does not specify a value for the column.
     9. We can also specify that a particular column is a primary key.
     10. We can also specify the data values or range (domain) that a column may take.

     Example 1:
     Create a table called "Persons" that contains four columns: Id, LastName, FirstName, and City.

     CREATE TABLE Persons
     (
           Id int,
           LastName varchar(255),
           FirstName varchar(255),
           City varchar(255)
     )

Prof. Mukesh N. Tekwani [9869 488 356]                                                                           Page 1
SQL - Ch 12 – Creating a Database

    Example 2:
    Create the OFFICES table with the following restrictions on data:
    OFFICE is a non-null integer value; city and region cannot be left blank and SALES is not null.

    CREATE TABLE OFFICES
    (
          OFFICE INTEGER NOT NULL,
          CITY VARCHAR(15) NOT NULL,
          REGION VARCHAR(10) NOT NULL,
          SALES MONEY NOT NULL
    )

4   How are missing and default values handled in a table?
    The definition of each column within a table tells the DBMS whether the data for the column is allowed to be a
    NULL value. In the SQL standard, the default is to allow missing data for a column.

    If the column must contain a data value for every row of a table, then its definition must include the NOT
    NULL clause. Microsoft SQL Server use the opposite convention, assuming that NULL values are not allowed
    unless the column is explicitly declared NULL.

    The SQL2 standard and many of the major SQL DBMS products support default values for columns. If a
    column has a default value, it is specified within the column definition.

    Example Here is a a CREATE TABLE statement for the OFFICES table that specifies default values:

    Define the OFFICES table with default values.

    CREATE TABLE OFFICES
    (
          OFFICE INTEGER NOT NULL,
          CITY VARCHAR(15) NOT NULL,
          REGION VARCHAR(10) NOT NULL DEFAULT 'Eastern',
          TARGET MONEY DEFAULT NULL,
          SALES MONEY NOT NULL DEFAULT 0.00
    )

    With this table definition, the office number and the city must be specified when you insert a new office. The
    region defaults to Eastern, the sales to zero, and the target to NULL. Note that the target would default to
    NULL even without the DEFAULT NULL specification.

5   What are primary and foreign key definitions?
    PRIMARY KEY
    Every row of a table must contain a unique value (or a group of values) that identifies it uniquely. This unique
    value is called as the primary key.
    • The CREATE TABLE statement can be used to identify the primary key of a table.
    • The PRIMARY KEY clause specifies the column or columns that form the primary key for the
        table.
    • The primary key value be unique in every row of the table.
    • The column definition for every column in the primary key must specify that the column is NOT
        NULL.

    Example:
    CREATE TABLE Customer
    (
          CustID integer PRIMARY KEY,
          LastName varchar(30),
          FirstName varchar(30)
    )


Page 2                                                                            mukeshtekwani@hotmail.com
SQL – Ch 12 – Creating a Database

     FOREIGN KEY
     A foreign key is a column (or columns) that points to the primary key of another table. The purpose of
     the foreign key is to ensure referential integrity of the data. In other words, only values that are
     supposed to appear in the database are permitted.
                                                       OR
     A set of columns in a table that corresponds to the primary key in another table is called as a foreign
     key.

     •   The FOREIGN KEY clause specifies a foreign key in a table and the relationship that it creates
         with tables in the database.
     •   The FOREIGN KEY clause specifies the table that is referenced by the foreign key. This is the
         parent table in the relationship; the table being defined is the child.
     •   It specifies how the DBMS should treat a NULL value in one or more columns of the foreign key,
         when matching it against rows of the parent table.
     •   It specifies an optional delete rule for the relationship (CASCADE, SET NULL, SET DEFAULT),
         which determines the action to take when a parent row is deleted.
     •   An optional update rule for the relationship which determines the action to take when part of the
         primary key in a parent row is updated.
     •   An optional check constraint, which restricts the data in the table so that its rows meet a
         specified search condition.

     Example:
     Suppose we have two tables, a CUSTOMER table that includes all customer data, and an ORDERS
     table that includes all customer orders. The constraint here is that all orders must be associated with
     a customer that is already in the CUSTOMER table. We place a foreign key on the ORDERS table
     and have it relate to the primary key of the CUSTOMER table. This way, we can ensure that all
     orders in the ORDERS table are related to a customer in the CUSTOMER table. In other words, the
     ORDERS table cannot contain information on a customer that is not in the CUSTOMER table.

     The structure of these two tables will be as follows:

     Table CUSTOMER (Parent table)
     column name        Characteristic
     CustID             Primary Key
     LastName
     FirstName

     Table ORDERS (Child table)
     column name        characteristic
     Order_ID           Primary Key
     Order_Date
     CustID             Foreign Key
     Amount
     In the above example, the CustID column in the ORDERS table is a foreign key pointing to the
     CustID column in the CUSTOMER table.

     CREATE TABLE ORDERS
     (
           Order_ID integer PRIMARY KEY,
           Order_Date datetime,
           CustID integer REFERENCES CUSTOMER(CustID),
           Amount double
     )



Prof. Mukesh N. Tekwani [9869 488 356]                                                              Page 3
SQL - Ch 12 – Creating a Database

    Example 2:
    For the diagram below, define the ORDERS table




             Primary                       Foreign   Forn.     Foreign key
               key                           key      key



    CREATE TABLE ORDERS
    (
          ORDER_NUM INTEGER NOT NULL,
          ORDER_DATE DATE NOT NULL,
          CUST INTEGER NOT NULL,
          REP INTEGER,
          MFR CHAR(3) NOT NULL,
          PRODUCT CHAR(5) NOT NULL,
          QTY INTEGER NOT NULL,
          AMOUNT MONEY NOT NULL,
          PRIMARY KEY (ORDER_NUM),
          CONSTRAINT PLACEDBY       /* this is the name given to the relationship */
          FOREIGN KEY (CUST)
          REFERENCES CUSTOMERS
          ON DELETE CASCADE,
          CONSTRAINT TAKENBY        /* this is the name given to the relationship */
          FOREIGN KEY (REP)
          REFERENCES SALESREPS
          ON DELETE SET NULL,
          CONSTRAINT ISFOR          /* this is the name given to the relationship */
          FOREIGN KEY (MFR, PRODUCT)
          REFERENCES PRODUCTS
          ON DELETE RESTRICT
    )

    When the DBMS processes the CREATE TABLE statement, it checks each foreign key definition
    against the definition of the table that it references (the parent table). The foreign key and the primary
    key of the referenced table must agree in the number of columns they contain and their data types.
Page 4                                                                        mukeshtekwani@hotmail.com
SQL – Ch 12 – Creating a Database

     The referenced table (i.e., the parent table) must already be defined in the database for this checking
     to succeed.

     The FOREIGN KEY clause also specifies the delete and update rules that are to be enforced for the
     parent/child table relationship that it creates.

6    What are check constraints?
     Check constraint is used to limit the value or range of values that can be stored in a column. If we
     define a CHECK constraint on a single column it allows only certain values for this column. For
     example, the GENDER column in EMPLOYEES table may only contain two values M and F. Such
     values are specified with the check constraint.

     Example 1: Create a table called EMPLOYEE and impose the restriction on the GENDER column
     such that it can only contain the values M or F.

     CREATE TABLE Employee
     (
           EmpId INTEGER NOT NULL,
           LName VARCHAR (40) NOT NULL,
           FName VARCHAR (40) NOT NULL,
           Gender CHAR(1) NOT NULL CHECK (GENDER = 'M' OR GENDER = 'F')
     )

     Example 2: Create a table called EMPLOYEE and impose the restriction that the date of birth should
                   st
     be more than 1 January 1990 and gender must be M or F.

     CREATE TABLE Employee
     (
           EmpId INTEGER NOT NULL,
           LName VARCHAR (40) NOT NULL,
           FName VARCHAR (40) NOT NULL,
           DOB DATETIME CHECK (DOB > '01-01-1990'),
           Gender CHAR(1) NOT NULL CHECK (GENDER = 'M' OR GENDER = 'F')
     )

     Example 3: Create a table called EMPLOYEE and impose the restrictions that the date of joining
     should be more than the DOB and Gender should be M or F.

     CREATE TABLE Employee
     (
           EmpId INTEGER NOT NULL,
           LName VARCHAR (40) NOT NULL,
           FName VARCHAR (40) NOT NULL,
           DOB DATETIME CHECK (DOB > '01-01-1990'),
           DOJ DATETIME,
           Gender CHAR(1) NOT NULL CHECK (GENDER = 'M' OR GENDER = 'F'),
           CHECK (YEAR(DOJ) > YEAR(DOB))
     )

     Note: We have put the constraint on DOJ at the end of the table definition, and not in the same line
     as DOJ. This is because if a constraint is specified in which two or more columns of a table
     compared with each other, then the column constraint must be specified as a table constraint.


     Naming a Constraint:
     We can give a name to a constraint. For example, in the above example (Ex 3), we can call the
     constraint on DOB as VALID _DOB and the constraint on DOJ as VALID_DOJ. Then the above table
     definition will be written as:


Prof. Mukesh N. Tekwani [9869 488 356]                                                              Page 5
SQL - Ch 12 – Creating a Database

    CREATE TABLE Employee
    (
          EmpId INTEGER NOT NULL,
          LName VARCHAR (40) NOT NULL,
          FName VARCHAR (40) NOT NULL,
          DOB DATETIME
          CONSTRAINT VALID_DOB
          CHECK (DOB >= '01-01-1980'),
          DOJ DATETIME,
          Gender CHAR(1) NOT NULL
          CONSTRAINT VALID_GENDER
          CHECK (GENDER = 'M' OR GENDER = 'F'),
          CONSTRAINT VALID_DOJ
          CHECK (YEAR(DOJ) > YEAR(DOB))
    )

    Here the names of the constraints are VALID_DOB, VALID_GENDER, and VALID_DOJ. The last
    constraint has not been given along with the DOJ statement as it references another column of the
    same table. So the VALID_DOJ constraint becomes a table constraint.

    (Note the line CHECK (GENDER = 'M' OR GENDER = 'F'),
    Can also be written as :
    CHECK (GENDER IN ('M', 'F')),

7   Explain how a table can be removed from a database. (DROP)
    The DROP TABLE statement is used to delete a table from a database. SQL removes the
    description of the table, all constraints, indexes and data associated with the table.

    Syntax: DROP TABLE tablename

    Example: DROP TABLE EMPLOYEE

    The DROP TABLE statement can include CASCADE. If this keyword is specified, and if other
    database objects refer to this table, the DROP TABLE statement will fail.

8   Describe the ALTER statement, with suitable examples.
    The ALTER statement is used to change the structure of a table. It can be used to:
    • Add a column
    • Delete a column
    • Change the default value for a column
    • Add or drop a primary key for the table
    • Add or drop a new foreign key for a table
    • Add or drop a check constraint for a table
    • But, the NOT NULL clause cannot be written when adding a new column with ALTER statement.

    Adding a column with ALTER statement:

    Example:
    ALTER TABLE Employee
    ADD DeptID Integer DEFAULT 1

    Dropping a column with ALTER statement:
    The ALTER TABLE statement can be used to drop one or more columns from an existing table.

    Example:
    ALTER TABLE Employee
    DROP COLUMN DeptId


Page 6                                                                 mukeshtekwani@hotmail.com
SQL – Ch 12 – Creating a Database

     If we attempt to drop a column that is being referred in a check constraint, it will cause an error and
     the column is not dropped.

     RESTRICT clause:

     Example:
     ALTER TABLE Employee
     DROP MgrID RESTRICT

     If any other objects in the database depend on the column to be dropped, the ALTER TABLE
     statement fails with an error and the column is not dropped.

     CASCADE clause:
     If any other objects in the database depend on the column, they are also dropped.

     Changing the PRIMARY and FOREIGN Key:
     The ALTER statement can be used to add or change primary and foreign key definitions in a table.

     Example 1:
     Make the EmpID in the Employee table as the primary key.

     ALTER TABLE Employee
     ADD PRIMARY KEY (EmpID)

     Example 2:
     Add a column called DeptID with the default value 100.

     ALTER TABLE Employee
     ADD DeptId INTEGER DEFAULT 100

     Example 3:
     Drop the Primary Key EmpID

     ALTER TABLE Employee
     DROP PRIMARY KEY (EmpID)

9    What is a table alias (synonym)? How is it created and dropped?
     An alias is an alternate name for a table or a column. If a table name is very long e.g.,
     Employee.Info, then it can become difficult to write this repeatedly in queries. If a query refers to
     another user's table, the table name can become difficult to type as a column qualifier. Once an alias
     is defined for a table or column, the alias can be used instead of giving the complete table name.

     Example: List names, quotas, and birthdays of salespeople.

     SELECT SALESREPS.NAME, SALESREPS.QUOTA, SAM.BIRTHDAYS.BIRTH_DATE
     FROM SALESREPS, BIRTHDAYS
     WHERE SALESREPS.NAME = SAM.BIRTHDAYS.NAME

     This becomes easier to read and type when the aliases S and B are used for the two tables:
     SELECT S.NAME, S.QUOTA, B.BIRTH_DATE
     FROM SALESREPS S, SAM.BIRTHDAYS B
     WHERE S.NAME = B.NAME

     The FROM clause specifies the tag (shown in bold letter) that is used to identify the table in the
     SELECT statement. If a table alias is specified, it becomes the table tag; otherwise, the table's name,
     exactly as it appears in the FROM clause, becomes the tag.

     Creating an Alias:

Prof. Mukesh N. Tekwani [9869 488 356]                                                                Page 7
SQL - Ch 12 – Creating a Database

     CREATE ALIAS REPS
     FOR JACK.SALESREPS

     CREATE ALIAS OFFICES
     FOR JILL.OFFICES

     Once the alias is defined, we can use the alias name in the query like this:
     SELECT NAME, REPS.SALES, OFFICE, OFFICES.SALES
     FROM REPS, OFFICES

     Dropping an alias:

     DROP ALIAS REP

     Now we can no longer use the alias REP to refer to the table JACK.SALESREPS.

10   What is an index? What are the advantages of an index? When should an index be avoided?
     1. An index in a database is similar to an index in a book. In an index a sorted list of the data from
        the table is kept along with pointers to the places where the data is found in the table.
     2. When a query is run, and an indexed field is referenced in the WHERE clause, the values are
        looked up in the index rather than the table itself. Then the database jumps immediately to the
        location in the table where the data is stored.
     3. Indexes are stored in a data structure called a B-tree which provides fast information retrieval.
     4. When an index is created, all the data in the column being indexed is stored within the B-tree.
     5. Whenever the table is modified (INSERT, DELETE, or UPDATE commands), the index is also
        updated.




     Example:
     CREATE INDEX FullName
     ON Employee (LName, FName)

     To drop an index, we use the DROP INDEX statement
     DROP INDEX FullName

     Advantages of an index:
     1. Data retrieval becomes fast if the table contains many records.
     2. Joining two tables using indexed columns in each table can be faster than joining non-indexed
        columns.
     3. If the WHERE clause is used frequently in queries, the index becomes more useful.

     Disadvantages of index:
     1. If the table is updated frequently, the index also has to be changed frequently and this can slow
        down the system.
     2. Index occupies disk space and so we should restrict the number of columns on which indexes
        are created.

11   Explain the single-database architecture.
     In single-database architecture the DBMS supports one system-wide database. Mainframe and
     minicomputer databases use this approach. Order processing, accounting, and payroll data are all
     stored in tables within the database. The major tables for each application are gathered together and
Page 8                                                                       mukeshtekwani@hotmail.com
SQL – Ch 12 – Creating a Database

     owned by a single user, who is probably the person in charge of that application on this computer.

     Advantage of single-database architecture:
     1. The tables in the various applications can easily reference one another. Users can run queries
        that combine data from the various applications.

     Disadvantage of single-database architecture:
        1. The database will grow huge over time as more and more applications are added to it.
        2. As database size grows, managing the database becomes more complicated as it involves,
            performing backups, recovering data, analyzing performance, etc.

12   Explain the multi-database architecture.
     1. In multi-database architecture, each database is assigned a unique name. MS SQL Server uses
        this architecture.
     2. Each database is dedicated for a particular application. E.g., there will be one database for
        accounting, another for employee details, yet another for orders received, etc.
     3. In this type of architecture, table names must be fully qualified. The table name must contain the
        database name, the name of the user and then the table name. This is done by using the “dot”
        notation. E.g., consider the EMPLOYEE table, owned by the user Anita, in the STAFF database.
        The fully qualified name of this table becomes: STAFF.ANITA.EMPLOYEE

     Advantages:
     1. It divides the data management tasks into smaller, more manageable pieces.
     2. Each person responsible for an application can now be the database administrator of their own
        database.
     3. When a new application is to be added, it can be developed in its own database, without
        disturbing the existing databases.
     4. Users and programmers can remember the overall structure of their own databases.

     Disadvantages:
     1. The main disadvantage of the multi-database architecture is that the individual databases may
        become "islands" of information, since they are not connected to one another.
     2. A table in one database cannot contain a foreign key reference to a table in a different database.
     3. DBMS does not support queries across database boundaries, making it impossible to relate data
        from two applications.

13   Explain multi-location architecture.
     1. A multi-location architecture supports multiple databases and uses the computer system's
        directory structure to organize them.
     2. Each application is assigned to its own database. Each database has a name, but two different
        databases in two different directories can have the same name.

     Advantages:
     1. The major advantage of the multi-location architecture is flexibility.
     2. It is especially appropriate in applications such as engineering and design, where many users of
        the computer system may want to use several databases to structure their own information.

     Disadvantages:
     1. The disadvantages of the multi-location architecture are the same as those of the multi-database
        architecture.
     2. The DBMS typically doesn't know about all of the databases that have been created, which may
        be spread throughout the system's directory structure. There is no "master database" that keeps
        track of all the databases, which makes centralized database administration very difficult.
     3. Access to a database is more complex, because both the name of the database and its location
        in the directory hierarchy must be specified.




Prof. Mukesh N. Tekwani [9869 488 356]                                                             Page 9

More Related Content

What's hot (19)

SQL
SQLSQL
SQL
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
SQL & PLSQL
SQL & PLSQLSQL & PLSQL
SQL & PLSQL
 
Database queries
Database queriesDatabase queries
Database queries
 
Ankit
AnkitAnkit
Ankit
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
Intro to tsql unit 7
Intro to tsql   unit 7Intro to tsql   unit 7
Intro to tsql unit 7
 
Assignment#04
Assignment#04Assignment#04
Assignment#04
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Intro to T-SQL - 1st session
Intro to T-SQL - 1st sessionIntro to T-SQL - 1st session
Intro to T-SQL - 1st session
 
Partitioning tables and indexing them
Partitioning tables and indexing them Partitioning tables and indexing them
Partitioning tables and indexing them
 

Similar to Sql ch 12 - creating database

SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 
Difference Between Sql - MySql and Oracle
Difference Between Sql - MySql and OracleDifference Between Sql - MySql and Oracle
Difference Between Sql - MySql and OracleSteve Johnson
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
 
Unit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxUnit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxPetroJoe
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL UsedTheVerse1
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 
Relational database management system
Relational database management systemRelational database management system
Relational database management systemPraveen Soni
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginnersISsoft
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraintsVivek Singh
 
Sql overview-1232931296681161-1
Sql overview-1232931296681161-1Sql overview-1232931296681161-1
Sql overview-1232931296681161-1sagaroceanic11
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
 

Similar to Sql ch 12 - creating database (20)

Integrity and security
Integrity and securityIntegrity and security
Integrity and security
 
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
Sql commandsSql commands
Sql commands
 
Difference Between Sql - MySql and Oracle
Difference Between Sql - MySql and OracleDifference Between Sql - MySql and Oracle
Difference Between Sql - MySql and Oracle
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Unit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxUnit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptx
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
Module 3
Module 3Module 3
Module 3
 
Relational database management system
Relational database management systemRelational database management system
Relational database management system
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginners
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Sql overview-1232931296681161-1
Sql overview-1232931296681161-1Sql overview-1232931296681161-1
Sql overview-1232931296681161-1
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
SQL
SQLSQL
SQL
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 

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
 

Recently uploaded

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

Sql ch 12 - creating database

  • 1. SQL – Ch 12 – Creating a Database 12. CREATING A DATABASE 1 What are the DDL statements of SQL? The statements of SQL that permit the creation of a database, table, view are called the SQL Data Definition Language or DDL. These statements are: CREATE, DROP, and ALTER. Using DDL statements, we can: • Create a new database • Define and create a new table • Remove a table that's no longer needed • Change the definition of an existing table • Define a virtual table (or view) of data • Establish security controls for a database • Build an index to make table access faster • Control the physical storage of data by the DBMS CREATE statement: It defines and creates a database object, such as a database or a table. DROP statement: It removes an existing database object. ALTER statement: It changes the definition of a database object, e.g. modifying the structure of a table. How do these DDL statements help the user? The DDL statements hide the low-level details of how data is physically stored in the database. They manipulate abstract database objects, such as tables and columns. 2 Explain the process of creation of a database. A database is created with the CREATE DATABASE statement. Syntax: CREATE DATABASE database_name Example : Create a database called ABCCO CREATE DATABASE ABCCO 3 Explain the CREATE TABLE statement. 1. The CREATE TABLE statement is used to create a new table in a database. 2. The columns of the newly created table are defined in the body of the CREATE TABLE statement. 3. The column definitions appear in a comma-separated list enclosed in parentheses. 4. The order of the column definitions determines the left-to-right order of the columns in the table. 5. Every column in a table must have a unique name, but columns in other tables may have the same name 6. The data type of the column, identifies the kind of data that the column stores. 7. If a column must contain certain data, we use the NOT NULL clause.. 8. We can specify an optional default value for the column. The DBMS uses this value when an INSERT statement for the table does not specify a value for the column. 9. We can also specify that a particular column is a primary key. 10. We can also specify the data values or range (domain) that a column may take. Example 1: Create a table called "Persons" that contains four columns: Id, LastName, FirstName, and City. CREATE TABLE Persons ( Id int, LastName varchar(255), FirstName varchar(255), City varchar(255) ) Prof. Mukesh N. Tekwani [9869 488 356] Page 1
  • 2. SQL - Ch 12 – Creating a Database Example 2: Create the OFFICES table with the following restrictions on data: OFFICE is a non-null integer value; city and region cannot be left blank and SALES is not null. CREATE TABLE OFFICES ( OFFICE INTEGER NOT NULL, CITY VARCHAR(15) NOT NULL, REGION VARCHAR(10) NOT NULL, SALES MONEY NOT NULL ) 4 How are missing and default values handled in a table? The definition of each column within a table tells the DBMS whether the data for the column is allowed to be a NULL value. In the SQL standard, the default is to allow missing data for a column. If the column must contain a data value for every row of a table, then its definition must include the NOT NULL clause. Microsoft SQL Server use the opposite convention, assuming that NULL values are not allowed unless the column is explicitly declared NULL. The SQL2 standard and many of the major SQL DBMS products support default values for columns. If a column has a default value, it is specified within the column definition. Example Here is a a CREATE TABLE statement for the OFFICES table that specifies default values: Define the OFFICES table with default values. CREATE TABLE OFFICES ( OFFICE INTEGER NOT NULL, CITY VARCHAR(15) NOT NULL, REGION VARCHAR(10) NOT NULL DEFAULT 'Eastern', TARGET MONEY DEFAULT NULL, SALES MONEY NOT NULL DEFAULT 0.00 ) With this table definition, the office number and the city must be specified when you insert a new office. The region defaults to Eastern, the sales to zero, and the target to NULL. Note that the target would default to NULL even without the DEFAULT NULL specification. 5 What are primary and foreign key definitions? PRIMARY KEY Every row of a table must contain a unique value (or a group of values) that identifies it uniquely. This unique value is called as the primary key. • The CREATE TABLE statement can be used to identify the primary key of a table. • The PRIMARY KEY clause specifies the column or columns that form the primary key for the table. • The primary key value be unique in every row of the table. • The column definition for every column in the primary key must specify that the column is NOT NULL. Example: CREATE TABLE Customer ( CustID integer PRIMARY KEY, LastName varchar(30), FirstName varchar(30) ) Page 2 mukeshtekwani@hotmail.com
  • 3. SQL – Ch 12 – Creating a Database FOREIGN KEY A foreign key is a column (or columns) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted. OR A set of columns in a table that corresponds to the primary key in another table is called as a foreign key. • The FOREIGN KEY clause specifies a foreign key in a table and the relationship that it creates with tables in the database. • The FOREIGN KEY clause specifies the table that is referenced by the foreign key. This is the parent table in the relationship; the table being defined is the child. • It specifies how the DBMS should treat a NULL value in one or more columns of the foreign key, when matching it against rows of the parent table. • It specifies an optional delete rule for the relationship (CASCADE, SET NULL, SET DEFAULT), which determines the action to take when a parent row is deleted. • An optional update rule for the relationship which determines the action to take when part of the primary key in a parent row is updated. • An optional check constraint, which restricts the data in the table so that its rows meet a specified search condition. Example: Suppose we have two tables, a CUSTOMER table that includes all customer data, and an ORDERS table that includes all customer orders. The constraint here is that all orders must be associated with a customer that is already in the CUSTOMER table. We place a foreign key on the ORDERS table and have it relate to the primary key of the CUSTOMER table. This way, we can ensure that all orders in the ORDERS table are related to a customer in the CUSTOMER table. In other words, the ORDERS table cannot contain information on a customer that is not in the CUSTOMER table. The structure of these two tables will be as follows: Table CUSTOMER (Parent table) column name Characteristic CustID Primary Key LastName FirstName Table ORDERS (Child table) column name characteristic Order_ID Primary Key Order_Date CustID Foreign Key Amount In the above example, the CustID column in the ORDERS table is a foreign key pointing to the CustID column in the CUSTOMER table. CREATE TABLE ORDERS ( Order_ID integer PRIMARY KEY, Order_Date datetime, CustID integer REFERENCES CUSTOMER(CustID), Amount double ) Prof. Mukesh N. Tekwani [9869 488 356] Page 3
  • 4. SQL - Ch 12 – Creating a Database Example 2: For the diagram below, define the ORDERS table Primary Foreign Forn. Foreign key key key key CREATE TABLE ORDERS ( ORDER_NUM INTEGER NOT NULL, ORDER_DATE DATE NOT NULL, CUST INTEGER NOT NULL, REP INTEGER, MFR CHAR(3) NOT NULL, PRODUCT CHAR(5) NOT NULL, QTY INTEGER NOT NULL, AMOUNT MONEY NOT NULL, PRIMARY KEY (ORDER_NUM), CONSTRAINT PLACEDBY /* this is the name given to the relationship */ FOREIGN KEY (CUST) REFERENCES CUSTOMERS ON DELETE CASCADE, CONSTRAINT TAKENBY /* this is the name given to the relationship */ FOREIGN KEY (REP) REFERENCES SALESREPS ON DELETE SET NULL, CONSTRAINT ISFOR /* this is the name given to the relationship */ FOREIGN KEY (MFR, PRODUCT) REFERENCES PRODUCTS ON DELETE RESTRICT ) When the DBMS processes the CREATE TABLE statement, it checks each foreign key definition against the definition of the table that it references (the parent table). The foreign key and the primary key of the referenced table must agree in the number of columns they contain and their data types. Page 4 mukeshtekwani@hotmail.com
  • 5. SQL – Ch 12 – Creating a Database The referenced table (i.e., the parent table) must already be defined in the database for this checking to succeed. The FOREIGN KEY clause also specifies the delete and update rules that are to be enforced for the parent/child table relationship that it creates. 6 What are check constraints? Check constraint is used to limit the value or range of values that can be stored in a column. If we define a CHECK constraint on a single column it allows only certain values for this column. For example, the GENDER column in EMPLOYEES table may only contain two values M and F. Such values are specified with the check constraint. Example 1: Create a table called EMPLOYEE and impose the restriction on the GENDER column such that it can only contain the values M or F. CREATE TABLE Employee ( EmpId INTEGER NOT NULL, LName VARCHAR (40) NOT NULL, FName VARCHAR (40) NOT NULL, Gender CHAR(1) NOT NULL CHECK (GENDER = 'M' OR GENDER = 'F') ) Example 2: Create a table called EMPLOYEE and impose the restriction that the date of birth should st be more than 1 January 1990 and gender must be M or F. CREATE TABLE Employee ( EmpId INTEGER NOT NULL, LName VARCHAR (40) NOT NULL, FName VARCHAR (40) NOT NULL, DOB DATETIME CHECK (DOB > '01-01-1990'), Gender CHAR(1) NOT NULL CHECK (GENDER = 'M' OR GENDER = 'F') ) Example 3: Create a table called EMPLOYEE and impose the restrictions that the date of joining should be more than the DOB and Gender should be M or F. CREATE TABLE Employee ( EmpId INTEGER NOT NULL, LName VARCHAR (40) NOT NULL, FName VARCHAR (40) NOT NULL, DOB DATETIME CHECK (DOB > '01-01-1990'), DOJ DATETIME, Gender CHAR(1) NOT NULL CHECK (GENDER = 'M' OR GENDER = 'F'), CHECK (YEAR(DOJ) > YEAR(DOB)) ) Note: We have put the constraint on DOJ at the end of the table definition, and not in the same line as DOJ. This is because if a constraint is specified in which two or more columns of a table compared with each other, then the column constraint must be specified as a table constraint. Naming a Constraint: We can give a name to a constraint. For example, in the above example (Ex 3), we can call the constraint on DOB as VALID _DOB and the constraint on DOJ as VALID_DOJ. Then the above table definition will be written as: Prof. Mukesh N. Tekwani [9869 488 356] Page 5
  • 6. SQL - Ch 12 – Creating a Database CREATE TABLE Employee ( EmpId INTEGER NOT NULL, LName VARCHAR (40) NOT NULL, FName VARCHAR (40) NOT NULL, DOB DATETIME CONSTRAINT VALID_DOB CHECK (DOB >= '01-01-1980'), DOJ DATETIME, Gender CHAR(1) NOT NULL CONSTRAINT VALID_GENDER CHECK (GENDER = 'M' OR GENDER = 'F'), CONSTRAINT VALID_DOJ CHECK (YEAR(DOJ) > YEAR(DOB)) ) Here the names of the constraints are VALID_DOB, VALID_GENDER, and VALID_DOJ. The last constraint has not been given along with the DOJ statement as it references another column of the same table. So the VALID_DOJ constraint becomes a table constraint. (Note the line CHECK (GENDER = 'M' OR GENDER = 'F'), Can also be written as : CHECK (GENDER IN ('M', 'F')), 7 Explain how a table can be removed from a database. (DROP) The DROP TABLE statement is used to delete a table from a database. SQL removes the description of the table, all constraints, indexes and data associated with the table. Syntax: DROP TABLE tablename Example: DROP TABLE EMPLOYEE The DROP TABLE statement can include CASCADE. If this keyword is specified, and if other database objects refer to this table, the DROP TABLE statement will fail. 8 Describe the ALTER statement, with suitable examples. The ALTER statement is used to change the structure of a table. It can be used to: • Add a column • Delete a column • Change the default value for a column • Add or drop a primary key for the table • Add or drop a new foreign key for a table • Add or drop a check constraint for a table • But, the NOT NULL clause cannot be written when adding a new column with ALTER statement. Adding a column with ALTER statement: Example: ALTER TABLE Employee ADD DeptID Integer DEFAULT 1 Dropping a column with ALTER statement: The ALTER TABLE statement can be used to drop one or more columns from an existing table. Example: ALTER TABLE Employee DROP COLUMN DeptId Page 6 mukeshtekwani@hotmail.com
  • 7. SQL – Ch 12 – Creating a Database If we attempt to drop a column that is being referred in a check constraint, it will cause an error and the column is not dropped. RESTRICT clause: Example: ALTER TABLE Employee DROP MgrID RESTRICT If any other objects in the database depend on the column to be dropped, the ALTER TABLE statement fails with an error and the column is not dropped. CASCADE clause: If any other objects in the database depend on the column, they are also dropped. Changing the PRIMARY and FOREIGN Key: The ALTER statement can be used to add or change primary and foreign key definitions in a table. Example 1: Make the EmpID in the Employee table as the primary key. ALTER TABLE Employee ADD PRIMARY KEY (EmpID) Example 2: Add a column called DeptID with the default value 100. ALTER TABLE Employee ADD DeptId INTEGER DEFAULT 100 Example 3: Drop the Primary Key EmpID ALTER TABLE Employee DROP PRIMARY KEY (EmpID) 9 What is a table alias (synonym)? How is it created and dropped? An alias is an alternate name for a table or a column. If a table name is very long e.g., Employee.Info, then it can become difficult to write this repeatedly in queries. If a query refers to another user's table, the table name can become difficult to type as a column qualifier. Once an alias is defined for a table or column, the alias can be used instead of giving the complete table name. Example: List names, quotas, and birthdays of salespeople. SELECT SALESREPS.NAME, SALESREPS.QUOTA, SAM.BIRTHDAYS.BIRTH_DATE FROM SALESREPS, BIRTHDAYS WHERE SALESREPS.NAME = SAM.BIRTHDAYS.NAME This becomes easier to read and type when the aliases S and B are used for the two tables: SELECT S.NAME, S.QUOTA, B.BIRTH_DATE FROM SALESREPS S, SAM.BIRTHDAYS B WHERE S.NAME = B.NAME The FROM clause specifies the tag (shown in bold letter) that is used to identify the table in the SELECT statement. If a table alias is specified, it becomes the table tag; otherwise, the table's name, exactly as it appears in the FROM clause, becomes the tag. Creating an Alias: Prof. Mukesh N. Tekwani [9869 488 356] Page 7
  • 8. SQL - Ch 12 – Creating a Database CREATE ALIAS REPS FOR JACK.SALESREPS CREATE ALIAS OFFICES FOR JILL.OFFICES Once the alias is defined, we can use the alias name in the query like this: SELECT NAME, REPS.SALES, OFFICE, OFFICES.SALES FROM REPS, OFFICES Dropping an alias: DROP ALIAS REP Now we can no longer use the alias REP to refer to the table JACK.SALESREPS. 10 What is an index? What are the advantages of an index? When should an index be avoided? 1. An index in a database is similar to an index in a book. In an index a sorted list of the data from the table is kept along with pointers to the places where the data is found in the table. 2. When a query is run, and an indexed field is referenced in the WHERE clause, the values are looked up in the index rather than the table itself. Then the database jumps immediately to the location in the table where the data is stored. 3. Indexes are stored in a data structure called a B-tree which provides fast information retrieval. 4. When an index is created, all the data in the column being indexed is stored within the B-tree. 5. Whenever the table is modified (INSERT, DELETE, or UPDATE commands), the index is also updated. Example: CREATE INDEX FullName ON Employee (LName, FName) To drop an index, we use the DROP INDEX statement DROP INDEX FullName Advantages of an index: 1. Data retrieval becomes fast if the table contains many records. 2. Joining two tables using indexed columns in each table can be faster than joining non-indexed columns. 3. If the WHERE clause is used frequently in queries, the index becomes more useful. Disadvantages of index: 1. If the table is updated frequently, the index also has to be changed frequently and this can slow down the system. 2. Index occupies disk space and so we should restrict the number of columns on which indexes are created. 11 Explain the single-database architecture. In single-database architecture the DBMS supports one system-wide database. Mainframe and minicomputer databases use this approach. Order processing, accounting, and payroll data are all stored in tables within the database. The major tables for each application are gathered together and Page 8 mukeshtekwani@hotmail.com
  • 9. SQL – Ch 12 – Creating a Database owned by a single user, who is probably the person in charge of that application on this computer. Advantage of single-database architecture: 1. The tables in the various applications can easily reference one another. Users can run queries that combine data from the various applications. Disadvantage of single-database architecture: 1. The database will grow huge over time as more and more applications are added to it. 2. As database size grows, managing the database becomes more complicated as it involves, performing backups, recovering data, analyzing performance, etc. 12 Explain the multi-database architecture. 1. In multi-database architecture, each database is assigned a unique name. MS SQL Server uses this architecture. 2. Each database is dedicated for a particular application. E.g., there will be one database for accounting, another for employee details, yet another for orders received, etc. 3. In this type of architecture, table names must be fully qualified. The table name must contain the database name, the name of the user and then the table name. This is done by using the “dot” notation. E.g., consider the EMPLOYEE table, owned by the user Anita, in the STAFF database. The fully qualified name of this table becomes: STAFF.ANITA.EMPLOYEE Advantages: 1. It divides the data management tasks into smaller, more manageable pieces. 2. Each person responsible for an application can now be the database administrator of their own database. 3. When a new application is to be added, it can be developed in its own database, without disturbing the existing databases. 4. Users and programmers can remember the overall structure of their own databases. Disadvantages: 1. The main disadvantage of the multi-database architecture is that the individual databases may become "islands" of information, since they are not connected to one another. 2. A table in one database cannot contain a foreign key reference to a table in a different database. 3. DBMS does not support queries across database boundaries, making it impossible to relate data from two applications. 13 Explain multi-location architecture. 1. A multi-location architecture supports multiple databases and uses the computer system's directory structure to organize them. 2. Each application is assigned to its own database. Each database has a name, but two different databases in two different directories can have the same name. Advantages: 1. The major advantage of the multi-location architecture is flexibility. 2. It is especially appropriate in applications such as engineering and design, where many users of the computer system may want to use several databases to structure their own information. Disadvantages: 1. The disadvantages of the multi-location architecture are the same as those of the multi-database architecture. 2. The DBMS typically doesn't know about all of the databases that have been created, which may be spread throughout the system's directory structure. There is no "master database" that keeps track of all the databases, which makes centralized database administration very difficult. 3. Access to a database is more complex, because both the name of the database and its location in the directory hierarchy must be specified. Prof. Mukesh N. Tekwani [9869 488 356] Page 9