SQL Practical Guide - 1: Mohamed Ghasia ICT 114 Dec 2012/13
SQL Practical Guide - 1: Mohamed Ghasia ICT 114 Dec 2012/13
Mohamed Ghasia
ICT 114
Dec 2012/13
Objectives
• MU12&*dict2008
• WAMP
• -WINDOWS
• -APACHE
• -MYSQL
• -PHP
Outcomes
• Students should be able to do the following:-
– Create database
– Create database table
– Insert data into a database table
– Update data into a database table
– Update database table
– Retrieve data from table (s)
– Join tables [equal join, left joins, right joins]
– Drop database /tables
– Apply group functions
– Use operators [ AND , OR, IN, =, <>,like, >,<, IS NULL, NOT IN, NOT
BETWEEN
– Use aliases
– Show tables/databases
– Describe tables
– Order by and group by
– Sub queries
Creating database
1. Log into MYSQL
2. List the available databases :
MYSQL>show databases;
3. Create database called training:
MYSQL> create database training;
4. List the available databases :
MYSQL>show databases;
5. The database should be created at this stage and
listed together with other databases.
6. To use training database among the list:
MYSQL> use training;
Creating Basic Tables
To create a basic table with no constraints:-
Create table student (
stID char(30) primary key,
Fname char(30),
Surname char(30),
Sex char(6),
Town char(30),
Email char(30),
CellNO char(30)
);
Creating Tables
To create a basic table with constraints:-
Create table student (
stID char(30) primary key,
Fname char(30) not null,
Surname char(30) not null,
Sex char(6) not null,
Town char(30) not null default ‘Mzumbe’,
Email char(30) unique,
CellNO char(30) unique
);
Describing and listing tables
• To show list of tables:
MYSQL>show tables;
To show student table schema:
MYSQL> describe student; or
MYSQL> desc student;
To show content of a table student:
MYSQL> select * from student;
Inserting data into table
• Inserting through headings:-
NOTE:
Arrangement of values should correspond
to the same in the table.
these examples are based on created tables;
match your table well
Altering Tables
• The ALTER TABLE statement is used to add, delete, or
modify columns in an existing table.
• SQL ALTER TABLE Syntax
• To add a column in a table, use the following syntax:
MYSQL>ALTER TABLE table_name ADD column_name
datatype constraints;
• To delete a column in a table:
MYSQL>ALTER TABLE table_name DROP COLUMN
column_name;
• To change the data type or constraint in a table:
MYSQL>ALTER TABLE table_name ALTER COLUMN
column_name datatype constraint;
Altering Tables
Inserting columns, changing data types, dropping
columns, adding constraints
• Adding new column(s) in a student table:
MYSQL> ALTER TABLE STUDENT add (DoB date not null,
Marital char(10));
• Droping column(s) from student table:
MYSQL>ALTER TABLE STUDENT drop DoB,cellno;
• Changing data types or constraints:-
MYSQL>alter table student modify marital
char(20) not null;
Altering Tables
• Inserting foreign key
MYSQL> ALTER TABLE student
ADD FOREIGN KEY (csid)
REFERENCES course(csid);
MYSQL> ALTER TABLE Sudent
ADD CONSTRAINT fk_student
FOREIGN KEY (CSID)
REFERENCES Course(csid);
NOTE: -parent table such as course should exist first
• referenced attribute should be declared within same domain
in both tables. Ie data type and size in both tables should be
same
• If there are data inside child table, error might appear if they
violate integrity rules
Updating table content
• Updating a column with same value to all
records:- example setting student town to kilosa.
MYSQL> update student SET town=“kilosa”;
• Updating specific records: example, set town for
students with ID 1234/t.08,12356/t.08 to songea.
MYSQL> update student SET town=“songea” WHERE stid
IN (“1234/t.08”, “12356/t.08”);
NOTE:
Observe WHERE clause to specify update criteria.
Without it, all records will be updated with new entry
Deleting table content
• Delete everything from student table:
MYSQL> delete from student;
• Delete only students from tanga and
sumbawanga:
MYSQL> delete from student where town=“tanga”
and town=“sumbawanga”;
NOTE:
Observe the use of WHERE to specify what to
delete?
Dropping table(s)/database/views
• To delete a database:-
MYSQL> DROP DATABASE database_name;
• To delete a table:-
MYSQL> DROP TABLE table_name;
• To delete all data inside student table:
MYSQL> TRUNCATE student;
NOTE: these commands are sensitive, use them
with care!! It will not warn u
Retrieving Data from table(s)
• Basic data retrieval (from single table)
• Retrieving all attributes and all rows:-
• MYSL>select * from tablename;
- Select*from student;
• Retrieving some attributes only:-
MYQL>select fname,surname,sex,town from
student;
• Retieving certain records such as female
students:-
– Select * from student WHERE sex=“female”;
Retrieving Data from table(s)
– Retieving female students from ICTM :-
MYSQL>Select * from student where csid=“ictm” AND
sex=“female”;
– Retrieving students born between 1990 and 1995
MYSQL>select * from student WHERE dob BETWEEN
“1990” AND “1995”;
– Retrieving students born after 1992
MYSQL>select * from student WHERE dob>”1992”;
Retrieving Data from table(s)
• Basic data retrieval ( IN, IS NULL, =, NOT IN)
• Retrieve students from southern Tanzania
MYSQL>select * from student where town IN
(“mtwara”,”lindi”,”mikindani”,”songea”,”masasi”,”liwale”,”tun
duru”,”nachingwea”,”newala””ruvuma”);
• Retrieve female students not coming from southern zone
MYSQL>select * from student where town NOT IN
(“mtwara”,”lindi”,”mikindani”,”songea”,”masasi”,”liwale”,”tun
duru”,”nachingwea”,”newala””ruvuma”) and SEX=“female”;
• Retrieve list of students whom have not indicated date they
were born:-
MYQL>select * from student WHERE dob IS NULL;
Retrieving Data from table(s)
• The BETWEEN Operator
The BETWEEN operator selects a range of data between two values. The
values can be numbers, text, or dates.
• SQL BETWEEN/not between Syntax
MYSQL> SELECT column_name(s)
FROM table_name WHERE column_name BETWEEN value1
AND value2;
Answer:
Answer:
Answer:
Example :
• List students from ICTB, ICTM, and ITS
MYSQL>CREATE VIEW ICT_STUDENTS AS
SELECT * FROM STUDENT WHERE CSID IN
(“ICTB”,”ICTM”,”ITS”);
WORKING WITH VIEWS
• list of students who do not receive boom
MYSQL>CREATE VIEW students_with_no_boom as SELECT
* FROM STUDENT WHERE BOOM IS NULL;
MYSQL>desc EASTERN_STUDENT;
• Views can be queried same way relations do:-
example:
• To display views’ content
MYSQL>select * from students_with_netboom;
• To display female students_with_netboom
MYSQL>select * from student_with_netboom where
sex=“female”;
WORKING WITH VIEWS
• Adding content into views; depends on the type of view created.
Some views allows data entry, some do not allow data entry
-END-