Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Dbms assignment programs

Sql programs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Dbms assignment programs

Sql programs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Dbms assignment programs

Unit -1

1)Sql program to display the students table

■ Create the students table


CREATE TABLE students (
Student_id INT PRIMARY KEY,
Name VARCHAR(255),
Age INT,
Grade VARCHAR(255),
Email VARCHAR(255)
);

■ Insert data into the students table


INSERT INTO students (student_id, name, age, grade, email)
VALUES
(1, ‘John Doe’, 20, ‘A’, ‘john.doe@example.com’),
(2, ‘Jane Doe’, 22, ‘B’, ‘jane.doe@example.com’),
(3, ‘Bob Smith’, 21, ‘A’, ‘bob.smith@example.com’);

■ Display the students table


SELECT * FROM students;
```

*Output:*

| student_id | name | age | grade | email |


|------------|------------|-----|-------|------------------------|
|1 | John Doe | 20 | A | john.doe@example.com |
|2 | Jane Doe | 22 | B | jane.doe@example.com |
|3 | Bob Smith | 21 | A | bob.smith@example.com |

2)2)
Create a table and use describe keyword and display the table
Here’s an example of creating a table, using the `DESCRIBE` keyword
to display its structure, and then displaying the table itself:
```
■ Create a table called “employees”
CREATE TABLE employees (
Employee_id INT PRIMARY KEY,
Name VARCHAR(255),
Department VARCHAR(255),
Salary DECIMAL(10, 2)
);

■ Describe the table structure


DESCRIBE employees;

■ Insert data into the table


INSERT INTO employees (employee_id, name, department, salary)
VALUES
(1, ‘John Doe’, ‘Sales’, 50000.00),
(2, ‘Jane Doe’, ‘Marketing’, 60000.00),
(3, ‘Bob Smith’, ‘IT’, 70000.00);

■ Display the table


SELECT * FROM employees;
```

*Output:*

*DESCRIBE employees:*

| Field | Type | Null | Key | Default |


|--------------|--------------|------|-----|---------|
| employee_id | int | NO | PRI | NULL |
| name | varchar(255) | YES | | NULL |
| department | varchar(255) | YES | | NULL |
| salary | decimal(10,2) | YES | | NULL |

*SELECT * FROM employees:*

| employee_id | name | department | salary |


|-------------|------------|------------|----------|
|1 | John Doe | Sales | 50000.00 |
|2 | Jane Doe | Marketing | 60000.00 |
|3 | Bob Smith | IT | 70000.00 |

3) Example program on normalization

*Before Normalization (Unnormalized Form)*

```

CREATE TABLE customer_orders (

Customer_name VARCHAR(255),

Customer_address VARCHAR(255),

Order_id INT,

Order_date DATE,

Product_name VARCHAR(255),

Product_price DECIMAL(10, 2),

Quantity INT

);

