Mysql Tutorial: Introduction To Database
Mysql Tutorial: Introduction To Database
Mysql Tutorial: Introduction To Database
Introduction to Database
Learning Objectives
Read and write Data Definition grammar of SQL
Read and write data modification statements
(INSERT, UPDATE, DELETE)
Read and write basic SELECT FROM WHERE
queries
Use aggregate functions
Introduction of MySQL
MySQL is an SQL (Structured Query Language)
based relational database management system
(DBMS)
MySQL is compatible with standard SQL
MySQL is frequently used by PHP and Perl
Commercial version of MySQL is also provided
(including technical support)
Part1: SQL used for Data
Definition
Allows the specification of not only a set of
relations but also information about each
relation, including:
The schema for each relation
The domain of values associated with each
attribute
Integrity constraints
Domain Types in SQL
Type Description
CHAR(n) Fixed length character string, with specified length n
VARCHAR(n) Variable length character string, with specified
maximum length n
INTEGER Integer (a machine-dependent finite subset of the
integers)
SMALLINT(n) A small integer (a finite subset of INTEGER)
FLOAT(M,D) Floating point number, with total number of digits M
and number of digits following the decimal point D
DOUBLE(M,D) Double-precision floating point number
Example
create database mydatabase
CREATE TABLE
An SQL relation is defined using the CREATE
TABLE command:
Create table [tablename] (A1 T1,A2 T2, … An Tn,
(integrity-constraint1),
…,
(integrity-constraintk))
Each Ai is an attribute name in the table
Each Ti is the data type of values for Ai
Example
Create table student
(flashlineID char(9) not null,
name varchar(30),
age integer, Integrity constraint
department varchar(20),
primary key (flashlineID) );
DROP and ALTER TABLE
The DROP TABLE command deletes all
information about the dropped relation from the
database
The ALTER TABLE command is used to add
attributes to or remove attributes from an
existing relation (table):
alter table tablename actions
where actions can be one of following actions:
ADD Attribute
DROP Attribute
ADD PRIMARY KEY (Attribute_name1,…)
DROP PRIMARY KEY
Part2: Modifying the database
3 basic cases:
Example
Select flashlineID, name from student
Where department=‘computer science’
The SELECT Clause – Duplicate
tuples
Unlike pure relational algebra, SQL does not automatically
remove duplicate tuples from relations or query results
Example
Find names of all students in computer science
department with age smaller than 18
select names
from student
where department=‘computer science’ and age<18
Aggregate Functions
Aggregate functions operate on the multiset of
values of a attribute and return a value
avg(attribute): average value
min(attribute): minimum value
max(attribute): maximum value
sum(attribute): sum of values
count(attribute): number of values
Note: if a select clause contains any aggregate functions, then all non-aggregated terms
in the select clause must be used in a group by clause. Ex: department is not
aggregated, so it must be in the group by clause.
Null Values and Aggregate
The youngest student in the university
select *
from student
where age=min(age)
Access MySQL
>mysql –u [username] –p
>password:[password]