Vous êtes sur la page 1sur 6

Indian Institute of Information Technology, Allahabad.

Course : DBMS Instructor : Vijay Kr. Chaurasiya Batch : MBA 1st Semester Lab Assignment #3 Deadline : 03/08/2011

Note: Some of the queries will not work in MYSQL so you are free to try those queries in ORACLE. The tutorial in the assignment is for SQL but you can find equivalent syntax in MYSQL. You have to practice this assignment on MYSQL server or ORACLE if available.

Tutorial
SQL statements are divided into two major categories: Data Definition Language (DDL) and data manipulation language (DML). In this lab, we will deal with the Data Definition Language . DDL statements are used to build and modify the structure of the tables and other objects in the database. When we execute a DDL statement, it takes effect immediately. A reference for major DML statements in SQL is given below.The Data Definition Language (DDL) permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are:

CREATE TABLE - creates a new database table.

CREATE VIEW- creates a new database view. CREATE OR REPLACE VIEW- creates/replace a database view with new name if it already exists. CREATE INDEX - creates an index (search key). ALTER TABLE - alters (changes) a database table. DROP TABLE - deletes a database table. DROP VIEW- deletes a database view. DROP INDEX - deletes an index (search key). CREATE TABLE statement is used to create tables. CREATE TABLE table_name ( col_name1 datatype1 [NOT NULL | NULL]

[{, col_name2

datatype2

[NOT NULL | NULL]} ] );

Example 1: Create Student table (single attribute PRIMARY KEY) with implicit naming of constraints. CREATE TABLE student ( student_id CHAR(11) PRIMARY KEY, lname VARCHAR(20), fname VARCHAR(20), middeinit CHAR(2), dept_id NUMBER ); Note: Name for PRIMARY KEY constraint will be given by system itself. We may face problem to trace PRIMARY KEY constraint definition later. Example 2: Create Student table (composite attribute PRIMARY KEY) with explicit naming of constraints. CREATE TABLE student ( student_id CHAR(11), lname VARCHAR(20), fname VARCHAR(20), middeinit CHAR(2), dept_id NUMBER, constraint student_pk PRIMARY KEY(student_id,dept_id) );

Note: In the above example, Name of PRIMARY KEY constraint i.e. student_pk is given by the user itself. We would not face any problem to trace PRIMARY KEY constraint

definition later because we know the name beforehand.

Example 3: Create Course table with FOREIGN KEY (i.e. student_id references to Student table) with implicit naming. CREATE TABLE COURSE ( course_no NUMBER PRIMARY KEY, course_name VARCHAR(20), student_id CHAR(11), FOREIGN KEY (student_id) REFERENCES student );

Example 4: Create Course table with FOREIGN KEY with explicit naming i.e. student_id references to Student table. CREATE TABLE COURSE ( course_no NUMBER , course_name VARCHAR(20), student_id CHAR(11) , constraint student_pk PRIMARY KEY(student_id), constraint student_fk FOREIGN KEY (student_id) references Student); Often columns in a table must have values that are within a certain range or that satisfy certain conditions. Check constraints allow users to restrict possible attribute values for a column toadmissible ones. They can be specified as column constraints or table constraints. The syntax for a check constraint is [constraint <name>] check(<condition>) CREATE VIEW statement is used to create views. Views are virtual tables that represent the data in one or more tables. You can use these virtual tables just as any real tables. Views are important to maintain logical data independence and can be used as security mechanisms by granting permission on a view but not on the underlying (base) tables. CREATE VIEW view_name AS Select_statement Example 1: create a students_view. Only list student_id, lname and fname CREATE VIEW students_view AS SELECT student_id, lname, fname FROM students;

There are many CREATE variants like CREATE SYNONYM, CREATE INDEX and CREATE ROLE (self reading). DROP TABLE and DROP VIEW statements are used to remove tables and views from the database. DROP TABLE table_name; DROP VIEW view_name; Example 1: Remove students_view and employee table from your own database. DROP VIEW students_view;

DROP TABLE employee; The ALTER TABLE statement is used to modify the schema of a table. You can also add or remove a constraint from a table. ALTER TABLE table_name ADD col_name data type [NULL | NOT NULL] /*adding single column*/ ADD( col_name datatype [NULL | NOT NULL] , col_name type [NULL | NOT NULL] ); /*adding multiple column*/ DROP COLUMN col_name ; single column*/ DROP(col_name, col_name, ....); columns*/ MODIFY col_name datatype [NULL | NOT NULL]; /*droping /*droping multiple /*modifying single

columns*/ MODIFY(col_name datatype [NULL | NOT NULL], col_name type [NULL | NOT NULL].); /*modifying multiple columns*/ Note: Only the owner of the table, the database owner and the system administrator have the privilege to modify the schema of a table. Example 1: Add a Column: Add a new column phone into department table. ALTER TABLE department ADD phone CHAR(12) NULL Example 2: Delete a column: Delete the column, phone, from the department table. ALTER TABLE department DROP COLUMN phone

Example 3: Change the data type of a column: Originally, the building column is of VARCHAR(20) type and NULLable. Alter its data type to be CHAR(35) and NOT NULLable. ALTER TABLE department MODIFY building CHAR(35) NOT NULL Changing Integrity Constraints

ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition; [,CONSTRAINT constraint_name constraint_definition.] DROP CONSTRAINT constraint_name constraint_definition; [,CONSTRAINT constraint_name constraint_definition.]

Modify the department table. Add primary key constraint on dept_id and a constraint to make building and building_no unique. ALTER TABLE department2 ADD CONSTRAINT pri_department PRIMARY KEY(dept_id),

CONSTRAINT uni_building UNIQUE (building, building_no) Remove the constraints that you created ALTER TABLE department DROP CONSTRAINT pri_department, CONSTRAINT uni_building ;

Assignment
1. We want to create a table called PROJECT to store information about projects. For each

project, we want to store the number and the name of the project, the employee number of the projects manager, the budget and the number of persons working on the
project, and the start date and end date of the project. Furthermore, we have the following conditions:

- a project is identified by its project number, - the name of a project must be unique,

- the manager and the budget must be defined.


- no two projects have the same start and end date

2. Create a table which has the name of an employee consisting of only upper case letters, the minimum salary of an employee is 500; department numbers must range between 10 and 100

3. Alter table EMP (created in Lab2) having DEPTNO as a foreign key from DEPT Table
and add column COMMISSION 4. Add an integrity constraint to table EMP using ALTER command which requires that each manager must earn more than 4000

5. Alter table EMPLOYEE. Add the check constraint named REVENUE defined so that each employee must make a total of salary and commission greater than $30,000. 6. Alter table EMPLOYEE. Drop the constraint REVENUE which was previously defined. 7. Alter the EMPLOYEE table to add 4 new columns with default values. 8. Create a table called EQUIPMENT exists defined with the following columns:
Column Name
EQUIP_NO

Data Type
INT

EQUIP_DESC LOCATION
EQUIP_OWNER

VARCHAR(50) VARCHAR(50)
CHAR(3)

Add an additional column EQUIP_QTY column should have a value of 1 and must never be null. 9. Create a View listing the name and salary of employees of the department 20 who are leading a project that started before December 31, 1990 10. Create a view which contains name as empname, job title as designation and the annual salary as yearlysal of employees working in the department 20

Vous aimerez peut-être aussi