sql ch 12 - creating database

9
SQL Ch 12 Creating a Database Prof. Mukesh N. Tekwani [9869 488 356] Page 1 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) )

Upload: mukesh-tekwani

Post on 20-Jan-2015

1.539 views

Category:

Education


0 download

DESCRIPTION

SQL

TRANSCRIPT

Page 1: Sql ch 12 - creating database

SQL – Ch 12 – Creating a Database

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

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) )

Page 2: Sql ch 12 - creating database

SQL - Ch 12 – Creating a Database

Page 2 [email protected]

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 3: Sql ch 12 - creating database

SQL – Ch 12 – Creating a Database

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

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

)

Page 4: Sql ch 12 - creating database

SQL - Ch 12 – Creating a Database

Page 4 [email protected]

Example 2:

For the diagram below, define the ORDERS table

Primary

key

Foreign

key

Forn.

key

Foreign 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 5: Sql ch 12 - creating database

SQL – Ch 12 – Creating a Database

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

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 be more than 1

st 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:

Page 6: Sql ch 12 - creating database

SQL - Ch 12 – Creating a Database

Page 6 [email protected]

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 7: Sql ch 12 - creating database

SQL – Ch 12 – Creating a Database

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

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:

Page 8: Sql ch 12 - creating database

SQL - Ch 12 – Creating a Database

Page 8 [email protected]

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 9: Sql ch 12 - creating database

SQL – Ch 12 – Creating a Database

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

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.