0% found this document useful (0 votes)
6 views18 pages

Week 6

Uploaded by

setoseto1907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

Week 6

Uploaded by

setoseto1907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Database Management Systems

SQL III
DML-Data Manipulation Language

Pınar Yıldırım
The SQL INSERT INTO Statement
• The INSERT INTO statement is used to insert new records in a table.
• INSERT INTO Syntax
– the INSERT INTO statement can be written in two ways.
– The first way specifies both the column names and the values to be
inserted:
– INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
The SQL INSERT INTO Statement
• In order to add values for all the columns of the table, not need to specify
the column names in the SQL query. But, the order of the values should
be in the same order as the columns in the table. The INSERT INTO syntax
would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
The SQL INSERT INTO Statement

CustomerID CustomerName City

391 Ali İzmit

564 Mehmet İstanbul

112 Pelin Ankara

346 Can Antalya


INSERT INTO Example
• The following SQL statement inserts a new record in the "Customers"
table:
• Example
– INSERT INTO Customers (CustomerID,
CustomerName,City)
VALUES (‘98', ‘Hakan', ‘Ankara');

CustomerID CustomerName City


391 Ali İzmit
564 Mehmet İstanbul
112 Pelin Ankara
346 Can Antalya
98 Hakan Ankara
Insert Data Only in Specified Columns
• It is also possible to only insert data in specific columns.
• Example
INSERT INTO Customers (CustomerName, City)
VALUES (‘Bahar', ‘London');

CustomerID Customer City


Name
391 Ali İzmit

564 Mehmet İstanbul

112 Pelin Ankara

346 Can Antalya


NULL Bahar London
The SQL UPDATE Statement
• The UPDATE statement is used to modify the existing records in a table.

• UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
The SQL UPDATE Statement

• Not: The WHERE clause specifies which record(s) that should be updated.
If the WHERE clause is omitted, all records in the table will be updated!
UPDATE Example
• UPDATE Customers
SET CustomerName = ‘Deniz', City= ‘İzmir'
WHERE CustomerID = 391;

CustomerID CustomerName City

391 Deniz İzmir

564 Mehmet İstanbul

112 Pelin Ankara

346 Can Antalya

NULL Bahar London


• UPDATE Customers
SET CustomerName = ‘Deniz', City= ‘İzmir'
WHERE CustomerID = 391 OR CustomerID=112;

CustomerID CustomerName City

391 Deniz İzmir

564 Mehmet İstanbul

112 Deniz İzmir

346 Can Antalya

NULL Bahar London


• UPDATE Customers
SET City= ‘Ankara'
WHERE CustomerID = 564 AND City=‘İstanbul’;
UPDATE Multiple Records
• It is the WHERE clause that determines how many records will be updated.
• Example
UPDATE Customers
SET City=‘Ankara'
WHERE CustomerID >391;

CustomerID CustomerName City

391 Deniz İzmir

564 Mehmet Ankara

112 Deniz İzmir

346 Can Antalya

NULL Bahar London


Update Warning!
• Be careful when updating records. If the WHERE clause is omitted, ALL
records will be updated!
• Example
UPDATE Customers
SET CustomerName=‘Sevgi';

CustomerID CustomerName City

391 Sevgi İzmir

564 Sevgi Ankara

112 Sevgi İzmir

346 Sevgi Antalya

NULL Sevgi London


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

• DELETE Syntax
DELETE FROM table_name WHERE condition;
SQL DELETE Statement

CustomerID CustomerName City

391 Sevgi İzmir

564 Sevgi Ankara

112 Sevgi İzmir

346 Sevgi Antalya

NULL Sevgi London


SQL DELETE Example
• The following SQL statement deletes the customer " Can"
from the "Customers" table:

DELETE FROM Customers


WHERE City=‘Antalya';

CustomerID CustomerName City

391 Sevgi İzmir

564 Sevgi Ankara

112 Sevgi İzmir

346 Sevgi Antalya


NULL Sevgi London
Delete All Records
• It is possible to delete all rows in a table without deleting the table.
DELETE FROM table_name;

• The following SQL statement deletes all rows in the "Customers" table,
without deleting the table:
• Example
DELETE FROM Customers;
References
• Modern Database Management 11th Edition, Jeffrey A. Hoffer, V. Ramesh,
Heikki Topi © 2013 Pearson Education, Inc. Publishing as Prentice Hall
• https://www.w3schools.com

You might also like