Program 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Program 1

AIM: Write queries to execute following DDL command.


1. Create
2. Alter
3. Drop

1. Create - The CREATE TABLE statement is used to create a new table in a database.
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Output:

2. Alter - The ALTER TABLE command adds, deletes, or modifies columns in a table.

2.1. Alter to change a size of particular - ALTER TABLE syntax to modify the
column data type and size.
Syntax:
ALTER TABLE table_name
MODIFY column_name datatype;
Output:

2.2. Add a new column to the existing table -The following ALTER TABLE ADD
statement appends a new column to a table.
Syntax:
ALTER TABLE table_name
ADD column_name;
Output:
2.3. Remove a column from the table - The DROP COLUMN command is used to
delete a column in an existing table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
Output:

2.4. Rename a column from the table - We can change the name of a column by
using the ALTER command along with the RENAME COLUMN command.
Syntax:
ALTER TABLE table_name
RENAME COLUMN column_name;
Output:

3. Drop – Destroy the table along with its data - The SQL DROP TABLE statement is used
to remove a table definition and all the data.
Syntax:
DROP TABLE table_name;
Output:
Program 2
AIM: Write queries to execute following command.
1. Insert Into
2. Update
3. Delete

1. Insert Into - The INSERT INTO statement is used to insert new records in a table.
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Output:

2. Update - The UPDATE statement is used to modify the existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Output:

3. Delete - The DELETE statement is used to delete existing records in a table.


Syntax:
DELETE FROM table_name WHERE condition;
Output:
Program 3
AIM: - Write quires to execute following aggregate function.

1. Sum Function
2. Average Function (AVG)
3. Count Function
4. Minimum Function (MIN)
5. Maximum Function (MAX)

1. Sum Function - The SUM() function returns the total sum of a numeric column.
Syntax: SELECT SUM(column_name)
FROM table_name
WHERE condition;
Output:

2. AVG Function - The AVG() function returns the average value of a numeric column.
Syntax: SELECT AVG(column_name)
FROM table_name
WHERE condition;
Output:

3. Count Function - The COUNT() function returns the number of rows that matches a
specified criterion.
Syntax: SELECT AVG(column_name)
FROM table_name
WHERE condition;
Output:
4. MIN Function - The MIN() function returns the smallest value of the selected column.
Syntax: SELECT MIN(column_name)
FROM table_name
WHERE condition;
Output:

5. MAX Function - The MAX() function returns the largest value of the selected
column.
Syntax: SELECT MAX(column_name)
FROM table_name
WHERE condition;
Output:
Program 4
AIM- Write quires to execute following DML Command
 To Retrieve entire contents of the table.
 Retrieve selective from the table
 Retrieve contents from a table based on various operators: Logical, Condition,
Boolean, String.
 Sort the data in ascending and descending order in a table on the basis of one
column or more than one column.

1. To Retrieve entire contents of the table - In SQL, to retrieve data stored in our tables,
we use the SELECT statement.
Syntax
SELECT * FROM table_name;
Output:

2. Retrieve selective from the table - In SQL, to retrieve data stored in our tables, we use
the SELECT statement.
Syntax
SELECT column1, column2, ...
FROM table_name;
Output:
3. Retrieve contents from a table based on various operators: Logical, Condition,
Boolean, String.

3.1. Logical Operator - SQL logical operators are used to test for the truth of the
condition.
Syntax
select * from table_name where logical_operator);
Output:

3.2. Condition Operator - Conditional Expressions in SQL are used to evaluate


conditions based on the input values.
Syntax
select * from table_name where condition);
Output:

3.3. Boolean Operator - A boolean is a data type that can store either a True or
False value.
Syntax
CREATE TABLE table_name (
Column_name VARCHAR2(10),
is_checked NUMBER(1)
);
Output:
3.4. String Operator – String functions are used to perform an operation on input
string and return an output string.
Syntax
SELECT * FROM table_name WHERE column_name LIKE '%Harsh%';
Output:

4. Sort the data in ascending and descending order in a table on the basis of one column
or more than one column.
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Output:
Program 5
AIM-Perform and explain following Integrity constraints with proper syntax and
example:
 Primary Key
 Not NULL
 Unique
 Check
 Referential key

1. Primary Key - The PRIMARY KEY constraint uniquely identifies each record in a
table.
Syntax
CREATE TABLE Persons (,
column2 datatype,
column3 datatype,
....
Age int,
PRIMARY KEY (ID)
);
Output:

2. Not Null - The NOT NULL constraint enforces a column to NOT accept NULL
values.
Syntax
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Output:
3. Unique - The UNIQUE constraint ensures that all values in a column are different.
Syntax
CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Output:

4. Check - The CHECK constraint is used to limit the value range that can be placed in a
column.
Syntax
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
Output:
5. Referential key - The Referential key constraint is used to prevent actions that would
destroy links between tables.
Syntax
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Output:
Program 6
AIM - Draw an ER diagram from given entities and their attribute.
Program 7
AIM – Convert ER Model into relational model with proper constrain.

You might also like