Professional Documents
Culture Documents
Our SQL tutorial helps you learn SQL (Structured Query Language) in simple and
easy steps so that you can start your database programming quickly. It covers most
of the important concepts related to SQL for a basic to advanced understanding of
SQL and to get a feel of how SQL works.
SQL is a database computer language designed for the retrieval and management
of data in a relational databases like MySQL, MS Access, SQL Server, Oracle,
Sybase, Informix, Postgres etc. SQL stands for Structured Query Language. SQL
was developed in the 1970s by IBM Computer Scientists.
.
2|Page Information Systems Management Lab File
3. Object-Relational Model:
Description: Combines elements of both the relational and object-
oriented models. It allows the use of objects, classes, and inheritance
within the relational database context. For example –
4. Key-Value Model:
Description: In key-value stores, data is stored as key-value pairs. These
databases are optimized for simple retrieval and storage of data without
the need for a complex schema.
ER Diagram:
ER Model:
1. Integer Types:
2. Decimal Types:
5. Boolean Type:
7. User-Defined Types:
Primary Key:
• A primary key is a unique identifier for a record in a table.
• It ensures that each row in a table is uniquely identified.
• The primary key can be a single column or a combination of columns.
Foreign Key:
• A foreign key is a column or a set of columns in a table that refers to the
primary key of another table.
• It establishes a link between two tables and enforces referential integrity.
Unique Key:
• A unique key constraint ensures that all values in a column (or a set of
columns) are unique.
• Unlike the primary key, it allows for NULL values.
Composite Key:
• A composite key is a key that consists of multiple columns to uniquely
identify a record.
• It is similar to a primary key but involves more than one column.
Candidate Key:
• A candidate key is a set of one or more columns that can uniquely identify a
record.
• From the candidate keys, one is chosen to be the primary key.
1. Create table
Definition: Create a new table in the database.
Example - Car Dealer:
CREATE TABLE Cars (
CarID INT PRIMARY KEY,
Model VARCHAR(50),
Year INT,
Price DECIMAL(10, 2),
Color VARCHAR(20)
);
1. Insert
Definition: Add new records (rows) to a table
UPDATE Cars
SET Price = 26000.00
WHERE Model = 'Toyota Camry';
FROM Student;
FROM Student;
SELECT* from student;
-- Subtraction
SELECT id, numeric_col - 5 AS subtracted_value FROM sample_table;
-- Multiplication
SELECT id, numeric_col * 2 AS multiplied_value FROM sample_table;
-- Division
SELECT id, numeric_col / 2 AS divided_value FROM sample_table;
-- AND
-- OR
-- NOT
-- INTERSECT
SELECT id, string_col FROM sample_table WHERE numeric_col > 15
INTERSECT
SELECT id, string_col FROM sample_table WHERE string_col = 'banana';
-- EXCEPT
SELECT id, string_col FROM sample_table WHERE numeric_col > 15
EXCEPT
SELECT id, string_col FROM sample_table WHERE string_col = 'orange';
-- LIKE
SELECT * FROM sample_table WHERE string_col LIKE 'a%';
-- IS NULL
SELECT * FROM sample_table WHERE date_col IS NULL;
-- Equal to
SELECT * FROM sample_table WHERE numeric_col = 20;
-- Not equal to
SELECT * FROM sample_table WHERE numeric_col <> 20;
-- Greater than
SELECT * FROM sample_table WHERE numeric_col > 15;
-- Less than
SELECT * FROM sample_table WHERE numeric_col < 25;
FROM BOOK b
SELECT CARD_NO
FROM BOOK_LENDING
GROUP BY CARD_NO
FROM BOOK_COPIES b
go
SELECT COUNT(CUSTOMER_ID),GRADE
GROUP BY GRADE;
FROM SALESMAN
WHERE SALESMAN_ID IN (
SELECT SALESMAN_ID
FROM CUSTOMER
GROUP BY SALESMAN_ID
);
FROM CUSTOMER c
UNION
FROM CUSTOMER c
FROM SALESMAN s
SELECT MOV_TITLE
FROM DIRECTOR d
SELECT m.MOV_TITLE
FROM MOVIES m
GROUP BY m.MOV_TITLE
SELECT a.ACT_ID
FROM ACTOR a
AND a.ACT_ID IN (
SELECT b.ACT_ID
FROM ACTOR b
);
FROM MOVIES m
GROUP BY m.MOV_TITLE
ORDER BY m.MOV_TITLE;
UPDATE RATING
SET REV_STARS = 5
FROM RATING r
• For any problem selected, write the ER Diagram, apply ER-mapping rules,
normalize the relations, and follow the application development process.
• Make sure that the application should have five or more tables, at least.
• Indicative areas include health care, education, industry, transport, supply
chain, etc
SOLUTION
Problem Selected: XYZ hospital is a multi specialty hospital that includes a number
of departments, rooms, doctors, nurses, compounders, and other staff working in
the hospital. Patients having different kinds of ailments come to the hospital and
get checkup done from the concerned doctors. If required they are admitted in the
hospital and discharged after treatment.
design and develop a database for the hospital to maintain the records of various
departments, rooms, and doctors in the hospital. It also maintains records of the
regular patients, patients admitted in the hospital, the check up of patients done
by the doctors, the patients that have been operated, and patients discharged from
the hospital.
Table Description: Following are the tables along with constraints used in Hospital
Management database.
10. PAT_OPR: If patient is operated in the hospital, his/her details are stored in this
table. Information stored includes patient number, date of admission, date of
operation, number of the doctor who conducted the operation, number of the
operation theater in which operation was carried out, type of operation, patient’s
condition before and after operation, treatment advice, etc.
Constraint: Patient number should exist in PAT_ENTRY table. Department, doctor
number should exist or should be valid.
11. ROOM_DETAILS: It contains details of all rooms in the hospital. The details
stored in this table include room number, room type (general or private), status
(whether occupied or not), if occupied, then patient number, patient name,
charges per day, etc.
Constraint: Room number should be unique. Room type can only be G or P and
status can only be Y or N
• The attribute "D_NAME" can be removed and replaced with a foreign key
referencing the "D_NAME" attribute in the DEPARTMENT table. This
eliminates redundancy and ensures data integrity.
• The remaining attributes are all dependent on the primary key "DOC_NO" and
are in 2nd Normal Form (2NF).
76 | P a g e Information Systems Management Lab File
4. DOC_ON_CALL:
• Similar to ALL_DOCTORS, this relation is redundant and can be merged with
DOC_REG.
• Additional attributes like "FS_PR_CL" and "PYMT_DU" can be added to
DOC_REG to accommodate on-call specific information.
5. PAT_ENTRY:
• This relation needs further normalization:
7. PAT_ADMIT:
• This relation is in 1NF but can be further normalized:
9. PAT_REG:
• This relation needs further normalization:
11. ROOM_DETAILS:
• This relation is in 1NF and 2NF.
Normalized Tables:
1. DEPARTMENT: (D_NAME, D_LOCATION, FACILITIES)
2. DOC_REG: (DOC_NO, D_NAME, QUALIFICATION, SALARY, EN_TI
ME, EX_TIME, ADDRESS, PH_NO, DOJ, FS_PR_CL, PYMT_DU)
3. PAT_ENTRY: (PAT_NO, PAT_NAME, CHKUP_DT, PT_AGE, SEX, DIA
GNOSIS, ADDRESS, CITY, PH_NO)
4. PAT_CHKUP: (PAT_NO, DOC_NO, DIAGNOSIS, STATUS, TREATME
NT)
5. PAT_ADMIT: (PAT_NO, ADV_PYMT, MODE_PYMT, ROOM_NO, AD
MTD_ON, COND_ON, INVSTGTN_DN, TRMT_SDT, ATTDNT_NM)
6. PAT_DIS: (PAT_NO, TR_ADVS, TR_GVN, PYMT_GV, DIS_ON)
7. PAT_MEDICINES: (PAT_NO, DATE_VIS, MEDICINES) (New table)
8. PAT_OPR: (PAT_NO, DATE_OPR, IN_COND, AFOP_COND, TY_OPER
ATION, DOC_NO, OPTH_NO, OTHER_SUG)
9. ROOM_DETAILS: (ROOM_NO, TYPE, STATUS, RM_DL_CRG, OTHER
_CRG)