Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views10 pages

Dbms

The document outlines the design and development of SQL DDL statements for creating and managing database objects such as tables, views, indexes, sequences, and synonyms. It includes detailed SQL code for creating tables for departments, courses, students, and faculty, along with examples of data insertion, alteration, and querying. Additionally, it discusses the use of views and indexes, providing insights into their functionality and limitations.

Uploaded by

snehasalunke2827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Dbms

The document outlines the design and development of SQL DDL statements for creating and managing database objects such as tables, views, indexes, sequences, and synonyms. It includes detailed SQL code for creating tables for departments, courses, students, and faculty, along with examples of data insertion, alteration, and querying. Additionally, it discusses the use of views and indexes, providing insights into their functionality and limitations.

Uploaded by

snehasalunke2827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Name of Student : Sachin Santosh Kokare Roll No: 32431

Design and Develop SQL DDL statements which demonstrate the use of SQL objects such as Table,
View, Index, Sequence and Synonym

Create
Create table of department, course and student
1. Department {D_Id, D_name, Year_of_establishment, Enrollement_no} :
CREATE TABLE Department ( D_Id INT AUTO_INCREMENT PRIMARY KEY, D_name
VARCHAR(10) NOT NULL, Year_of_establishment YEAR NOT NULL CHECK
(Year_of_establishment >= 1900 AND Year_of_establishment <= YEAR(CURDATE())),
Enrollment_no INT NOT NULL CHECK (Enrollment_no > 0) );

2. Course {Course_Id, Course_name, credits, D_Id} :


Note: Add referential integrity, verify domain integrity
CREATE TABLE Course (Course_Id INT AUTO_INCREMENT PRIMARY KEY,
Course_name VARCHAR(10) NOT NULL, Credits INT NOT NULL CHECK (Credits
BETWEEN 1 AND 10), D_Id INT NOT NULL, FOREIGN KEY (D_Id) REFERENCES
Department(D_Id) );

3. Student {S_Id, F_name, L_name, Gender, DOB, City, Mobile, D_Id}


Note: add Primary key and foreign key, auto_increment, Not null & check constraint :
CREATE TABLE Student (S_Id INT AUTO_INCREMENT PRIMARY KEY, F_name
VARCHAR(10) NOT NULL, L_name VARCHAR(10) NOT NULL, Gender VARCHAR(7)
NOT NULL, DOB DATE NOT NULL, City VARCHAR(10) NOT NULL, Mobile
VARCHAR(10) NOT NULL, D_Id INT NOT NULL, FOREIGN KEY (D_Id) REFERENCES
Department(D_Id));
Name of Student : Sachin Santosh Kokare Roll No: 32431

Describe
Describe the all tables
1. Department:

2. Course :

3. Student :
Name of Student : Sachin Santosh Kokare Roll No: 32431

Drop and Truncate


Mention an example on drop and truncate
1. Drop :
Drop table student ;

2. Truncate:
Truncate student;

ALTER
Create Faculty table
Faculty {E_no, F_name, L_name, Gender, Date of Joning, Course_Id} :
CREATE TABLE Faculty (E_no INT AUTO_INCREMENT PRIMARY KEY, F_name
VARCHAR(10) NOT NULL, L_name VARCHAR(10) NOT NULL, Gender VARCHAR(7)
NOT NULL, Date_of_Joining DATE NOT NULL , Course_Id INT NOT NULL, FOREIGN KEY
(Course_Id) REFERENCES Course(Course_Id));

Write a query on above table by using ALTER with ADD, MODIFY, DROP and RENAME. Write a
query to add foreign key constraint
1. Alter :
ALTER TABLE Faculty ADD D_Id INT;
Name of Student : Sachin Santosh Kokare Roll No: 32431

ALTER TABLE Faculty ADD CONSTRAINT fk_faculty_department FOREIGN KEY


(D_Id) REFERENCES Department(D_Id);

ALTER TABLE Faculty MODIFY F_name VARCHAR(15) NOT NULL;

ALTER TABLE Faculty DROP COLUMN Gender;

ALTER TABLE Faculty RENAME COLUMN Date_of_Joining TO Joining_Date;

Faculty :

INSERT
Write a query using syntax Insert in different way
Single record / multiple record / value in some of the attributes
1. Department :
INSERT INTO Department (D_name, Year_of_establishment, Enrollment_no) VALUES
('ENTC', 1980, 120), ('CS', 1982, 150), ('IT', 1980, 100), ('AIDS', 2000, 80);

2. Course :
INSERT INTO Course (Course_Id, Course_name, Credits, D_Id) VALUES (1, 'DE', 4,
1), (2, 'MC', 3, 1), (3, 'DSA', 4, 2), (4, 'OS', 3, 2), (5, 'Dbms', 4, 3), (6, 'WebDev', 3, 3), (7,
'AI & ML', 4, 4), (8, 'CybSec', 3, 4);

