Assignment 1 SQL
Assignment 1 SQL
ASSIGNMENT - 1
STRUCTURED QUERY LANGUAGE (SQL)
This is the first assignment on Structured Query Language (SQL). In this assignment, you will learn
the introduction of SQL, MySQL relational database management system and basic Data Definition
Language (DDL) and Data Manipulation Language (DML) commands.
Time Required
2 hours.
Please note that the above number is only indicative and should be regarded as an average amount
of time for all students. You may require significantly more or less time depending on personal
circumstances, e.g. knowledge and understanding to the relevant subject topics, experience of the
software used, and level of programming skills. Please do prepare to use more time outside the
officially arranged sessions.
Report Requirement
Complete all the tasks, and report your code and results for Tasks 1.3-1.7 and 1.9-1.12.
Note that you should copy and paste the code and results in the text format as much as possible.
Screenshots as the output results are only acceptable for the exceptional cases.
Structured Query Language (SQL)
SQL (sometimes pronounced sequel for historical reasons) is an abbreviation for Structured Query
Language. It is a comprehensive language for controlling and interacting with a relational database
management system. SQL commands are used for organizing, managing and retrieving data stored
in a relational database. There are very few SQL commands, which make the language very easy to
learn. SQL is called a fourth generation language (4GL) due to its power, conciseness, English like,
free format and low level procedures.
The standard set of SQL commands falls into four categories:
Data Definition Language (DDL) commands for creating and altering the structure of a
database.
Data Manipulation Language (DML) commands for adding and modifying data.
Data control commands for controlling access to the database.
Query commands for extracting information from the database.
MySQL
MySQL is a relational database management system (RDBMS) developed and distributed by MySQL
AB: http://www.mysql.com/ It provides the latest information about MySQL software and MySQL
AB. MySQL uses an extension of the standard SQL. During our lab sessions we are going to use
MySQL on the DISC local net.
With SQL you can:
Create new database tables.
Store information in these tables.
Select exactly any information you need from tables.
Make changes in tables, updating or deleting them as required.
Design and print reports.
Combine data from various tables.
phpMyAdmin
phpMyAdmin is an open source web application, written in PHP for managing MySQL databases.
Currently it can create and drop databases, create/drop/alter tables, delete/edit/add fields,
execute any SQL statement, manage keys on fields, manage privileges, export data into various
formats and is available in 50 languages.
SQL Command:
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
1. Click “SQL” in the middle
2. Type your SQL query in the box
CREATE TABLE Departments (
DepartmentID INT NOT NULL PRIMARY KEY ,
Title VARCHAR( 50 ) NOT NULL ,
Description VARCHAR( 255 ),
DateAddedtoPC DATETIME NOT NULL ,
DateModified DATETIME)
3. Click “Go” to execute the SQL query
4. After execute the SQL query and see the structure of created table
Similar to the above example, please create the other tables as outlined in Task 1.2, i.e. Courses and
StudentCourses.
Task 1.7 SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
SQL Command:
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax (notice that some database systems
don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype
Please do NOT delete the tables or database you created unless you wish to create them again.
Create your separate tables or databases when you are practicing these statements.
Task 1.12 load data from tab delimited text file into table
After logging in, navigate to the phpMyAdmin import screen by doing this:
1. Look in the left-hand column and click on the name of the table into which you want to load
the data.
2. Click the Import tab that appears at the top of the page.
3. Change the "Format of imported file" to CSV. (Do not choose "CSV using LOAD DATA"; it will
not work.)
4. You willl then see a screen that allows you to choose the import options:
5. Next to "Location of the text file", choose the file from your hard disk that you want to
import.
6. Change the Fields terminated by option to be a tab instead of the default semicolon.
7. Leave the other options unchanged unless you know what you're doing.
8. Press Go.
You should see something like Inserted rows: 50 (Query took 0.00 sec) at the top of the screen.
Click the Browse tab to review the imported data.
SELECT * Example
The following SQL statement selects all the columns from the "Departments" table:
SELECT * FROM Departments
After executing the above command, the following screen will be show:
SQL Command:
SELECT DISTINCT column_name, column_name
FROM table_name
SELECT DISTINCT Example
The following SQL statement selects only the distinct values from the "DepartmentID" column from
the "Students" table:
SELECT DISTINCT DepartmentID FROM Students
= Equal
SQL Command:
SELECT column_name, column_name
FROM table_name
ORDER BY column_name, column_name ASC|DESC;
ORDER BY Example
The following SQL statement selects all students from the "Students" table, sorted by the
"DepartmentID" column:
SELECT * FROM Students
ORDER BY DepartmentID
SQL Command:
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
or
SQL Aliases
SQL aliases are used to give a database table, or a column in a table, a temporary name. Basically
aliases are created to make column names more readable. Aliases can be useful when:
There are more than one table involved in a query
Functions are used in the query
Column names are big or not very readable
Two or more columns are combined together
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name
Note: INNER JOIN is the same as JOIN.
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
SQL RIGHT JOIN Example
The following SQL statement will return all courses, and any students they have registered:
SELECT a.CourseID, b.Title, a.StudentID
FROM StudentCourses as a
RIGHT OUTER JOIN Courses as b
ON a. CourseID=b. CourseID
ORDER BY a.courseID
Note: The RIGHT JOIN keyword returns all the rows from the right table (Departments), even if
there are no matches in the left table (Students).
SQL FULL OUTER JOIN Keyword
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right
table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
SQL Functions
SQL has many built-in functions for performing calculations on data.
SQL Aggregate Functions
SQL aggregate functions return a single value, calculated from values in a column. Useful aggregate
functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
SQL Scalar functions
SQL scalar functions return a single value, based on the input value. Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed