A Bit of SQL
A Bit of SQL
A bit of SQL
Using Access
Matthew Rowe
5 January 2012
Contents
1. SQL – A quick introduction............................................................................................................2
1.1. The structure of SQL e.g. DML, DDL and DCL.........................................................................4
1.1.1. DDL................................................................................................................................4
1.1.2. DML................................................................................................................................5
1.1.3. SQL.................................................................................................................................7
1.1.4. DDL (One more example)...............................................................................................7
1.1.5. Constraints.....................................................................................................................8
1.1.6. DCL (Not covered but here’s an example)...................................................................10
1.1.7. DCL 1............................................................................................................................11
1.1.8. DCL 2............................................................................................................................11
1.2. The use of SQL by database administrators, users and application developers...................12
1.3. A series of suitable SQL scripts for the system (including queries using more than one table
and queries using Boolean expressions) with an explanation of their structure.............................13
Page 1 of 14
1. SQL – A quick introduction.
SQL (Structured Query Language or Sequel) is the language used to manipulate databases (DB).
ANSI SQL is the standard format of the language, however, DB manufactures often debase the
standard language to suit their needs.
Therefore when writing SQL using Access, the notation looks slightly different, however the overall
structure remains essentially the same.
We won’t need the default table so right click “Table1” tab and select “Close”.
Page 2 of 14
We are now going to open the SQL window to enable us to write SQL queries to create and
manipulate tables (virtually independently of the Access Front End).
Hot tip: It is worth noting that you can access the SQL view at any point, so if you’ve created a
regular Query using Design View or the Wizard, you can see how the SQL looks by selecting the
query and then choosing View/SQL View.
Note: Access does not write very pretty SQL, it uses Inner Joins and every column is given an alias
which is unnecessary. Please use your skills to unpick what’s necessary and what isn’t when you
look at SQL generated by Access.
Page 3 of 14
1.1. The structure of SQL e.g. DML, DDL and DCL
SQL is the overall term for the language we type to manipulate data in a database, it is subdivided in
to DML (data manipulation language), DDL (data definition language) and DCL (data control
language).
Action: Try these examples – IN ORDER! You’ll need to run each query and save it with the
filename given.
1.1.1. DDL
Note: Run this query, then look at your tables, you should now see a new table called EMP.
Critical: You can’t run this twice! The table already exists, doing so will result in an error.
Page 4 of 14
Oops! Forgot to add a field.
Create it in the same way you did before and save it as DDL_ALTER
Note: This adds two columns, one is a BIT datatype, it only stores 0 or -1, otherwise known as
TRUE/FALSE or Boolean, the second column it adds is the DEPTNO, which we could use as a foreign
key. Look at your EMP table now.
1.1.2. DML
Note: This adds a record to the EMP table. John Smith, is employee number 1, he has NO
pension and works in DeptNo 12.
Page 5 of 14
Note: This adds a record to the EMP table. Manjit Kwarteng, is employee number 2, she has GOT
A pension and works in DeptNo 3.
Look at your queries, if they are done right you will see a different icon, next to these two with a
PLUS sign. Look in your EMP table, can you see your two new records.
UPDATE EMP
SET FNAME = "Jimmy"
WHERE EMPNO=2;
Page 6 of 14
1.1.3. SQL
Add 10 records (manually) into your EMP table, make sure that you have different first names.
Note: This sorts your FNAME column into alphabetical order and shows you the BOTTOM 3
records, remove the DESC to see the TOP 3!
There are lots of features like this, simply use ACCESS help to find features.
Critical: Do NOT RUN THIS QUERY, if you do you will have to re-run you previous queries
and re-insert your data!!!
Page 7 of 14
1.1.5. Constraints
You can constrain a database table in many ways. A constraint is a restriction, your parents probably
constrain you in certain ways:
Call me at 10:00
Don’t talk to strangers
You can’t leave the table until you’ve cleared your plate.
Etc…
In the same way we can add constraints to a database table, we can ensure that no value over 200
goes into a number field or, more commonly, we can use constraints to ENFORCE REFERENTIAL
INTEGRITY.
Here we will build a new table “PayCheques” with a forced link to the EMP table.
This is a slightly different constraint firstly it forces NOT NULL (so every record must have related
EMPNO). Then we link it to the relevant table using “References”.
Page 8 of 14
Let’s make it harder
We know that we want to link our EMP table to another table which will show what Department the
employees work in (we created the Foreign Key).
Note: This constraint is added to the EMP table as the relationship goes in this direction (1
employee works in 1 department).
Once it works, revisit the relationships window and see if all the relationships are where you’d
expect them to be.
Page 9 of 14
1.1.6. DCL (Not covered but here’s an example)
You have created a single user database – that means that only a single user (you) have access to it.
Every user has a database account. Users can be given different levels of access to tables.
1. Completely denied
2. Read only
3. Read and Write
4. Read, Write, Alter
Page 10 of 14
The code used, is as follows.
1.1.7. DCL 1
This allows (GRANTS) all users in the PROCGROUP to SELECT and INSERT
data to the EMP table.
If you want to take privileges away from a group of users you could use this code.
1.1.8. DCL 2
Easy so far?
EMP DEPT
EmpNo (PK) DeptNo (PK)
FName DeptName
LName TelephoneNo
Pension ManagerNo
DeptNo (FK)
Page 11 of 14
The following query is WRONG!
Select EmpNo, FName, DeptName
From EMP, DEPT
Where FNAME=”John” ;
This will produce 100x10 (1000) records all containing all of the details and substituting the FNAME
field for John. (Something like this…)
This is a Cartesian product! Although you know how your tables are linked, the database doesn’t.
Therefore you MUST tell it.
Notice the table name before the key, this is there because both tables have a field called DEPTNo,
the table name tells SQL which table to select from!!
Now you’ve seen a few queries work, how easy is it to recreate the table EMP table, using your
saved queries?
Easy!
Q: -Why do we use SQL rather than design view?
A: -Because SQL is more powerful and SQL is common in all databases. With SQL you can program in
Oracle, Access, Informix etc… Using Access Design view restricts you to working with Access.
Write briefly about the benefits of SQL scripts for administrators (about 1 page).
Page 12 of 14
1.3. A series of suitable SQL scripts for the system (including
queries using more than one table and queries using
Boolean expressions) with an explanation of their structure.
See if you can create a database consisting of two tables using SQL formed constraints.
The two tables should contain details of your friends and the car they drive.
Consider
What is a good PK for a car table?
Can a friend own more than one car?
Use a Boolean expression to show if they should get a birthday card from you or not.
Remember: You need to use a BOOLEAN expression, so you will need one field in your database
saved as a BIT! Use a table where you don’t have much data, use your imagination when deciding
what to call the field. You could use an SQL ALTER query to add this field.
When you select you can use TRUE to pull out any records stored as -1.
Like this:-
This selects all fields from the EMP table where the EMPLOYEE has a PENSION
(-1).
Good Luck!
Page 13 of 14