Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Database Management

System (DBMS)
ASSIGNMENT-2:

(1) What is data and information?


In the context of a database management system (DBMS), "data" and
"information" are related but distinct concepts.
1. Data: Data refers to the raw facts and figures that are collected, stored, and
processed by a computer. In the context of a DBMS, data typically
represents the values or attributes of entities within the system. For
example, in a database for a company, data might include employee
names, salaries, and job titles. Data in its raw form lacks context and
meaning.
2. Information: Information, on the other hand, is the result of processing,
organizing, and analyzing data to extract meaning. It's data that has been
processed or interpreted in some way to make it meaningful or useful to
users. In a DBMS, information might be derived from querying the database
to generate repoThe commands In summary, data is the raw material that
is stored in a database, while information is the meaningful output derived
from processing and analyzing that data. The primary goal of a DBMS is to
efficiently manage data and facilitate the generation of useful information
from that data.

(2) Write down DDL and DML commands. (list)


Data Definition Language (DDL) and Data Manipulation Language (DML)
commands in a database management system:
DDL (Data Definition Language) Commands:
CREATE TABLE: Creates a new table in the database.
1. ALTER TABLE: Modifies the structure of an existing table (e.g., adding,
modifying, or dropping columns).
2. DROP TABLE: Deletes a table and all its data from the database.
3. CREATE INDEX: Creates an index on a table to improve query performance.
4. DROP INDEX: Deletes an index from a table.
5. CREATE DATABASE: Creates a new database.
6. DROP DATABASE: Deletes an existing database.
7. TRUNCATE TABLE: Deletes all rows from a table, but retains the table
structure.

DML (Data Manipulation Language) Commands:


1. SELECT: Retrieves data from one or more tables.
2. INSERT INTO: Adds new rows of data into a table.
3. UPDATE: Modifies existing data in a table.
4. DELETE FROM: Removes one or more rows of data from a table.
5. MERGE: Performs an "upsert" operation, which can insert new rows or
update existing rows based on a specified condition.
6. CALL: Executes a database procedure or function.
7. EXPLAIN PLAN: Provides information on how the database executes a SQL
statement.
8. LOCK TABLE: Controls concurrency by locking a table so that other users
cannot modify it simultaneously.
These commands are fundamental to managing the structure and content of a
database and are essential for performing various operations such as creating,
modifying, querying, and deleting data.
(3) Explain the datatypes in detail.
SQL Datatype:
o SQL Datatype is used to define the values that a column can
contain.
o Every column is required to have a name and data type
database table.
Datatype of SQL:

1. Binary Datatypes:
There are Three types of binary Datatypes which are given
below:
Data Type Description

Binary: It has a maximum length of 8000 bytes. It


contains fixed-length binary data.
Var binary: It has a maximum length of 8000 bytes. It
contains variable-length binary data.
Image: It has a maximum length of 2,147,483,647
bytes. It contains variable-length binary
data.
2. Approximate Numeric Datatype :
The subtypes are given below:
Data type From To Description

Float: -1.79E + 308 1.79E + 308 It is used to specify


floating-point value
e.g. 6.2, 2.9 etc.
real: -3.40e + 38 3.40E + 38 It specifies a
single precision
floating point
number.
3. Exact Numeric Datatype
The subtypes are given below:

Data type Description

Int: It is used to specify an integer value.


Smallint: It is used to specify small integer value.
Bit: It has the number of bits to store.
Decimal: It specifies a numeric value that can have a decimal number.
Numeric: It is used to specify a numeric value.

4. Character String Datatype


The subtypes are given below:

Data type Description

Char: It has a maximum length of 8000 characters.


It contains Fixed-length non-unicode
characters.
Varchar: It has a maximum length of 8000 characters.
It contains variable-length non-unicode
characters.

Text: It has a maximum length of 2,147,483,647


characters. It contains variable-length non- Unicode
characters.

5. Date and time Datatypes


The subtypes are given below:

Data type Description

Date: It is used to store the year, month, and


