Professional Documents
Culture Documents
Relational Databases
Relational Database: A method of structuring data as tables associated to each other by shared attributes.
A table row corresponds to a record; a column corresponds to an attribute (field) of the record
Relational databases typically use Structured Query Language (SQL) to define, manage, and search data
Wikipedia: http://en.wikipedia.org/wiki/Relational_database
Database Software
Oracle Database
Microsoft SQL Server (powerful) and Microsoft Access (simple) IBM DB2
http://www.mysql.com/
Database Design
Database Design: the act of deciding the schema for a database
Database Schema: a description of what tables a database should have, what columns each table should contain, which columns' values must be unique, etc.
What's good and bad about this design? Uses only one table, but contains redundancy
Splitting data into two tables (linked by Student ID) avoids redundancy This is also called normalizing the database Normalized tables are often linked by unique integer IDs
A language for searching and updating a database A standard syntax that is used by all database software (with minor incompatibilities)
See: http://www.w3schools.com/php/php_mysql_intro.asp
The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function.
Create a Database
To create a MySQL database you can use:
CREATE DATABASE database_name To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
Create a Database
<?php $con = mysql_connect("localhost", "root", "pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?>
Create a Table
The CREATE TABLE statement is used to create a table in MySQL.
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )
We must add the CREATE TABLE statement to the mysql_query() function to execute the command.
Create a Table
$sql = "CREATE TABLE Persons ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )";
mysql_query($sql,$con);
mysql_close($con); ?>