Databases SQL statements
Databases SQL statements
There are 2 statements used to delete from databases – DELETE and DROP.
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:
b) add a new attribute, a new column to hold data values, using syntax ALTER TABLE… ADD…
c) To insert new record in an existing database table, using syntax INSERT INTO… VALUES…,
the syntax here is:
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:
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:
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”
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
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