days value.
Time: It is used to store the hour, minute, and
second values.
Timestamp: It stores the year, month, day, hour, minute,
and the second value.

(4) Explain create command with syntax and


examples.
SQL CREATE TABLE statement is used to create table in a
database.
If you want to create a table, you should name the table
and define its column and each column's data type.

Let's see the simple syntax to create the table.

1. create table "tablename"


2. ("column1" "data type",
3. "column2" "data type",
4. "column3" "data type",
5. ...
6. "columnN" "data type");

The data type of the columns may vary from one database to
another. For example, NUMBER is supported in Oracle
database for integer value whereas INT is supported in MySQL.

Let us take an example to create a STUDENTS table with ID as


primary key and NOT NULL are the constraint showing that
these fields cannot be NULL while creating records in the table.

1. SQL> CREATE TABLE STUDENTS (


2. ID INT NOT NULL,
3. NAME VARCHAR (20) NOT NULL,
4. AGE INT NOT NULL,
5. ADDRESS CHAR (25),
6. PRIMARY KEY (ID)
7. );
You can verify it, if you have created the table successfully by
looking at the message displayed by the SQL Server, else you
can use DESC command as follows:
SQL> DESC STUDENTS;

(5) Explain Alter commands with its syntax and


example.
SQL (Modifying structure of table) ALTER TABLE

The ALTER TABLE statement in Structured Query


Language allows you to add, modify, and delete columns
of an existing table. This statement also allows database
users to add and remove various SQL constraints on the
existing tables.

Any user can also change the name of the table using this
statement.

ALTER TABLE ADD Column statement in SQL

In many situations, you may require to add the columns


in the existing table. Instead of creating a whole table or
database again you can easily add single and multiple
columns using the ADD keyword.

Syntax of ALTER TABLE ADD Column statement in


SQL

1. ALTER TABLE table_name ADD column_name column-


definition;

The above syntax only allows you to add a single


column to the existing table. If you want to add more
than one column to the table in a single SQL
statement, then use the following syntax:

1. ALTER TABLE table_name


2. ADD (column_Name1 column-definition,
3. column_Name2 column-definition,
4. .....
5. column_NameN column-definition);
Let's take an example of a table named Employee:

Table: Employee

o Suppose you want to add two columns,


Emp_ContactNo. and Emp_EmailID, in the above
Employee table. For this, you have to type the
following query in the SQL:

