Академический Документы
Профессиональный Документы
Культура Документы
Programming
Kishan wimalawrne
Introduction
• Databases are used to data in a permanent
storage.
• Mush more efficient than using a file to write
and read from a file.
– Better structured representations
– Easy usage
• Relational databases
RDMS systems
• Many database products (commercial and
opensource)
– Mysql
– Oracle
– Ms Access
• SQL – Structured Query Language
– Used in working with databases
Mysql
• We will be using MySql which comes with
wamp server.
• Graphical support comes with phpmyadmin
• First we need to create a database.
– Command: (SQL)
Create database database_name
Logical table
• Data can be models as tables.
– E.g.:- details of people
Create a table
• CREATE Sql statement
Syntax
CREATE TABLE table_name ( column_name1 data_type,
column_name2 data_type,
column_name3 data_type, .... )
E.g-
CREATE TABLE Persons ( P_Id int, LastName varchar(255), FirstName
varchar(255), Address varchar(255), City varchar(255) )
Retrieving values from a table
• SELECT SQL statement
SELECT column_name(s) FROM table_name
SELECT * FROM table_name
e.g.-
SELECT LastName,FirstName FROM Persons
Retrieving values from a table
Use SUM() ,COUNT() , MAX() …. Functions
Select COUNT(*) from Persons
-> 3
while(rs.next())
{
int id= rs.getInt(“p_id");
String ln= rs.getString(“LastName");
String fn= rs.getString(“FirstName");
System.out.println("\t ID= " + id+ "\tName = " + fn + “ “ + ln);
}