TUTORIAL 1 DATABASE PRACTICE:
1. Some Common SQL Data Types:
• Data type selection is usually dictated by the nature of the data and by the
intended use.
Data Type Format Description
Numeric INTEGER -2 147 483 648
+2 147 483 647
SMALLINT -32 768 32 767
DECIMAL DECIMAL(5,2) 123.45
(L,D)
Character CHAR(L) CHAR(5)
If ‘Ali’, database keeps ‘Ali__’ 2
space wasted.
VARCHAR(L) VARCHAR(5)
If ‘Ali’, database keeps 3 space
only.
Date DATE Year 0001 – 9999
Month 01 –12
Day 01 - 31
2. Creating our own database:
CREATE DATABASE <database name>;
CREATE TABLE <table name> (
<attribute1 name and attribute1 characteristics,
attribute2 name and attribute2 characteristics,
attribute3 name and attribute3 characteristics,
primary key designation,
foreign key designation and foreign key requirements>);
• Data Entry
INSERT INTO <table name> VALUES (attribute 1 value, attribute 2 value, … etc.);
E-R DIAGRAM
1 M
PROFESSOR MANAG STUDENT
E
– CREATE DATABASE COLLEGE;
– USE COLLEGE;
CREATE TABLE PROFESSOR
(PROF_ID INTEGER NOT NULL PRIMARY KEY,
PROF _LNAME VARCHAR (20),
PROF _FNAME VARCHAR (20),
PROF _CONTACT VARCHAR (15),
PROF _SKILL VARCHAR (20),
PROF _PHONE INTEGER,
PROF _EMAIL VARCHAR(20));
CREATE TABLE STUDENT
(STU_ID INTEGER NOT NULL PRIMARY KEY,
STU_LNAME VARCHAR (20),
STU_FNAME VARCHAR (20),
STU_CONTACT VARCHAR(15),
STU_SECTION VARCHAR(20),
STU_PHONE VARCHAR(10),
STU_EMAIL VARCHAR(20),
PROF_ID INTEGER NOT NULL,
FOREIGN KEY (PROF_ID) REFERENCES PROFESSOR (PROF_ID) ON
DELETE CASCADE ON UPDATE CASCADE);
INSERT INTO PROFESSOR VALUES (101, ‘Hamid’, ‘Abdul’, ‘Damansara’, 989456,
‘Mathematics’, ‘
[email protected]’);
INSERT INTO PROFESSOR VALUES (102, ‘Hasan’, ‘Reza’, ‘Bangi’, ‘Java’, 989656,
‘
[email protected]’);
INSERT INTO PROFESSOR VALUES (103, ‘Islam’, ‘Zairul’, ‘Penang’, ‘Database’,
989856, ‘
[email protected]’);
INSERT INTO STUDENT VALUES (201, ‘Islam’, ‘Khairul’, ‘kedah’, ‘2nd year’,
8780978, ‘
[email protected]’, 101);
INSERT INTO STUDENT VALUES (202, ‘Patel’, ‘Preveen’, ‘PJ’, ‘3rd year’, 8785978,
‘
[email protected]’, 102);
Query:
1. Show all the Professor Information.
Select * from PROFESSOR;
2. Show all the Student Information.
Select * from STUDENT;
3. Display student last name and first name those are from Kedah.
Select STU_LNAME, STU_FNAME from STUDENT where STU_CONTACT =
‘Kedah’;
4. Display professor last name and first name those skills is Java
Select PROF_LNAME, PROF_FNAME, from PROFESSOR where PROF_SKILL =
‘JAVA’
5. Display the lecturers’ name who manages students.
Select PROF_LNAME, PROF_FNAME, STU_LNAME, STU_FNAME from
PROFESSOR, STUDENT where PROFESSOR.PROF_ID = STUDENT.PROF_ID;
DROP TABLE PROFESSOR;
DROP TABLE STUDENT;
DROP DATABASE COLLEGE;