ALTER TABLE Employee ADD ( Emp_ContactNo.


Number(13), Emp_EmailID varchar(50 ) ;

This statement will add Emp_ContactNo. and


Emp_EmailID columns to the Employee table.

Syntax of ALTER TABLE MODIFY Column


statement in SQL

1. ALTER TABLE table_name MODIFY column_name


column-definition;

This syntax only allows you to modify a single


column of the existing table. If you want to modify
more than one column of the table in a single SQL
statement, then use the following syntax:

1. ALTER TABLE table_name


2. MODIFY (column_Name1 column-definition,
3. column_Name2 column-definition,
4. .....
5. column_NameN column-definition);

Examples of ALTER TABLE MODIFY Column


statement in SQL
Here, we have taken the following two different
SQL examples, which will help you how to modify
single and multiple columns of the existing table
using ALTER TABLE statement:

Table: Employee
o Suppose, you want to modify the datatypes of
two columns Emp_ContactNo. and Emp_EmailID of
the above Employee table. For this, you have to
type the following query in the SQL:
ALTER TABLE Employee ADD ( Emp_ContactNo. Int,
Emp_EmailID varchar(80) ;

ALTER TABLE DROP Column statement in


SQL
In many situations, you may require to delete the
columns from the existing table. Instead of deleting
the whole table or database you can use DROP
keyword for deleting the columns.
Syntax of ALTER TABLE DROP Column
statement in SQL
ALTER TABLE table_name DROP Column
column_name ;

Examples of ALTER TABLE DROP Column


statement in SQL
Here, we have taken the following two different
SQL examples, which will help you how to delete a
column from the existing table using ALTER TABLE
statement:
Table: Employee
o Suppose, you want to delete the Emp_Salary and
Emp_City column from the above Employee table.
For this, you have to type the following two
different queries in the SQL:
1. ALTER TABLE Cars DROP COLUMN Emp_Salary ;
2. ALTER TABLE Cars DROP COLUMN Emp_City ;
Syntax of ALTER TABLE RENAME Column
statement in SQL
1. ALTER TABLE table_name RENAME COLUMN
old_name to new_name;

Examples of ALTER TABLE RENAME Column


statement in SQL
Here, we have taken the following two different
SQL examples, which will help you how to
change the name of a column of the existing
table using ALTER TABLE statement:

Table: Employee

o Suppose you want to change the name of the


Emp_City column of the above Employee table.
For this, you have to type the following query in
the SQL:

1. ALTER TABLE Employee RENAME COLUMN


Emp_City to Emp_Address;

(6) Explain drop and truncate commands with its


syntax and example.

SQL TRUNCATE TABLE

A truncate SQL statement is used to remove all rows


(complete data) from a table. It is similar to the DELETE
statement with no WHERE clause.

Let's see the syntax to truncate the table from the


database.

1. TRUNCATE TABLE table_name;


For example, you can write following command to
truncate the data of employee table

1. TRUNCATE TABLE Employee;

DROP COLUMN from a table

The DROP COLUMN command is used to delete a column in


an existing table.

The following SQL deletes the "ContactName" column from


the "Customers" table:
Example
ALTER TABLE Customers
DROP COLUMN ContactName;

(7) Explain insert commands with its syntax and


example. (simple and use with &)

SQL INSERT STATEMENT

SQL INSERT statement is a SQL query. It is used to insert a single


or multiple records in a table.
There are two ways to insert data in a table:

1. By SQL insert into statement

1. By specifying column names


2.Without specifying column names
2. By SQL insert into select statement
1) Inserting data directly into a table

You can insert a row in the table by using SQL INSERT INTO command.
There are two ways to insert values in a table.
In the first method there is no need to specify the column name where the data
will be inserted, you need only their values.
1. INSERT INTO table_name
2. VALUES (value1, value2, value3....);
The second method specifies both the column name and values which you want
to insert.
1. INSERT INTO table_name (column1, column2, column3....)
2. VALUES (value1, value2, value3.....);

Let's take an example of table which has five records within it.

1. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)


2. VALUES (1, ABHIRAM, 22, ALLAHABAD);
3. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
4. VALUES (2, ALKA, 20, GHAZIABAD);
5. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
6. VALUES (3, DISHA, 21, VARANASI);
7. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
8. VALUES (4, ESHA, 21, DELHI);
9. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
10. VALUES (5, MANMEET, 23, JALANDHAR);

You can create a record in CUSTOMERS table by using this syntax also.

1. INSERT INTO CUSTOMERS


2. VALUES (6, PRATIK, 24, KANPUR);

2) Inserting data through SELECT Statement

SQL INSERT INTO SELECT Syntax

1. INSERT INTO table_name


2. [(column1, column2, .... column)]
3. SELECT column1, column2, .... Column N
4. FROM table_name [WHERE condition];

(8) Explain update and delete commands with its


syntax and example.
SQL DELETE
The SQL DELETE statement is used to delete rows from a table. Generally
DELETE statement removes one or more records from a table.

SQL DELETE Syntax

1. DELETE FROM table_name [WHERE condition];


SQL DELETE EXAMPLE
Let us take a table, named "EMPLOYEE" table.
1. DELETE FROM EMPLOYEE WHERE ID=101;

SQL UPDATE
The SQL commands (UPDATE and DELETE) are used to modify the data
that is already in the database. The SQL DELETE command uses a WHERE
clause.

SQL UPDATE statement is used to change the data of the records held by
tables. Which rows is to be update, it is decided by a condition. To
specify condition, we use WHERE clause.

The UPDATE statement can be written in following form:

1. UPDATE table_name SET [column_name1= value1,... column_nameN


= valueN] [ WHERE condition]

Let's see the Syntax:

