Dbms 1-6
Dbms 1-6
_____________________________________________
____
Aim:
A. Introduction to SQL & Types of SQL Statements (DDL, DML, TCL, DCL)
B. Introduction to DBMS, RDBMS.
C. Introduction to various RDBMS software
Theory:
SQL:
SQL stands for Structured Query Language.(Also called “Sequel”)
SQL is a standard language for accessing databases
SQL has been an international standard (ISO) since 1987,
Characteristics of SQL:
o SQL is easy to learn.
o SQL is used to define the data in the database and manipulate it when needed.
Types of SQL:
DDL (Data Definition Language)
DQL (Data Query Language)
DML (Data Manipulation Language)
DCL (Data Control Language)
TCL (Transaction Control Language)
DDL (Data Definition Language) :
DDL or Data Definition Language actually consists of the SQL commands that can be used to
define the database schema. DDL is a set of SQL commands used to create, modify, and
delete database structures but not data.
Command Description
Command Description
Command Description
command Description
In SQL Server, for storing integer or numeric values in a table column the
int
INT data type is used.
DBMS:
Database management system is a software which is used to manage the database. For
example: MySQL, Oracle, etc are a very popular commercial database which is used in
different applications.
DBMS provides an interface to perform various operations like database creation, storing
data in it, updating data, creating a table in the database and a lot more.
It provides protection and security to the database. In the case of multiple users, it also
maintains data consistency.
RDBMS:
RDBMS stands for Relational Database Management System.
All modern database management systems like SQL, MS SQL Server, IBM DB2, ORACLE,
My-SQL, and Microsoft Access are based on RDBMS.
It is called Relational Database Management System (RDBMS) because it is based on the
relational model introduced by E.F. Codd.
DBMS RDBMS
Data elements need to access Multiple data elements can be accessed at the same
individually. time.
It stores data in either a It uses a tabular structure where the headers are the
navigational or hierarchical column names, and the rows contain corresponding
form. values.
RBDMS software:
Some popular RDBMS software options:
1. MySQL: An open-source RDBMS that's widely used for web applications. It's known
for its reliability and performance.
2. PostgreSQL: Another open-source RDBMS, known for its advanced features and
standards compliance. It supports complex queries and large databases.
3. Oracle Database: A commercial RDBMS known for its high performance, scalability,
and robustness. It's widely used in enterprise environments.
4. Microsoft SQL Server: A commercial RDBMS from Microsoft, known for its
integration with other Microsoft products and its strong security features.
Program: 2
___________________________________________________________________________
___
Components:
1. Entities:
Represent real-world objects or
concepts. Visualized as rectangles.
2. Attributes:
Characteristics or properties of entities.
Visualized as ovals connected to their corresponding
entity.
3. Relationships:
Describe how entities interact with each
other. Visualized as diamonds or lines
connecting entities.
4. Cardinality:
• Indicates the number of instances of one entity that can or must be associated
with instances of another entity.
• Types include:
One-to-One (1:1)
One-to-Many (1) Many-to-Many
(M)
5. Primary Keys:
Unique identifiers for each entity instance, usually underlined in the diagram.
6. Foreign Keys: Attributes that create a link between two entities, indicating a
relationship.
7. Weak Entities:
Entities that cannot be uniquely identified by their own attributes alone. They
depend on a "strong" entity for their identification, typically represented with
a double rectangle.
8. Generalization/Specialization:
• Generalization: Combining common characteristics from multiple entities into
a higher-level entity.
• Specialization: Creating sub-entities from a more general entity, adding
specific attributes.
9. Multi-valued Attributes:
Attributes that can hold multiple values for a single entity instance (e.g., a
Student may have multiple. Visualized as double ovals.
DDL or Data Definition Language actually consists of the SQL commands that can be
used to define the database schema. It simply deals with descriptions of the database
schema and is used to create and modify the structure of database objects in the
database
CREATE:
Create database or its objects (table, index, function, views, store procedure, and
triggers)
Syntax: CREATE TABLE table_name (column1 data_type, column2
data_type, ...); Code:
CREATE TABLE STUDENT(
S_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(25) NOT NULL,
SECOND_NAME VARCHAR2(25) NOT NULL,
CITY VARCHAR2(15) NOT NULL,
DOB DATE NULL
);
ALTER:
• TRUNCATE:
Remove all records from a table, including all spaces allocated for the records are
removed.
Syntax: TRUNCATE TABLE table_name;
CODE:
• INSERT:
Insert data into a table in a database.
Syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1,
value2,
...);
Code:
INSERT INTO STUDENTS (STD_ID, F_NAME, L_NAME, UMAR)
VALUES (1, ’ARYAN’, ‘BAKSHI’, 20);
INSERT INTO STUDENTS (STD_ID, F_NAME, L_NAME, UMAR)
VALUES (2, ’ABHISHEK’, ‘GUPTA’, 20);
SELECT* FROM STUDENTS;
• UPDATE:
Update existing data within a table.
Syntax: UPDATE table_name SET column1 = value1, column2 = value2
WHERE condition; Code:
UPDATE STUDENT SET L_NAME= ‘KUMAR’ WHERE STD_ID= 2
SELECT * FROM STUDENT;
_____________________________________________
___
Aim: Implementing data Constraints at Column Level and Table
level: PRIMARY Key, FOREIGN Key, UNIQUE, NOT NULL, CHECK,
DEFAULT value concept.
Theory:
Data Constraints:
• SQL constraints are used to specify rules for data in a table.
• Constraints can be specified when the table is created with
the CREATE
TABLE statement, or after the table is created with the
ALTER TABLE statement.
PRIMARY Key:
The PRIMARY KEY constraint uniquely identifies each record
in a table.
Primary keys must contain UNIQUE values, and cannot
contain NULL values.
A table can have only ONE primary key; and in the table,
this primary key can consist of single or multiple columns
(fields)
NOT NULL –
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept
NULL values.
This enforces a field to always contain a value, which means
that you cannot insert a new record, or update a record
without adding a value to this field
UNIQUE –
The UNIQUE constraint ensures that all values in a column
are different.
Both the UNIQUE and PRIMARY KEY constraints provide a
guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE
constraint.
However, you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
FOREIGN KEY –
The FOREIGN KEY constraint is used to prevent actions that
would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table,
that refers to the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and
the table with the primary key is called the referenced or
parent table.
CHECK –
The CHECK constraint is used to limit the value range that
can be placed in a column.
If you define a CHECK constraint on a column it will allow
only certain values for this column.
If you define a CHECK constraint on a table it can limit the
values in certain columns based on values in other columns
in the row.
DEFAULT –
The DEFAULT constraint is used to set a default value for a
column.
The default value will be added to all new records, if no
other value is specified.
Code:
CREATE TABLE EMPLOYEE (
E_ID INT PRIMARY KEY,
F_NAME VARCHAR(50) NOT NULL,
L_NAME VARCHAR(50) NOT NULL,
AGE INT CHECK (AGE>=18),
SALARY DECIMAL(10,2) DEFAULT 30000,
D_ID INT, FOREIGN KEY (D_ID) REFERENCES DEPARTMENT
(D_ID)
);
CREATE TABLE DEPARTMENT (
);
DESCRIBE EMPLOYEE;
DESCRIBE DEPARTMENT;
Fig 5.1: Applying various constraints on table while creating the
table and description of table employee
• Like
• In
• Between Like Operator:
In SQL, the LIKE operator is used in the WHERE clause to search
for a specified pattern in a column.
CODE:
SELECT * FROM EMPLOYEE
CODE:
SELECT * FROM EMPLOYEE
(value1,value2,..);
CODE:
SELECT * FROM EMPLOYEE
BETWEEN Operator:
The BETWEEN operator selects values within a given range. The
values can be numbers, text, or dates. The BETWEEN operator is
inclusive: begin and end values are included.
Syntax:
SELECT column_name(s) FROM table_name WHERE
column_name BETWEEN value1 AND value2; CODE:
Program-8
__________________________________________________________________________
FROM table_name
WHERE condition;
CODE: SELECT * FROM EMPLOYEE;
CODE:
SELECT SUM(SALARY) AS TOTAL_SALARY FROM EMPLOYEE;
Fig 7.2: Finding total salary of
Employee AVG():
FROM
table_name
WHERE
condition;
CODE:
SELECT AVG(SALARY) FROM EMPLOYEE;
COUNT():
The COUNT() function returns the number of rows that matches a
specified criterion.
SYNTAX:
SELECT COUNT(column_name)
FROM
table_name
WHERE
condition;
CODE:
SELECT COUNT(NAME) AS TOTAL_EMPLOYEE FROM EMPLOYEE;
FROM
table_name
WHERE
condition;
CODE:
SELECT MIN(SALARY) FROM EMPLOYEE;
MAX():
The MAX() function returns the largest value of the selected
column.
SYNTAX:
SELECT MAX(column_name)
FROM
table_name
WHERE
condition;
CODE:
1. NOW():
2. CURDATE():
3. CURTIME():
4. DATE():
1. CAST():
2. CONVERT():
3. TO_CHAR():
4. TO_DATE():
5. CONCAT():
All columns in the SELECT list that are not in group functions must be in
the GROUP BY clause.
Output:
Illegal Queries Using Group Functions: Any column or expression in the
SELECT list that is not an aggregate function must be in the GROUP BY
clause:
SELECT department_id, COUNT(last_name) FROM employees;
Output:
GROUP BY
department_id;
Output:
Restricting Group Results with the HAVING Clause
When we use the HAVING clause, the Oracle server restricts groups as
follows:
1. Rows are grouped.
2. The group function is applied.
3. Groups matching the HAVING clause are displayed.
Syntax:
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Using the HAVING Clause
Experiment -9
___________________________________________________________________________
_____
Output:
Illegal Queries Using Group Functions: Any column or expression in the
SELECT list that is not an aggregate function must be in the GROUP BY
clause:
SELECT department_id, COUNT(last_name) FROM employees;
Output:
GROUP BY
department_id;
Output:
Syntax:
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Using the HAVING Clause
GROUP BY deptno
HAVING
MAX(sal)>2000 ;
Output: