2015-06-09

MySQL Create DB

A database holds one or multiple tables.

Create a Database

The CREATE DATABASE statement is used to create a database in MySQL.

Syntax

CREATE DATABASE database_name 

To learn more about SQL, please visit our
SQL tutorial.
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.

Example

The following example creates a database called "my_db":


<?php

$con = mysql_connect("localhost","peter","abc123");
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.

Syntax

CREATE TABLE table_name
(
  column_name1 data_type,
  column_name2 data_type,
  column_name3 data_type,
  ....  ) 

To learn more about SQL, please visit our SQL tutorial.
We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

Example

The following example creates a table named "Persons", with three columns. The column names will be "FirstName", "LastName" and "Age":
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
 die('Could not connect: ' . mysql_error());
}

// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
  echo "Error creating database: " . mysql_error();
}

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);
mysql_close($con);

?>

Important:A database must be selected before a table can be created. The database is selected with the mysql_select_db() function.

Note:When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(15).


The data type specifies what type of data the column can hold. For a complete reference of all the data types available in MySQL, go to complete Data Types reference.

MySQL Connect

The free MySQL database is very often used with PHP.

Create a Connection to a MySQL Database

Before you can access data in a database, you must create a connection to the database.
In PHP, this is done with the mysql_connect() function.

Syntax

mysql_connect(servername,username,password);

Parameter
Description
servername
Optional. Specifies the server to connect to. Default value is "localhost:3306"
username
Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process
password
Optional. Specifies the password to log in with. Default is ""

Example

In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
<?php
  $con = mysql_connect("localhost","peter","abc123");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }
  // some code
?>

Closing a Connection

The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:
<?php
  $con = mysql_connect("localhost","peter","abc123");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }
  // some code  
  mysql_close($con);
?>


MySQL Database

MySQL Introduction

MySQL is the most popular open-source database system.

What is MySQL?
MySQL is a database.
The data in MySQL is stored in database objects called tables.
A table is a collections of related data entries and it consists of columns and rows.
Databases are useful when storing information categorically. A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders".


Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
Below is an example of a table called "Persons":
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
The table above contains three records (one for each person) and four columns (LastName, FirstName, Address, and City).
Queries
A query is a question or a request.
With MySQL, we can query a database for specific information and have a recordset returned.
Look at the following query:
SELECT LastName FROM Persons 
The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this:

  LastName   
  Hansen        
  Svendson     
  Pettersen     


RDBMS

RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
SQL is a standard language for accessing and manipulating databases.


What is SQL?
  • SQL stands for Structured Query Language
  • SQL lets you access and manipulate databases
  • SQL is an ANSI (American National Standards Institute) standard


What Can SQL do?
  • SQL can execute queries against a database
  • SQL can retrieve data from a database
  • SQL can insert records in a database
  • SQL can update records in a database
  • SQL can delete records from a database
  • SQL can create new databases
  • SQL can create new tables in a database
  • SQL can create stored procedures in a database
  • SQL can create views in a database
  • SQL can set permissions on tables, procedures, and views




Step by Step for HTML Beginner

Write HTML Using Notepad or TextEdit

HTML can be edited by using a professional HTML editor like:
Editor Description Downlaod
Adobe Dreamweaver Download
Microsoft Expression Web Download
CoffeeCup HTML Editor Download
Netbeans Download
Eclipse Download
Notepad ++ Download
Sublime Download
However, for learning HTML we recommend a text editor like Notepad (PC) or TextEdit (Mac).
We believe using a simple text editor is a good way to learn HTML.

Follow the 4 steps below to create your first web page with Notepad.


Step 1: Open Notepad

To open Notepad in Windows 7 or earlier:
Click Start (bottom left on your screen). Click All Programs. Click Accessories. Click Notepad.
To open Notepad in Windows 8 or later:
Open the Start Screen (the window symbol at the bottom left on your screen). Type Notepad.

Step 2: Write Some HTML

Write or copy some HTML into Notepad.

<!DOCTYPE html>
<html>
<head>
  <title> My First web page </title>
</head>
<body>
  <h1>My First Heading</h1>
  <p>My first paragraph.</p>
</body>
</html>

Step 3: Save the HTML Page

Save the file on your computer.

Select File > Save as in the Notepad menu.
Name the file "index.html" or "index.htm" or any name ending with ".html".
UTF-8 is the preferred encoding for HTML files. ANSI encoding covers US and Western European characters only.
You can use either .htm or .html as file extension. There is no difference, it is up to you.

Step 4: View HTML Page in Your Browser

Open the saved HTML file in your favorite browser. The result will look much like this:

To open a file in a browser, double click on the file, or right-click, and choose open with.

Tools & Plugins