1. UPDATE table_name
2. SET column_name = expression
3. WHERE conditions

Let's take an example: here we are going to update an entry in the source
table.

SQL statement:

1. UPDATE students
2. SET User_Name = 'beinghuman'
3. WHERE Student_Id = '3'
Updating Multiple Fields:

If you are going to update multiple fields, you should separate each field
assignment with a comma.
SQL UPDATE statement for multiple fields:
1. UPDATE students
2. SET User_Name = 'beserious', First_Name = 'Johnny'
3. WHERE Student_Id = '3'

(9) What is different between drop, truncate, and


delete commands.
The commands DROP, TRUNCATE, and DELETE are all used in SQL (Structured
Query Language) to remove data from a database, but they have different
functionalities and implications:
1. DROP:
• The DROP command is used to delete an entire table or a database
object like a table, view, index, etc.
• When you use DROP, you're essentially removing the entire structure
and data associated with the object being dropped.
• It's a DDL (Data Definition Language) command and can't be rolled
back. Once you drop a table or object, it's gone permanently.
2. TRUNCATE:
• The TRUNCATE command is used to remove all rows from a table.
• Unlike DELETE, TRUNCATE does not log individual row deletions,
making it faster and less resource-intensive, especially for large
tables.
• It's also a DDL command, but it's less harmful than DROP since it only
removes data, not the structure of the table.
• TRUNCATE cannot be rolled back, and it typically resets any
associated auto-increment values.
3. DELETE:
• The DELETE command is used to remove one or more rows from a
table based on specified criteria.
• Unlike TRUNCATE, DELETE is a DML (Data Manipulation Language)
command. It's slower than TRUNCATE, especially for large datasets,
because it logs individual row deletions and can trigger triggers or
other related operations.
• DELETE can be rolled back if used within a transaction, which means
you can undo the deletion if required.
• It's often used when you want to selectively remove specific rows
from a table while retaining the table structure.

(10) Explain select query with its syntax and


example (with its possible way)

SQL view Statement

The SELECT statement is the most commonly used command in


Structured Query Language. It is used to access the records from one
or more database tables and views. It also retrieves the selected data
that follow the conditions we want.
By using this command, we can also access the particular record
from the particular column of the table. The table which stores the
record returned by the SELECT statement is called a result-set table.
Syntax of SELECT Statement in SQL
1. SELECT Column_Name_1, Column_Name_2, .....,
Column_Name_N FROM Table_N ame;

If you want to access all rows from all fields of the table, use the
following SQL
SELECT syntax with * asterisk sign:
1. SELECT * FROM table_name;

Examples of SELECT Statement in SQL


Use the following query to create the Student_Records table in SQL:

1. CREATE TABLE Student_Records


2. (
3. Student_Id Int PRIMARY KEY,
4. First_Name VARCHAR (20),
5. Address VARCHAR (20),
6. Age Int NOT NULL,
7. Percentage Int NOT NULL,
8. Grade VARCHAR (10)
9. ) ;
The following query inserts the record of intelligent students into the
Student_Records table:

1. INSERT INTO Student VALUES (201, Akash, Delhi, 18, 89, A2),
2. (202, Bhavesh, Kanpur, 19, 93, A1),
3. (203, Yash, Delhi, 20, 89, A2),
4. (204, Bhavna, Delhi, 19, 78, B1),
5. (05, Yatin, Lucknow, 20, 75, B1),
6. (206, Ishika, Ghaziabad, 19, 51, C1),
7. (207, Vivek, Goa, 20, 62, B2);

The following SQL query displays all the values of each column from the above
Student_records table:
1. SELECT * FROM Student_Records;

(11) Explain where conditions with =,<,>, AND, OR, NOT,


IN, NOT IN (use select query).

1. = (Equal): This condition is used to retrieve rows where a column has a


specific value.

SELECT * FROM table_name WHERE column_name = value;

2. < (Less Than): This condition is used to retrieve rows where a column's
value is less than a specified value.

SELECT * FROM table_name WHERE column_name < value;


