SQL queries. DML (Data Manipulation Language)

11.4.2.4 describe the basic SQL queries for working with one table in a database: SELECT, UPDATE, INSERT, and DELETE

11.4.2.5 use SQL SELECT for data selection in more tables

SQL queries. Data Manipulation Language.

Data Manipulation Language (DML) is a computer language including commands permitting users to manipulate data in a database.

 

The SQL SELECT Statement

The SELECT statement is used to select data from a database.

Syntax Example
SELECT column1, column2, ...
FROM table_name;
SELECT surname, firstname
FROM users;
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT surname, firstname, iin 
FROM users
WHERE userID=5;

Additionally: SQL ORDER BY KeywordSQL AND, OR, and NOT Operators

The SQL INSERT Statement

The INSERT command is used to enter records into a table. Values are entered into the table corresponding order to the listed columns.

Syntax Example
INSERT INTO tablename (field1, field2, ...) 
VALUES ('value1', 'value2', ...);
INSERT INTO users (surname, firstname,
address, age)
VALUES ('Aliyev', 'Ruslan', 'Konaev dist. 7-15', 15);
INSERT INTO tablename 
VALUES ('value1', 'value2', ...);
INSERT INTO users 
VALUES (userID, 'Aliyev', 'Ruslan', 'Konaev dist. 7-15', 15);

SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

Syntax Example
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE users
SET surname = "Makeev", firstname = "Maksim"
WHERE userID = 67;

SQL DELETE Statement

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

Syntax Example
DELETE FROM table_name WHERE condition; DELETE FROM users WHERE iin = "950210350464";

 

SELECT from several tables

If you need to select data from two or more related tables, then for each field you need to specify the table name and add the equality of fields related by a foreign key to the selection conditions.
 

Example,
ERD of relational database:

Task. Write an SQL statement to select all appointments to room 204 to get the next output:

Decision:

SELECT Dentist.dentist_surname, Patient.patient_surname, Patient.patient_firstname,  Appointments.date 
FROM Dentist, Patient, Appointments
WHERE Appointments.room = 204 AND Dentist.dentistID = Appointments.dentistID AND Patient.patientID = Appointments.patientID

Video 2. SQL queries. Add records into table

Video 3. SQL queries: SELECT, UPDATE, DELETE

All videos

 


Questions:


Exercises:

Ex. 1  (Author: Litvinova Olga - CS teacher of NIS Pavlodar)

Ex. 2

Test SELECT (Ex. 1-3) on w3schools.com

Test WHERE (Ex. 1-5) on w3schools.com

Test INSERT on w3schools.com

Test UPDATE (Ex. 1-3) on w3schools.com

Test DELETE (Ex. 1-2) on w3schools.com

Ex. 3

SQLZOO - TASK 1 "SELECT basics

SQLZOO - TASK 2 "SELECT from WORLD"

SQLZOO - TASK 3 "SELECT from Nobel"

Ex. 4

SELECT QUIZ 1

BBC QUIZ 2

Nobel QUIZ 3

Ex. 5

Ex. 6

Ex. 7

https://www.101computing.net/sql/sql-1.html

Ex. 8 (Author: Ermekova Ainagul - CS teacher of NIS Pavlodar)

Ex. 9 SQL Investigation: The Stolen Gemstones (Link)


Exam questions:


 

Категория: Relational databases | Добавил: bzfar77 (10.11.2020)
Просмотров: 6783 | Теги: Select, update, DML, database, query, INSERT, Delete | Рейтинг: 5.0/4
Всего комментариев: 0
avatar