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

Laboratory work №3

Completed by Zhamilya Kozhagulova

Task 1
DDL (Data Definition Language) is a subset of SQL (Structured Query Language)
used to define the structure of a database and its objects such as tables, views,
indexes, and procedures. DDL Statements are used to create, modify, and delete
database objects, including tables, views, indexes, and stored procedures. Some of
the most common DDL operators include:

1. CREATE: This statement creates a new database object such as a table,


view, or index. For example, the following SQL statement creates the table
"customers":
CREATE TABLE customers (id INT PRIMARY KEY, name
VARCHAR(255), address VARCHAR(255));

2. ALTER: This statement is used to modify an existing database object. For


example, the following SQL statement adds a new column "email" to the
table "customers":
ALTER TABLE customers ADD email VARCHAR(255);

3. DROP: This statement is used to delete an existing database object. For


example, the following SQL statement deletes the table "customers":
DROP TABLE customers;

4. TRUNCATE: This statement is used to delete all rows in a table, but unlike
the DROP statement, it preserves the table structure and indexes.

5. RENAME: This statement is used to rename an existing database object. For


example, the following SQL statement renames the table "customers" to
"clients":
RENAME TABLE customers TO clients;
DDL statements are executed immediately and are persistent, meaning that once an
object is created, modified, or deleted, the changes cannot be undone.
DML (Data Manipulation Language) is a subset of SQL which is used to
manipulate data in a database. DML Operators are used to insert, update and delete
data in a database. Some of the most common DML operators include:
1. SELECT: This statement is used to retrieve data from one or more tables in a
database. For example, the following SQL statement retrieves all records
from the "customers" table:
SELECT * FROM customers;

2. INSERT: This statement is used to insert new data into a table. As an


example, the following SQL statement inserts a new row into the
"customers" table:
INSERT INTO customers (id, name, address) VALUES (1, 'John Smith,'
'123 Main St');

3. UPDATE: This statement is used to modify existing data in a table. For


example, the following SQL statement updates the address of the customer
with ID 1 in the table "customers":
UPDATE customers SET address = '456 Park Ave' WHERE id = 1;

4. DELETE: This statement is used to delete data from a table. For example,
the following SQL statement deletes the customer with id 1 from the table
"customers":
DELETE FROM customers WHERE id = 1;
DML Statements are executed immediately and can be undone with a rollback
statement.

Task 2

CREATE TABLE products (


id VARCHAR,
name VARCHAR,
description TEXT,
price DOUBLE PRECISION
);

CREATE TABLE order_items (


order_code INTEGER,
product_id VARCHAR,
quantity INTEGER
);

CREATE TABLE orders (


code INTEGER,
customer_id INTEGER,
total_sum DOUBLE PRECISION,
is_paid BOOLEAN
);

CREATE TABLE customers (


id INTEGER,
full_name VARCHAR(50),
timestamp TIMESTAMP,
delivery_address TEXT
);

SELECT product_id
FROM order_items
JOIN products ON order_items.product_id = products.id;

SELECT order_code
FROM order_items
JOIN orders ON order_items.order_code = orders.code;

SELECT customer_id
FROM orders
JOIN customers ON orders.customer_id = customers.id;

Task 3

CREATE TABLE students (


student_id INT PRIMARY KEY,
full_name VARCHAR(255),
speaking_languages VARCHAR(255),
age INT,
birth_date DATE,
gender VARCHAR(10),
average_grade DECIMAL(4,2),
about_yourself TEXT,
need_for_dormitory BOOLEAN,
additional_info TEXT
);

CREATE TABLE instructors (


instructor_id INT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
speaking_languages VARCHAR(255),
work_experience INT,
remote_lessons_allowed BOOLEAN
);

CREATE TABLE lesson_participants (


lesson_id INT PRIMARY KEY,
lesson_title VARCHAR(100) NOT NULL,
instructor_id INT,
FOREIGN KEY (instructor_id) REFERENCES instructors(instructor_id),
room_number INT,
UNIQUE(lesson_title, instructor_id, room_number)
);

Task 4

INSERT INTO products (id, name, description, price)


VALUES
('1', 'Stray Kids Maxident album', 'sealed', 11000),
('2', 'Stray Kids 5-STAR album B version', 'sealed', 11000),
('3', 'Stray Kids 5-STAR album C version', 'sealed', 11000),
('4', 'Bangchan Levanter card', 'no defect', 2500),
('5', 'Seungmin 5-STAR', 'no defect', 2900),
('6', 'Changbin ROCK-STAR', 'no defect', 2000);

UPDATE products
SET name = 'Stray Kids 5-STAR album A version'
WHERE name = 'Stray Kids Maxident album';

UPDATE products
SET price = 2500
WHERE price = 2900;

DELETE FROM products WHERE name = 'Stray Kids 5-STAR album C version';
DELETE FROM products WHERE name = 'Bangchan Levanter card';

You might also like