3. > (Greater Than): This condition is used to retrieve rows where a column's
value is greater than a specified value.

SELECT * FROM table_name WHERE column_name > value;

4. AND: This condition is used to combine multiple conditions, and it retrieves


rows where both conditions are true.

SELECT * FROM table_name WHERE condition1 AND condition2;

5. OR: This condition is used to retrieve rows where at least one of the
conditions is true.

SELECT * FROM table_name WHERE condition1 OR condition2;

6. NOT: This condition is used to retrieve rows where a condition is not true.

SELECT * FROM table_name WHERE NOT condition;

7. IN: This condition is used to check if a value matches any value in a list.

SELECT * FROM table_name WHERE column_name IN (value1, value2, ...);

8. NOT IN: This condition is used to check if a value does not match any value
in a list.

SELECT * FROM table_name WHERE column_name NOT IN (value1, value2,


...);

(12) Define Data constraints. Write down overview data


constraint types.
Data constraints, also known as database constraints, are rules
that enforce the integrity, accuracy, and consistency of data
stored within a database. These constraints are defined on tables
to maintain data quality and ensure that only valid data is stored.
Data constraints help in preventing invalid data entry or
modifications that could lead to data corruption or
inconsistencies. Here's an overview of common data constraint
types:

Primary Key Constraint:


Ensures that each row in a table is uniquely identified.
Primary key values cannot be null and must be unique within the
table.
Typically implemented using a single column or a combination of
columns.

Foreign Key Constraint:


Enforces referential integrity between two tables.
Ensures that values in a column (or a set of columns) of one table
match values in a column (or set of columns) in another table.
Helps maintain relationships between tables and prevents
orphaned records.

Unique Constraint:
Ensures that all values in a column (or a combination of columns)
are unique.
Unlike primary keys, unique constraints allow null values, but if a
column has a unique constraint, only one row can have a null
value in that column.

Check Constraint:
Specifies a condition that each row must satisfy.
Used to enforce domain integrity, ensuring that values in a
column meet certain criteria.
The condition can be based on comparisons, arithmetic
operations, or logical expressions.

Not Null Constraint:


Specifies that a column cannot contain null values.
Ensures that every row must have a value for the specified
column.

Default Constraint:
Specifies a default value for a column.
If no value is provided for the column during insertion, the default
value is used.

(13) What is the Primary key? Explain with features and


examples.

PRIMARY KEY –

Primary Key is a field which uniquely identifies each row in the


table. If a field in a table as primary key, then the field will not be
able to contain NULL values as well as all the rows should have
unique values for this field. So, in other words we can say that this
is combination of NOT NULL and UNIQUE constraints.

A table can have only one field as primary key. Below query will
create a table named Student and specifies the field ID as primary
key.

CREATE TABLE Student


(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20),
PRIMARY KEY(ID)
);

(14) What is unique? Explain with features and examples.

UNIQUE –

This constraint helps to uniquely identify each row in the table.


i.e. for a particular column, all the rows should have unique
values.

Here are the key features of the unique key constraint:

Uniqueness: The unique key constraint ensures that each value in


the specified column (or combination of columns) is unique within
the table. No two rows can have the same value(s) for the
column(s) defined as unique.

Nullable Columns: Unlike primary keys, unique constraints allow


nullable columns. This means that a unique key column can
contain null values. However, if a column has a unique constraint,
only one row can have a null value in that column.

Enforced at the Column Level: Unique key constraints are applied


at the column level, specifying that the values within the specified
column(s) must be unique.

Multiple Columns: Unique key constraints can be applied to a


single column or a combination of columns. When applied to
multiple columns, the combination of values across those columns
must be unique.
For example, the below query creates a table Student where the
field ID is specified as UNIQUE. i.e, no two students can have the
same ID. Unique constraint in detail.

CREATE TABLE Student


(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);

(15) Write down differences between primary key and


unique constraints.
Primary Key and Unique Key constraints are both used to enforce
uniqueness in database columns, but they have some key
differences:

Purpose:

Primary Key: The primary key uniquely identifies each record in a


