0% found this document useful (0 votes)
13 views9 pages

Points To Remember - DB

Points to remember

Uploaded by

kvfwteachers
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)
13 views9 pages

Points To Remember - DB

Points to remember

Uploaded by

kvfwteachers
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/ 9

DATABASE IS A COLLECTION OFRELATED INFORMATION THAT IS ORGANIZED IN

SUCH A WAY THAT SUPPORTS FOR EASY ACCESS,MODIFY, AND MAINTAIN DATA .

A DATABASE MANAGEMENT SYSTEM(DBMS) OR DATABASE SYSTEM IS A SOFTWARE


THAT CAN BE USED TO CREATE AND MANAGE DATABASES.,IT LETS USERS TO
CREATE A DATABASE,STORE, MANAGE, UPDATE/MODIFY AND RETRIEVE DATA FROM
THAT DATABASE BY USERS OR APPLICATION PROGRAMS.
SOME EXAMPLES OF DBMS INCLUDEMYSQL, ORACLE, POSTGRESQL, SQLSERVER,
MICROSOFT ACCESS, MONGODB.
RELATIONAL DATA MODEL,DATABASE IS REPRESENTED AS COLLECTION OF
RELATED TABLES. EACH TABLE IS TERMED AS RELATION AND HAS ITS UNIQUE NAME
IN THE RELATIONAL DATA MODEL. TABLES ARE FORMED BY USING ROWS AND
COLUMNS.
A ROW (HORIZONTAL SUBSET) OF A TABLE REPRESENTS A TUPLE OR RECORD.
A COLUMN(VERTICAL SUBSET) OF A TABLE REPRESENTS AN ATTRIBUTE.
Degree: No. of Columns or attributes of A table or a relation.
Cardinality: No. of rows or tuples of a table / or a relation.

FEATURES OF A GOOD DBMS/NEED OF DBMS


a. PROVIDES EASE OF ACCESS TO DATA
b. REDUCES DATA REDUNDANCY: REDUNDANCY MEANS SAME DATA ARE
DUPLICATED INDIFFERENT PLACES.
c. REDUCES DATA INCONSISTENCY: DATA INCONSISTENCY OCCURS WHEN SAME
DATA MAINTAINED IN DIFFERENT PLACES DO NOT MATCH.
d. FACILITATES CONTROLLED DATA SHARING: A DATABASE SYSTEM HIDES
CERTAIN DETAILS ABOUT HOW DATA ARE ACTUALLY STORED AND
MAINTAINED. THUS, IT PROVIDES USERS WITH AN ABSTRACT VIEW OF THE
DATA.

DDL COMMANDS (Commands which definesor alters table /database structure-related to


columns )
1. CREATE: CREATE TABLE , CREATE DATABASE
2. ALTER:
● ALTER TABLE TABLENAME ADD COLUMN
● ALTER TABLE TABLENAME MODIFY COLUMNNAME DATATYPE(SIZE)
CONSTRAINT
● ALTER TABLE TABLENAME CHANGE OLDCOLNAME NEWCOLNAME
DATATYPE(SIZE) CONSTRAINT
● ALTER TABLE TABLENANE DROP COLUMNNAME
● ALTER TABLE TABLENAME ADD CONSTRAINT AliasName
VONSTRAINTNAME(COLUMNNAME) .
● ALTER TABLE TABLE NAME DROP CONSTRAINT CONSTRAINTNAME aliasName

3. DROP:
● DROP TABLE TABLENAME
● DROP DATABASE DATABASENAME

Case Study 1:

A University database contains information about its courses and students. The following table
needs to be created:

CourseID (INT) - Unique ID for each course.

CourseName (VARCHAR) - Name of the course.

CourseDuration (INT) - Duration of the course in months.

StartDate (DATE) - The date when the course starts.

Fee (DECIMAL) - The fee for the course.

Questions and Answers:

1. Question: Write the SQL query to create the Courses table. The CourseID should be set as a
primary key, and the CourseName should have a unique constraint.
Answer:

CREATE TABLE Courses (


CourseID INT PRIMARY KEY,
CourseName VARCHAR(100) UNIQUE,
CourseDuration INT,
StartDate DATE,
Fee DECIMAL(8, 2)
);

2. Question: Modify the table to ensure that the Fee column cannot have a NULL value.

Answer:

ALTER TABLE Courses


MODIFY Fee DECIMAL(8, 2) NOT NULL;

3. Question: The University realized that some courses may have the same name but different
durations. Remove the unique constraint on the CourseName column.

Answer:

ALTER TABLE Courses


DROP INDEX CourseName;

4. Question: The CourseDuration column should be changed to store the duration in years, so
the data type needs to be updated to DECIMAL(3, 1) (e.g., 1.5 for 1 year and 6 months). Write
the query for this modification.

Answer:

ALTER TABLE Courses


MODIFY CourseDuration DECIMAL(3, 1);

5. Question: After a policy update, the University wants to rename the Fee column to
TuitionFee. Write the query to rename this column.

