Assignment 6 1 Solution

You might also like

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

Assignment : 6_1

Q1.Table name student1: (Perform DDL AND DML command)


Columns : Student_id, student_name, student_age, student_std
5 rows data :

Solution:
■ Create the table
CREATE TABLE student1 (
Student_id INT PRIMARY KEY,
student_name VARCHAR(255),
student_age INT,
student_std VARCHAR(10)
);

■ Insert 5 rows of data


INSERT INTO student1 (Student_id, student_name, student_age,
student_std) VALUES
(1, 'John Doe', 18, '12th'),
(2, 'Jane Smith', 17, '11th'),
(3, 'Bob Johnson', 19, '12th'),
(4, 'Alice Brown', 16, '10th'),
(5, 'Eva Davis', 18, '11th');

■ DDL Commands:
1. Alter Table:
ALTER TABLE student1
ADD student_email VARCHAR(255);
2. DROP table:
DROP TABLE student1;

3. Truncate Command:

TRUNCATE TABLE student1;

4. RENAME Command:
ALTER TABLE student1
RENAME TO student_data;

■ DML Commands:
1. Insert Data:
INSERT INTO student1 (Student_id, student_name, student_age,
student_std, student_email) VALUES (6, 'Grace Lee', 17, '11th',
'grace@example.com');
2. Update Data:

UPDATE student1
SET student_age = 20
WHERE Student_id = 3;

3. Delete Data:

DELETE FROM student1


WHERE Student_id = 4;

4. Select Data:
Select * from student1;

___________________________________________________________________
___________________
Q2. Table name customer1:( Perform TCL command)
Columns : c_id, c_name, c_age, c_billamount
5 rows data:
Solution:
■ Create the table
CREATE TABLE customer1 (
c_id INT PRIMARY KEY,
c_name VARCHAR(255),
c_age INT,
c_billamount DECIMAL(10, 2));

■ Insert 5 rows of data


INSERT INTO customer1 (c_id, c_name, c_age, c_billamount)
VALUES
(1, 'Alice Johnson', 28, 500.50),
(2, 'Bob Smith', 35, 1000.75),
(3, 'Charlie Brown', 22, 250.00),
(4, 'David Lee', 30, 750.25),
(5, 'Eva Davis', 40, 1200.90);

■ Perform TCL commands


1. Start a transaction
BEGIN;

2. Update the bill amount for a customer


UPDATE customer1
SET c_billamount = 600.00
WHERE c_id = 1;
3. Set a savepoint within the transaction
SAVEPOINT update_savepoint;

4. Delete a customer record


DELETE FROM customer1
WHERE c_id = 3;

5. Rollback to the savepoint if needed


ROLLBACK TO update_savepoint;

6. Commit the transaction


COMMIT;

___________________________________________________________________
___________________

Q3. Table name student_details:( Perform primary key)


Columns : Student_id, student_name, student_age, student_std
5 rows data :

Solution:
1. Create the table with a primary key
CREATE TABLE student_details (
Student_id INT PRIMARY KEY,
student_name VARCHAR(255),
student_age INT,
student_std VARCHAR(10)
);
2. Insert 5 rows of data
INSERT INTO student_details (Student_id, student_name,
student_age, student_std)VALUES
(1, 'John Doe', 18, '12th'),
(2, 'Jane Smith', 17, '11th'),
(3, 'Bob Johnson', 19, '12th'),
(4, 'Alice Brown', 16, '10th'),
(5, 'Eva Davis', 18, '11th');

___________________________________________________________________
__________________

Q4. Table name student_info:( Perform foreign key)


Columns : Student_id, student_name, student_age, student_city1
5 rows data:
Table name city_info:
Columns: city_id, student_city
5 rows data:

Solution:
1. Create the city_info table
CREATE TABLE city_info (
city_id INT PRIMARY KEY,
student_city VARCHAR(255)
);

2. Insert 5 rows of city data


INSERT INTO city_info (city_id, student_city)
VALUES
(1, 'New York'),
(2, 'Los Angeles'),
(3, 'Chicago'),
(4, 'Houston'),
(5, 'San Francisco');

3. Create the student_info table with a foreign key


CREATE TABLE student_info (
Student_id INT PRIMARY KEY,
student_name VARCHAR(255),
student_age INT,
student_city1 INT,
FOREIGN KEY (student_city1) REFERENCES
city_info(city_id)
);

4. Insert 5 rows of student data


INSERT INTO student_info (Student_id, student_name,
student_age, student_city1)
VALUES
(1, 'John Doe', 18, 1), /*-- Student from New York
*/
(2, 'Jane Smith', 17, 2), /*-- Student from Los Angeles
*/
(3, 'Bob Johnson', 19, 3), /*-- Student from Chicago*/
(4, 'Alice Brown', 16, 4), /*-- Student from Houston */
(5, 'Eva Davis', 18, 5); /*-- Student from San
Francisco */

You might also like