Professional Documents
Culture Documents
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
9
Unique constraint
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
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