INSERT INTO customer_orders (

Customer_name,

Customer_address,

Order_id,

Order_date,

Product_name,

Product_price,

Quantity

VALUES

(‘John Doe’, ‘123 Main St’, 1, ‘2022-01-01’, ‘Laptop’, 1000.00, 1),


(‘John Doe’, ‘123 Main St’, 1, ‘2022-01-01’, ‘Mouse’, 20.00, 2),

(‘Jane Doe’, ‘456 Elm St’, 2, ‘2022-01-15’, ‘Tablet’, 500.00, 1),

(‘Jane Doe’, ‘456 Elm St’, 2, ‘2022-01-15’, ‘Keyboard’, 50.00, 1);

```

*First Normal Form (1NF)*

Split the table into two tables: `customers` and `orders`.

```

CREATE TABLE customers (

Customer_id INT PRIMARY KEY,

Customer_name VARCHAR(255),

Customer_address VARCHAR(255)

);

CREATE TABLE orders (

Order_id INT PRIMARY KEY,

Customer_id INT,

Order_date DATE,

Product_name VARCHAR(255),

Product_price DECIMAL(10, 2),

Quantity INT

);

INSERT INTO customers (customer_id, customer_name, customer_address)

VALUES
(1, ‘John Doe’, ‘123 Main St’),

(2, ‘Jane Doe’, ‘456 Elm St’);

INSERT INTO orders (order_id, customer_id, order_date, product_name,


product_price, quantity)

VALUES

(1, 1, ‘2022-01-01’, ‘Laptop’, 1000.00, 1),

(2, 1, ‘2022-01-01’, ‘Mouse’, 20.00, 2),

(3, 2, ‘2022-01-15’, ‘Tablet’, 500.00, 1),

(4, 2, ‘2022-01-15’, ‘Keyboard’, 50.00, 1);

```

*Second Normal Form (2NF)*

Split the `orders` table into two tables: `orders` and `order_items`.

```

CREATE TABLE customers (

Customer_id INT PRIMARY KEY,

Customer_name VARCHAR(255),

Customer_address VARCHAR(255)

);

CREATE TABLE orders (

Order_id INT PRIMARY KEY,

Customer_id INT,

Order_date DATE
);

CREATE TABLE order_items (

Order_item_id INT PRIMARY KEY,

Order_id INT,

Product_name VARCHAR(255),

Product_price DECIMAL(10, 2),

Quantity INT

);

INSERT INTO customers (customer_id, customer_name, customer_address)

VALUES

(1, ‘John Doe’, ‘123 Main St’),

(2, ‘Jane Doe’, ‘456 Elm St’);

INSERT INTO orders (order_id, customer_id, order_date)

VALUES

(1, 1, ‘2022-01-01’),

(2, 2, ‘2022-01-15’);

INSERT INTO order_items (order_item_id, order_id, product_name,


product_price, quantity)

VALUES

(1, 1, ‘Laptop’, 1000.00, 1),

(2, 1, ‘Mouse’, 20.00, 2),

(3, 2, ‘Tablet’, 500.00, 1),

(4, 2, ‘Keyboard’, 50.00, 1);


```

*Third Normal Form (3NF)*

Split the `order_items` table into two tables: `order_items` and `products`.

```

CREATE TABLE customers (

Customer_id INT PRIMARY KEY,

Customer_name VARCHAR(255),

Customer_address VARCHAR(255)

);

CREATE TABLE orders (

Order_id INT PRIMARY KEY,

Customer_id INT,

Order_date DATE

);

CREATE TABLE products (

Product_id INT PRIMARY KEY,

Product_name VARCHAR(255),

Product_price DECIMAL(10, 2)

);

CREATE TABLE order_items (

Order_item_id INT PRIMARY KEY,


Order_id INT,

Product_id INT,

Quantity INT

);

INSERT INTO customers (customer_id, customer_name, customer_address)

VALUES

(1, ‘John Doe’, ‘123 Main St’),

(2, ‘Jane Doe’, ‘456 Elm St’);

INSERT INTO orders (order_id, customer_id, order_date)

VALUES

(1, 1, ‘

```)

4)Example program on relational calculus.

Create tables

CREATE TABLE Students (

StudentID INT PRIMARY KEY,

Name VARCHAR(255),

Age INT,

Department VARCHAR(255)

);

CREATE TABLE Courses (

CourseID INT PRIMARY KEY,

CourseName VARCHAR(255),

Credits INT
);

CREATE TABLE Enrollments (

StudentID INT,

CourseID INT,

Grade VARCHAR(255),

FOREIGN KEY (StudentID) REFERENCES Students(StudentID),

FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)

);

■ Insert data

INSERT INTO Students (StudentID, Name, Age, Department)

VALUES

(1, ‘John’, 20, ‘CS’),

(2, ‘Jane’, 21, ‘EE’),

(3, ‘Bob’, 22, ‘ME’);

INSERT INTO Courses (CourseID, CourseName, Credits)

VALUES

(101, ‘Database Systems’, 3),

(102, ‘Algorithms’, 4),

(103, ‘Computer Networks’, 3);

INSERT INTO Enrollments (StudentID, CourseID, Grade)

VALUES

(1, 101, ‘A’),

(1, 102, ‘B’),


(2, 101, ‘A’),

(3, 103, ‘B’);

■ Relational calculus queries

■ Query 1: Find all students enrolled in “Database Systems”

SELECT S.*

FROM Students S

JOIN Enrollments E ON S.StudentID = E.StudentID

JOIN Courses C ON E.CourseID = C.CourseID

WHERE C.CourseName = ‘Database Systems’;

■ Query 2: Find all courses with more than 3 credits

SELECT *

FROM Courses

WHERE Credits > 3;

■ Query 3: Find all students enrolled in at least two courses

SELECT S.*

FROM Students S

JOIN Enrollments E1 ON S.StudentID = E1.StudentID

JOIN Enrollments E2 ON S.StudentID = E2.StudentID

WHERE E1.CourseID <> E2.CourseID;

```
These SQL queries achieve the same results as the relational calculus
queries:

*Query 1 Results:*

| StudentID | Name | Age | Department |

|-----------|------|-----|------------|

|1 | John | 20 | CS |

|2 | Jane | 21 | EE |

*Query 2 Results:*

| CourseID | CourseName | Credits |

| 102 | Algorithms | 4 |

*Query 3 Results:*

| StudentID | Name | Age | Department |

|-----------|------|-----|------------|

|1 | John | 20 | CS |

4) Union, intersection, projection and selection program in sql

Create tables

CREATE TABLE Students (

StudentID INT PRIMARY KEY,

Name VARCHAR(255),

Age INT,
Department VARCHAR(255)

);

CREATE TABLE CS_Students (

StudentID INT PRIMARY KEY,

Name VARCHAR(255),

Age INT

);

■ Insert data

INSERT INTO Students (StudentID, Name, Age, Department)

VALUES

(1, ‘John’, 20, ‘CS’),

(2, ‘Jane’, 21, ‘EE’),

(3, ‘Bob’, 22, ‘ME’),

(4, ‘Alice’, 20, ‘CS’),

(5, ‘Mike’, 21, ‘EE’);

INSERT INTO CS_Students (StudentID, Name, Age)

VALUES

(1, ‘John’, 20),

(4, ‘Alice’, 20),

(6, ‘Emma’, 22);

■ Union Operation

SELECT Name, Age FROM Students

UNION
SELECT Name, Age FROM CS_Students;

■ Intersection Operation (using INTERSECT)

SELECT Name, Age FROM Students

INTERSECT

SELECT Name, Age FROM CS_Students;

■ Projection Operation

SELECT Name, Age

FROM Students;

■ Selection Operation

SELECT *

FROM Students

WHERE Department = ‘CS’ AND Age > 20;

```

*Output:*

_Union Operation:_

| Name | Age |

| John | 20 |

| Jane | 21 |

| Bob | 22 |

| Alice | 20 |
| Mike | 21 |

| Emma | 22 |

_Intersection Operation:_

| Name | Age |

| John | 20 |

| Alice | 20 |

_Projection Operation:_

| Name | Age |

| John | 20 |

| Jane | 21 |

| Bob | 22 |

| Alice | 20 |

| Mike | 21 |

_Selection Operation:_

| StudentID | Name | Age | Department |

|-----------|------|-----|------------|

|3 | Bob | 22 | ME |
*Note:* The INTERSECT operation may not work in all SQL databases (e.g.,
MySQL). Instead, use:

```

SELECT Name, Age

FROM Students

WHERE (Name, Age) IN (

SELECT Name, Age

FROM CS_Students

Unit -2

Create a table customers in sql and use different datatypes display


the table

-- Create table

1) CREATE TABLE customers (


2) customer_id INT PRIMARY KEY,
3) name VARCHAR(255) NOT NULL,
4) email VARCHAR(255) UNIQUE,
5) phone CHAR(10),
6) address TEXT,
7) city VARCHAR(50),
8) state CHAR(2),
9) zip_code CHAR(5),
10) country VARCHAR(50),
11) date_of_birth DATE,
12) last_purchase DATE,
13) total_purchases DECIMAL(10, 2),
14) is_active BOOLEAN DEFAULT TRUE,
15) created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
16) );
17)
18) -- Insert data
19) INSERT INTO customers (
20) customer_id,
21) name,
22) email,
23) phonne, address,

