0% found this document useful (0 votes)
6 views

Databases SQL statements

The document outlines SQL statements for managing databases, focusing on DELETE and DROP commands for removing data and structures. It details how to create tables, add columns, and insert records, along with modifying table structures and data values. Additionally, it provides exercises for practicing SQL statements related to user and flight tables.

Uploaded by

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

Databases SQL statements

The document outlines SQL statements for managing databases, focusing on DELETE and DROP commands for removing data and structures. It details how to create tables, add columns, and insert records, along with modifying table structures and data values. Additionally, it provides exercises for practicing SQL statements related to user and flight tables.

Uploaded by

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

Databases SQL Statements

There are 2 statements used to delete from databases – DELETE and DROP.

DROP applies to database structure, predominantly in 2 cases:

a) we need to delete entire table –


DROP TABLE table_name
b) we need to delete a column or columns in a table –
ALTER TABLE table_name
DROP COLUMN column_name;

or:
ALTER TABLE TableName
DROP Column1,
DROP Column2;
for multiple columns
DELETE applies to the data in a table, thus it is used to delete data:

a) we need to delete all data from the table, but keep the table structure –
DELETE FROM table_name;
b) we need to delete a row(s) of record(s) -
DELETE FROM table_name WHERE condition;

2. Using SQL to add to databases consists of 2 types –first type is used to create a structure – either
a whole table, or adding columns to existing tables, second type is used to add actual data records
into new rows:

a) adding a whole new table – using syntax CREATE TABLE – to set up a whole table for and
entity with its attributes used as column heading:

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);
For example:

CREATE TABLE tbl_Employee

EmpID, INTEGER NOT NULL, PRIMARY KEY

EmpName VARCHAR (20), NOT NULL

EmpSurname VARCHAR (20), NOT NULL

DepID VARCHAR (10), NOT NULL


)

b) add a new attribute, a new column to hold data values, using syntax ALTER TABLE… ADD…

the syntax is:

ALTER TABLE table_name


ADD column_name datatype;
For example:

ALTER TABLE tbl_Employee

ADD Salary CURRENCY

c) To insert new record in an existing database table, using syntax INSERT INTO… VALUES…,
the syntax here is:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);
For example:

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)


VALUES ('Cardinal', 'Tom B. Erichsen', '21, Peartree Road', 'Hatfield', 'AL108AB', 'UK');

3. Using SQL to change either structure of data values also consists of 2 types – first type is to modify
the structural features of a table, for example changing the data type of a column, second type is to
modify the values of records in rows, this would make changes to the data in a cell.

a) to modify the column, using syntax ALTER TABLE… MODIFY COLUMN…, the syntax is:

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;
For example, to allow a longer surname in employees table:

ALTER TABLE tbl_employees

MODIFY COLUMN EmpSurname VARCHAR (30), NOT NULL

b) to modify data itself, i.e. a specific field of a record, an UPDATE TABLE…SET COLUMN…
WHERE…, this is needed to locate the exact cell in the table that contains the required
change, or if all records need to be changed in a specific column, the exact column that
contains values that need to be changed:

i) to modify a specific cell the syntax is

UPDATE table_name
SET column1 = value1,
WHERE condition;
For example:

UPDATE Customers
SET ContactName = 'Alfred Schmidt',
WHERE CustomerID = 1;
ii) it is possible to modify a few specific cells for the same record, just by adding
more columns and values:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
For example:

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
iii) to modify all records and change values in a specific column, the syntax is:

UPDATE table_name
SET column1 = value1*1.2
WHERE condition;
For example:

UPDATE tbl_Employee
SET Salary=Salary*1.2
WHERE Department = “Technical”

Exercises for tbl_users and tbl_flights (tables on next page):

1/ write an SQL statement to remove items as follows:

a) delete both tables: tbl_users and tbl_flights


b) delete column locked from tbl_users
c) delete column Destination name from tlb_flights
d) delete columns Destination Code and Destination Name from tbl_flights
e) delete a record of username Hera from tbl_users
f) delete records of Zeus and Poseidon from tbl_users
g) delete a record for Flight ID 1355 from tbl_flights
h) delete top 3 records fro tbl_flights.

2/ write an SQL statement to add items as follows:

a) add a new table to the database where tbl_flights is kept, the new table should be called
tbl_Destinations and hold the attributes Destination code set as primary key and Destination
name set as secondary key.
b) create a new database with 2 tables: students and courses, the table for students should
hold the following attributes: students ID, set as primary key, datatype string, student name
and surname, date of birth and table courses should hold the following attributes: course ID,
set as primary key, course title, course start date and finish date.
c) add a new column to table students with year group, type integer
d) create a table vehicles with the following attributes: vehicle ID set as integer and primary
key, vehicle name, category, price,
e) add a new column to the table vehicles with supplier name set as string and maximum 50
characters
f) add a record for a new student in table students with the following values: student ID is
AJ12356, student surname Marney, student first name Alex, DOB 24 June 2009
g) add a record for a new for a new student with student ID AK12365, student surname
Alexand, student first name not know, DOB 24 January 2009
h) add a new record to table users with user ID 4, username Haos, passwordHash
9jslew9sfjsm5SLF25xmvjsl, locked set as zero
i) add a new record to the table flights with the following attributes: flight ID 2225, flight
number OC8789, Destination Code JFK, Departure Date 04/07/18 and departure time 08:40

3/ write an SQL statement to achieve the following modifications:

a) the student with ID AD12365 has changed surname to Smith and his first name can be
added as Mike
b) user account of Zeus in tbl_users has been locked
c) departure time of flight OC7750 has changed to 9:30, update the record
d) flight 1331 is no longer going to Halifax, but to Heathrow, change the record to show this
change
e) the tbl_users table needs to have an additional column to hold a 6 digit user PIN for
additional security, add this column to the table structure with correct data type and
field length
f) modify the data type for the column tbl_users from integer to alphanumeric, limit field
length to 6

tbl_users

tbl_flights

You might also like