3. Students:
INSERT INTO Student (F_name, L_name, Gender, DOB, City, Mobile, D_Id) VALUES
('AmitKumar ', 'Sharma ', 'Male', '2001-05-12', 'Mumbai', '9876543210', 1), ('Ritika ',
'Verma ', 'Female', '2002-08-20', 'Delhi', '9123456789', 2), ('Suresh ', 'Patil ', 'Male',
'2000-11-03', 'Pune', '9012345678', 3), ('NehaRani ', 'Joshi ', 'Female', '2001-03-18',
'Nagpur', '9988776655', 4), ('Vikas ', 'Deshmukh ', 'Male', '1999-12-25', 'Nashik',
'9090909090', 1), ('Kavita ', 'Pandey ', 'Female', '2003-06-30', 'Lucknow',
'9876501234', 2), ('Manoj ', 'Yadav ', 'Male', '2000-01-10', 'Bhopal', '9123498765',
3), ('Anjali ', 'Shinde ', 'Female', '2002-07-22', 'Aurangabad', '9998887776', 4),
('Rohan ', 'Naik ', 'Male', '2001-10-05', 'Panaji', '9876123450', 1), ('Priyanka ',
Name of Student : Sachin Santosh Kokare Roll No: 32431

'Kapoor ', 'Female', '2002-04-14', 'Chennai', '9345612390', 2), ('Rahul ', 'Singh ',
'Male', '2000-09-17', 'Jaipur', '9988771122', 3), ('Sneha ', 'Mehta ', 'Female', '2001-
12-09', 'Indore', '9001122334', 4);

4. Faculty :
INSERT INTO Faculty (F_name, L_name, Gender, Joining_Date, Course_Id, D_Id)
VALUES ('Sandeep', 'Kumar', 'Male', '1980-06-01', 1, 1), ('Meenakshi', 'Joshi', 'Female',
'1980-08-12', 2, 1), ('Ravi', 'Verma', 'Male', '1984-01-20', 3, 2), ('Anita', 'Sharma',
'Female', '2010-03-15', 4, 2), ('Nikhil', 'Patel', 'Male', '2019-07-10', 5, 3), ('Priya', 'Rao',
'Female', '1985-02-25', 6, 3), ('Alok', 'Naidu', 'Male', '2001-09-05', 7, 4), ('Snehal', 'Desai',
'Female', '2002-11-18', 8, 4);

SELECT
Display the all records from department, course, student and faculty.
1. Department :
select * from department ;

2. Course :
select * from course;
Name of Student : Sachin Santosh Kokare Roll No: 32431

3. Students :
select * from student ;

4. Faculty :
select * from faculty ;

SEQUENCE
Example: Insert the records by auto_increment
INSERT INTO Student (F_name, L_name, Gender, DOB, City, Mobile, D_Id) VALUES
('Siddharth', 'Shetty', 'Male', '2002-01-10', 'Bengaluru', '9876543219', 1), ('Prajakta', 'Sawant',
'Female', '2001-09-05', 'Pune', '9123456701', 2), ('Tanmay', 'Bhagwat', 'Male', '2000-12-20',
'Nashik', '9988776654', 3), ('Sakshi', 'Chavan', 'Female', '2003-04-18', 'Nagpur', '9001122335', 4);
Name of Student : Sachin Santosh Kokare Roll No: 32431

VIEW
Create a view table on following statements.
1. Display the student id, student name and department name who enrolled in E&TC
Department.
CREATE VIEW ECT_Students AS SELECT S.S_Id, CONCAT(S.F_name, ' ',
S.L_name) AS Student_Name, D.D_name AS Department_Name FROM Student S JOIN
Department D ON S.D_Id = D.D_Id WHERE D.D_name = 'ENTC';
Name of Student : Sachin Santosh Kokare Roll No: 32431

2. Display the student id, student name, birth date and age of student.
CREATE VIEW Student_Age_Details AS SELECT S.S_Id, CONCAT(S.F_name, ' ',
S.L_name) AS Student_Name, S.DOB, TIMESTAMPDIFF(YEAR, S.DOB,
CURDATE()) AS Age FROM Student S;

Check if insert any value in view table created on single table and multiple table and observe the
error or tuples in view and base table

Comment on VIEW table :


A VIEW in MySQL is like a virtual table that shows results from a query without storing the
data itself. It’s useful for simplifying complex queries, improving security by limiting data
access, and providing a clean, consistent interface. You can use views to easily select data, but
inserting, updating, or deleting through a view only works if it’s based on a single table without
any joins or expressions like CONCAT. If the view involves multiple tables or derived columns,
Name of Student : Sachin Santosh Kokare Roll No: 32431

it becomes read-only. In short, views make your queries easier and safer, but they come with
some update limitations.

INDEX
1. Search the f_name from student table and use explain syntax :
CREATE INDEX idx_fname ON Student(F_name);
EXPLAIN SELECT * FROM Student WHERE F_name = 'Aarav';

2. Search the S_ID from student table and use explain syntax :
EXPLAIN SELECT * FROM Student WHERE S_Id = 5;

3. Comment on above (clustered index and non-clustered index)

A clustered index determines the physical order of data in the table. In MySQL, the
primary key by default acts as a clustered index. This means the table is sorted and stored
based on that column. So, when you search using the primary key like s_id, MySQL can
directly go to that row without scanning other rows. It’s very fast because the index and
data are in the same structure. You can only have one clustered index per table because
you can't physically sort a table in more than one way.

4. Create index on f_name :


CREATE INDEX idx_fname ON Student(F_name);

5. Search the f_name from student table and use explain syntax :
EXPLAIN SELECT * FROM Student WHERE F_name = ‘Sneha';

6. Comment on above :
An index in SQL is a tool that helps the database find data faster, just like an index in a
book helps you quickly find a topic. Without an index, the database has to scan every
row
Name of Student : Sachin Santosh Kokare Roll No: 32431

in the table to find what you're looking for, which takes more time, especially when the
table has a lot of data. With an index, the database can jump directly to the needed
information, improving the speed of queries that use conditions like WHERE, JOIN, or
ORDER BY.

There are mainly two types of indexes. The primary or clustered index is automatically
created on the primary key of a table, and it stores the actual data rows in the order of
that key. This makes searching by primary key very fast. On the other hand, a non-
clustered index, also called a secondary index, is created on other columns and holds a
reference to the actual data, which requires one extra step to fetch the full row.

7. Show clustered and non-clustered index

You might also like