Answer:
ALTER TABLE Courses
CHANGE Fee TuitionFee DECIMAL(8, 2);

---

Case Study 2:

A company stores employee details in the Employees table. The table has the following
structure:

EmpID (INT) - Unique identifier for each employee.

FirstName (VARCHAR) - First name of the employee.

LastName (VARCHAR) - Last name of the employee.

Age (INT) - Age of the employee.

Salary (DECIMAL) - Employee's salary.

HireDate (DATE) - Date of hiring.

Questions and Answers:

1. Question: Create the Employees table, ensuring that no two employees can have the same
FirstName and LastName combination, and the Salary should be greater than or equal to
10000.

Answer:

CREATE TABLE Employees (


EmpID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Salary DECIMAL(10, 2) CHECK (Salary >= 10000),
HireDate DATE,
UNIQUE (FirstName, LastName)
);
2. Question: The company needs to add a new column DepartmentID (INT) with a foreign key
reference to a table named Departments (with DepartmentID as the primary key). Write the
query to add this column along with the foreign key constraint.

Answer:

ALTER TABLE Employees


ADD DepartmentID INT,
ADD CONSTRAINT fk_department
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID);

3. Question: Write the query to drop the foreign key constraint on the DepartmentID column.

Answer:

ALTER TABLE Employees


DROP FOREIGN KEY fk_department;

4. Question: The company needs to ensure that all future salaries are entered with a value of at
least 15000. Modify the Salary column to reflect this change.

Answer:

ALTER TABLE Employees


MODIFY Salary DECIMAL(10, 2) CHECK (Salary >= 15000);

5. Question: Write a query to remove the unique constraint from the combination of FirstName
and LastName in the Employees table.

Answer:

ALTER TABLE Employees


DROP INDEX FirstName_LastName;

---

Case Study 3:
A music streaming company maintains a Songs table with the following structure:

SongID (INT) - Unique identifier for each song.

SongName (VARCHAR) - The name of the song.

Artist (VARCHAR) - The name of the artist.

Album (VARCHAR) - The name of the album.

ReleaseYear (YEAR) - The year the song was released.

Questions and Answers:

1. Question: Write the query to create the Songs table and ensure that the combination of
SongName and Artist is unique, while also ensuring that the ReleaseYear is not before 1900.

Answer:

CREATE TABLE Songs (


SongID INT PRIMARY KEY,
SongName VARCHAR(100),
Artist VARCHAR(50),
Album VARCHAR(50),
ReleaseYear YEAR CHECK (ReleaseYear >= 1900),
UNIQUE (SongName, Artist)
);

2. Question: Modify the table to add a new column Genre (VARCHAR) that can only contain one
of the following values: 'Pop', 'Rock', 'Jazz', 'Classical', or 'Hip-Hop'.

Answer:

ALTER TABLE Songs


ADD Genre VARCHAR(20) CHECK (Genre IN ('Pop', 'Rock', 'Jazz', 'Classical', 'Hip-Hop'));

3. Question: The music company decides that they will no longer track the Album of the songs.
Write the SQL query to remove the Album column.

Answer:
ALTER TABLE Songs
DROP COLUMN Album;

4. Question: The ReleaseYear needs to be updated to VARCHAR(4) to accommodate some


songs that are labeled with approximate release years (like '1990s'). Modify the column
accordingly.

Answer:

ALTER TABLE Songs


MODIFY ReleaseYear VARCHAR(4);

5. Question: The company wants to delete all rows from the Songs table where the
ReleaseYear is before 2000. However, the table structure should remain intact. Write the query
to do this.

Answer:

DELETE FROM Songs


WHERE ReleaseYear < 2000;

---

Case Study 4:

An e-commerce company needs to manage product details in a Products table. The structure of
the table is as follows:

ProductID (INT) - Unique identifier for each product.

ProductName (VARCHAR) - The name of the product.

Price (DECIMAL) - The price of the product.

StockQuantity (INT) - The number of units available in stock.

Questions and Answers:


1. Question: Create the Products table with the above structure, ensuring that ProductID is the
primary key, and the Price should not be negative.

Answer:

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2) CHECK (Price >= 0),
StockQuantity INT
);

2. Question: Add a new column Category (VARCHAR) to the Products table and make sure that
the Category column can only store values: 'Electronics', 'Clothing', 'Furniture'.

Answer:

ALTER TABLE Products


ADD Category VARCHAR(30) CHECK (Category IN ('Electronics', 'Clothing', 'Furniture'));

3. Question: Rename the StockQuantity column to AvailableStock in the Products table.

Answer:

ALTER TABLE Products


CHANGE StockQuantity AvailableStock INT;

4. Question: The company wants to ensure that for any product in the Products table, if the
Price is greater than 50000, then the AvailableStock must be at least 10. Write the query to
implement this constraint.

Answer:

ALTER TABLE Products


ADD CONSTRAINT chk_price_stock
CHECK (Price <= 50000 OR AvailableStock >= 10);

5. Question: Drop the Products table completely from the database.


Answer:

DROP TABLE Products;

You might also like