You are on page 1of 16

Tutorial 3

Database Introduction

1
Introduction
• Database: is a collection of related data.
• DBMS: is a collection of programs that enables users to
create and maintain a database
• It is a general-purpose software system enables us in
Defining, Constructing and Manipulating DB:
Defining: specifying the data types, structures and
constraints
Constructing: storing the data in the DB
Manipulating: as querying, updating and
generating reports
Database + DBMS Database System

2
Simplified Database System
Environment

3
HTTP Request

HTTP Response
Table
• Table is a collection of related data held in a
structured format within a database. It consists
of fields (columns), and records (rows)
• Table has a specified number of fields, but can
have any number of records.
• Record (row) represents a single, implicitly
structured data item in a table.
• Fields (Column) is a set of data values of a
particular simple type, one for each row of the
table.

5
Create table example
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type,
....
);
• The column_name parameter specifies the name of the
column of the table.
• The data_type parameter specifies what type of data the
column can hold.
• The size parameter specifies the maximum length of the
column of the table.
• (Some data types do not need size parameter)
6
MySQL Data_Type
• BIT
• TINYINT
• INT
• BIGINT
• DECIMAL(length,decimals)
• DATE
• TIME
• DATETIME
• VARCHAR(length)
• TEXT

7
Create table script example
CREATE TABLE person
(
person_id INT, --default length is 11
last_name VARCHAR(255),
first_name VARCHAR (255),
address VARCHAR (255),
city VARCHAR (255),
gender BIT,
birthdate DATE,
register_datetime DATETIME,
note TEXT
);

8
Primary key constraint

• The PRIMARY KEY constraint uniquely identifies


each record in a database table.
• Primary keys must contain UNIQUE values.
• A primary key column cannot contain NULL values.
• Most tables should have a primary key, and each
table can have only ONE primary key.

9
Unique constraint

• The UNIQUE constraint uniquely identifies each


record in a database table.
• The UNIQUE and PRIMARY KEY constraints both
provide a guarantee for uniqueness for a column or
set of columns.
• A PRIMARY KEY constraint automatically has a
UNIQUE constraint defined on it.
• Note that you can have many UNIQUE constraints
per table, but only one PRIMARY KEY constraint per
table.
10
Foreign key constraint
• A FOREIGN KEY in one table points to a PRIMARY KEY
in another table.
• A FOREIGN KEY constraint does not have to be linked
only to a PRIMARY KEY constraint in another table; it
can also be defined to reference the columns of a
UNIQUE constraint in another table.
• The FOREIGN KEY constraint is used to prevent actions
that would destroy links between tables.
• The FOREIGN KEY constraint also prevents invalid data
from being inserted into the foreign key column,
because it has to be one of the values contained in the
table it points to.
11
Student and department Example

Student
(student_id, name, email, gender, dept_no)

Department
(department_no, name, location)

12
department_no Name location
Computer and System
1 building #1
Engineering

2 Electrical Engineering building #1

student_id Name Email gender dept_no


1 Ahmed ahmed@test.com 0 1
2 Hany hany@test.com 0 1
3 Nader nader@test.com 0 2
4 Nada nada@test.com 1 2

13
Student and department DDL
create database faculty;
use faculty;
create table department (
department_no INT(4) not null,
name VARCHAR(100) not null,
location VARCHAR(255),
PRIMARY KEY(department_no)
);
Student and department DDL (cont)
create table student (
student_id INT(4) not null,
name VARCHAR(100) not null,
email VARCHAR(100) UNIQUE,
gender BIT,
dept_no INT(4),
PRIMARY KEY(student_id)
);
ALTER TABLE student ADD FOREIGN KEY (dept_no)
REFERENCES department(department_no);
References
Mysql 5.7 data types
• https://dev.mysql.com/doc/refman/5.7/en/numeric-
type-overview.html
• https://dev.mysql.com/doc/refman/5.7/en/date-and-
time-type-overview.html
• https://dev.mysql.com/doc/refman/5.7/en/string-
type-overview.html
Constraints
• http://www.w3schools.com/sql/sql_primarykey.asp
• http://www.w3schools.com/sql/sql_unique.asp
• http://www.w3schools.com/sql/sql_foreignkey.asp

16

You might also like