city,

24) state,
25) zip_code,
26) country,
27) date_of_birth,
28) last_purchase,
29) total_purchases,
30) is_active
31) )
32) VALUES
33) (1, 'John Doe', 'john.doe@example.com', '1234567890', '123
Main St', 'New York', 'NY', '10001', 'USA', '1990-01-01', '2022-01-01',
100.00, TRUE),
34) (2, 'Jane Doe', 'jane.doe@example.com', '9876543210', '456
Elm St', 'Los Angeles', 'CA', '90001', 'USA', '1995-06-01', '2022-06-01',
200.00, TRUE),
35) (3, 'Bob Smith', 'bob.smith@example.com', '5551234567', '789
Oak St', 'Chicago', 'IL', '60601', 'USA', '1980-03-01', '2022-03-01',
50.00, FALSE);
36)
37) -- Display table
38) SELECT * FROM customers;
39) ```
40)
41) _Output:_
42)
43) | customer_id | name | email | phone | address
| city | state | zip_code | country | date_of_birth | last_purchase |
total_purchases | is_active | created_at |
44) |-------------|------------|------------------------|-------------|-------------|----------
-----|-------|----------|---------|---------------|---------------|------------------|-----------|--
-------------------|
45) |1 | John Doe | john.doe@example.com | 1234567890 |
123 Main St | New York | NY | 10001 | USA | 1990-01-01 |
2022-01-01 | 100.00 | TRUE | 2024-03-01 12:00:00 |
46) |2 | Jane Doe | jane.doe@example.com | 9876543210 |
456 Elm St | Los Angeles | CA | 90001 | USA | 1995-06-01 |
2022-06-01 | 200.00 | TRUE | 2024-03-01 12:00:00 |
47) |3 | Bob Smith | bob.smith@example.com | 5551234567
| 789 Oak St | Chicago | IL | 60601 | USA | 1980-03-01 |
2022-03-01 | 50.00 | FALSE | 2024-03-01 12:00:00 |

You might also like