Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Engineer
SQL Syntax : TRUNCATE table
• TRUNCATE TABLE
Engineer
SQL Syntax : INSERT row
• INSERT INTO Engineer
VALUES ('Nilsen',345598, 29,3 500,
‘Electrical')
SQL Syntax : INSERT data
• INSERT INTO Engineer (Eng_Name, Eng_Id, Dept_Name)
VALUES (‘Jen',495598,‘Mechanical')
Result of INSERT
• SELECT Eng_Name, Age
FROM Engineer;
Result :
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
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
BETWEEN Between an inclusive range
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 Age = 39
Result :
AND Dept_Name=‘Mechanical‘;
Result :
SELECT *
FROM Engineer
WHERE Salary = 3000
Result :
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
DELETE FROM Engineer
WHERE Eng_Name=‘Jen’;
Result :
Eng_Name Eng_Id Age Salary Dept_Name
SELECT table1.column1, table2.column2
FROM table1, table2
WHERE table1.column1 = table2.column1;
SQL Syntax : Sample Tables
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
SELECT Eng_Name, Dept_Name, Proj_Name
FROM Engineer, Project
WHERE Engineer.Eng_id = Project.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())
int main () {
if(mysql_query(connection, query.c_str()) != 0)
{
cout << "Error: " << mysql_error(connection) << endl;
return;
}
/* store the result from query*/
if(result == NULL)
{
cout << "Error: " << mysql_error(connection) << endl;
return;
}
...
Example - cont…
/* if no result */
int rows = mysql_num_rows(result);
if(rows == 0)
{
cout << "Information not found" << endl;
return;
/*print result*/
int string_to_int(string s)
{
instringstream instr(s);
int n;
instr >> n;
return n;
}
Summary