Database: TDB2073 - SPDB
Database: TDB2073 - SPDB
Database: TDB2073 - SPDB
Database
Part 2
Course Objectives
• Relational database
– a set of relations
• A relation is a table with columns and rows.
– Only applies to logical structure of the
database, not the physical structure.
• Attribute is a named column of a relation.
– Sometimes referred as fields
• Attribute Domain is the set of allowable
values for one or more attributes.
Relational Model Terminology
• Primary Key
– Candidate key selected to identify tuples
uniquely within relation.
• Foreign Key
– An attribute whose values match primary
key values in the related table
Relational Query Languages
CREATE TABLE
TableName
(
ColumnName1 Datatype1,
ColumnName2 Datatype2,
ColumnName3 Datatype3,
)
CREATE TABLE
Engineer
(
Eng_Name char(50),
Eng_Id int,
Age int,
Salary float,
Dept_Name char(50)
)
SQL Common data types
• To remove a column:
• ALTER TABLE
Engineer
DROP
Salary
SQL Syntax : DROP table
• DROP TABLE
Table_Name
• DROP TABLE
Engineer
SQL Syntax : TRUNCATE table
• TRUNCATE TABLE
Table_Name
• TRUNCATE TABLE
Engineer
SQL Syntax : INSERT row
Eng_Name Age
Faheem 24
Rahmanov 30
Mphatabalo 30
Kumar 28
Linda 39
Liew 26
Nilsen 29
Jen
SQL Syntax : SELECT DISTINCT
Dept_Name
Mechanical
Chemical
Petroleum
Civil
Electrical
SQL Syntax : SELECT and WHERE clause
• to extract only those records that fulfill a specified
criterion
SELECT column_name(s)
FROM table_name
WHERE column_name operator value;
SQL Syntax : SELECT and WHERE clause - RULES
This is wrong:
SELECT * FROM Engineer WHERE Eng_Name= Jen
This is wrong:
SELECT * FROM Engineer WHERE Salary=‘3500’
SQL Syntax : SELECT and WHERE clause
SELECT *
FROM Engineer
WHERE Dept_Name=‘Civil‘;
Result :
Operator Description
= Equal
<> Not Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
SELECT *
FROM Engineer
WHERE Age >= 30;
Result :
•The AND operator displays a record if both the first condition and
the second condition is true.
SELECT *
FROM Engineer
WHERE
Result : Age = 39
AND Dept_Name=‘Mechanical‘;
Result :
SELECT *
FROM Engineer
WHERE
Result : Salary = 3000
OR Dept_Name=‘Chemical‘;
Result :
SELECT *
FROM Engineer
WHERE Age = 30
AND (Dept_Name=‘Chemical‘ OR
Result :
Dept_Name=‘Petroleum‘);
Result :
UPDATE Engineer
SET Age = 30, Salary= 5000
WHERE Eng_Name=‘Jen' AND Dept_Name=‘Mechanical‘;
Result :
Eng_Name Eng_Id Age Salary Dept_Name
Result :
Eng_Name Eng_Id Age Salary Dept_Name
Table : Engineer
Eng_Name Eng_Id Age Salary Dept_Name
Faheem 223368 24 3 000 Mechanical
Rahmanov 137714 30 4 350 Chemical
Mphatabalo 117945 30 4580 Petroleum
Table : Project
Proj_Name Proj_Id Duration Customer_Id Eng_Id
Result :
• mysql_init()
– to initialize the connection structure to the
database
• mysql_real_connect(8 arguments)
– To establish connection to the server that
contains the a MySQL database
• mysql_error()
– To detect and display error message
• mysql_close()
– To close the connection
MySQL API Functions
• MYSQL_RES
– Structure type for information concerning the
results of a SELECT or SHOW query.
• MYSQL_ROW
– The array type of data for one row of a SELECT
query
MySQL API Functions (parameters and examples)
• mysql_query (connection pointer, SQL query statement)
– To query for data from the database
Ex :
string query = "SELECT Eng_Name, Age, Dept_Name FROM Engineer"
"WHERE Eng_Id = '"+engId+"'";
mysql_query(connection, query.c_str())
/*
/* perform
perform the
the query
query */
*/
void print_eng_info(MYSQL* connection, string engId)
void print_eng_info(MYSQL* connection, string engId)
{
{
string query = "SELECT Eng_Name, Age, Dept_Name FROM Engineer"
string query = "SELECT Eng_Name, Age, Dept_Name FROM Engineer"
"WHERE Eng_Id = '"+engId+"'";
"WHERE Eng_Id = '"+engId+"'";
/* check the query */
/* check the query */
if(mysql_query(connection, query.c_str()) != 0)
if(mysql_query(connection, query.c_str()) != 0)
{
{
cout << "Error: " << mysql_error(connection) << endl;
cout << "Error: " << mysql_error(connection) << endl;
return;
return;
}
/*}store the result from query*/
/* store the result from query*/
MYSQL_RES* result = mysql_store_result(connection);
MYSQL_RES* result = mysql_store_result(connection);
/*if the result cannot be retrieved*/
/*if the result cannot be retrieved*/
if(result == NULL)
if(result == NULL)
{
{
cout << "Error: " << mysql_error(connection) << endl;
cout << "Error: " << mysql_error(connection) << endl;
return;
return;
}
}
...
...
Example - cont…
/* if no result */
/* if no result */
int rows = mysql_num_rows(result);
int rows = mysql_num_rows(result);
if(rows == 0)
if(rows == 0)
{
{
cout << "Information not found" << endl;
cout << "Information not found" << endl;
return;
return;
}
}
/* get a row of data*/
/* get a row of data*/
MYSQL_ROW row = mysql_fetch_row(result);
MYSQL_ROW row = mysql_fetch_row(result);
string name = row[0];
string name = row[0];
int age = string_to_int(row[1]); //converts to int
int age = string_to_int(row[1]); //converts to int
string dept = row[2];
string dept = row[2];
/*print result*/
/*print result*/
cout << " Information found\n\n";
cout << " Information found\n\n";
cout << "Engineer ID : " << engId << endl;
cout << "Engineer ID : " << engId << endl;
cout << "Name : " << name << endl;
cout << "Name : " << name << endl;
cout << "Age : " << age << end;
cout << "Age : " << age << end;
cout << "Department : " << dept << endl;
cout << "Department : " << dept << endl;
}// end of function
}// end of function
Example - Cont…