table. It must be unique and cannot contain null values. There can
only be one primary key in a table.
Unique Key: The unique key constraint ensures that all values in a
column or combination of columns are unique within the table.
Unlike primary keys, unique key columns can contain null values.

Null Values:

Primary Key: Does not allow null values. Every row must have a
value for the primary key column.
Unique Key: Allows null values, but if a column has a unique
constraint, only one row can have a null value in that column.

Number of Constraints:

Primary Key: Each table can have only one primary key constraint.
Unique Key: A table can have multiple unique key constraints,
either on a single column or a combination of columns.

Indexing:

Primary Key: Primary key constraints are automatically indexed in


most database systems, which can improve query performance.
Unique Key: Unique key constraints may or may not be
automatically indexed, depending on the database system.

Constraint Naming:

Primary Key: Often named as "PK_tableName".


Unique Key: Can have custom names defined by the user.

(16) Explain check constraint with proper example.

CHECK –
Using the CHECK constraint we can specify a condition for a field,
which should be satisfied at the time of entering values for this
field. For example, the below query creates a table Student and
specifies the condition for the field AGE as (AGE >= 18 ). That is,
the user will not be allowed to enter any record in the table with
AGE < 18. Check constraint in detail

CREATE TABLE Student


(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);

(17) Explain not null and default constraint with example.

1. NOT NULL –

If we specify a field in a table to be NOT NULL. Then the field will


never accept null value. That is, you will be not allowed to insert a
new row in the table without specifying any value to this field.

For example, the below query creates a table Student with the
fields ID and NAME as NOT NULL. That is, we are bound to specify
values for these two fields every time we wish to insert a new
row.

CREATE TABLE Student


(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
ADDRESS varchar(20)
);

2. DEFAULT –

This constraint is used to provide a default value for the fields.


That is, if at the time of entering new records in the table if the
user does not specify any value for these fields then the default
value will be assigned to them.

For example, the below query will create a table named Student
and specify the default value for the field AGE as 18.
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);

(18) How to assign the constraint with constraint key word?

In SQL, you can assign constraints using the CONSTRAINT keyword


when creating or altering a table. Here's how you can assign
constraints with the CONSTRAINT keyword:

(1) Primary Key Constraint:

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
CONSTRAINT pk_constraint_name PRIMARY KEY (column1)
);

(2) Unique Constraint:

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
CONSTRAINT unique_constraint_name UNIQUE (column1)
);

(3) Foreign Key Constraint:

CREATE TABLE table_name1 (


column1 datatype PRIMARY KEY,
column2 datatype,
);

CREATE TABLE table_name2 (


column1 datatype,
column2 datatype,
CONSTRAINT fk_constraint_name FOREIGN KEY (column1)
REFERENCES table_name1(column1)
);

(4) Check Constraint:

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
CONSTRAINT check_constraint_name CHECK (column1 > 0)
);

(5) Not Null Constraint:

CREATE TABLE table_name (


column1 datatype NOT NULL,
column2 datatype
);

(6) Default Constraint:

CREATE TABLE table_name (


column1 datatype DEFAULT default_value,
column2 datatype
);
(19) What is group by clause? Explain with syntax and
example.

The GROUP BY clause in SQL is used to group rows that have the
same values into summary rows, typically to apply aggregate
functions such as COUNT, SUM, AVG, MAX, or MIN to each
group. It's commonly used in conjunction with the SELECT
statement to generate summary reports from the database.

Here's the syntax for the GROUP BY clause:

SELECT column1, aggregate_function(column2)


FROM table_name
WHERE condition
GROUP BY column1;

column1: The column(s) by which you want to group the result


set.
aggregate function: The function used to perform calculations on
the grouped data, such as COUNT, SUM, AVG, MAX, or MIN.
table name: The name of the table from which you're selecting
data.
condition: Any conditions that you want to apply to filter the
rows.

Example to illustrate how the GROUP BY clause works:

CREATE TABLE orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
city VARCHAR(100),
total_amount DECIMAL(10, 2)
);

You might also like