> Online tutorial : establishing a connection to sql server
Showing posts with label establishing a connection to sql server. Show all posts
Showing posts with label establishing a connection to sql server. Show all posts

create table in mysql

Create a Table
The CREATE TABLE statement is used to create a database table in MySQL.
Syntax


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.
Example
The following example shows how you can create a table named "person", with three columns.
The column names will be "FirstName", "LastName" and "Age":

≤?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE data",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table in my_db database
mysql_select_db("data", $con);
$sql = "CREATE TABLE person
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
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).

establishing a connection to sql server

Establishing a Connection
The first thing you need to do is establish a connection with the DBMS you want to use.
This involves two steps:

(1) loading the driver and
(2) making the connection.

Loading Drivers
Loading the driver or drivers you want to use is very simple and involves just one line of
code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following
code will load it:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Your driver documentation will give you the class name to use.
For instance, if the class name is jdbc.DriverXYZ ,
you would load the driver with the following line of code:
Class.forName("jdbc.DriverXYZ");
You do not need to create an instance of a driver and register it with the DriverManager
because calling Class.forName will do that for you automatically. If you were to create
your own instance, you would be creating an unnecessary duplicate, but it would do no
harm.
When you have loaded a driver, it is available for making a connection with a DBMS.
Making the Connection
The second step in establishing a connection is to have the appropriate driver connect to
the DBMS. The following line of code illustrates the general idea:
Connection con = DriverManager.getConnection(url,
"myLogin", "myPassword");


This step is also simple, with the hardest thing being what to supply for url .
If you are using the JDBC-ODBC Bridge driver, the JDBC URL will start with jdbc:odbc: .
 The rest of the URL is generally your data source name or database system. So, if you are using
ODBC to access an ODBC data source called " Fred, " for example, your JDBC URL
could be jdbc:odbc:Fred . In place of " myLogin " you put the name you use to log in to
the DBMS; in place of " myPassword " you put your password for the DBMS. So if you
log in to your DBMS with a login name of " Fernanda " and a password of " J8, " just
these two lines of code will establish a connection:

String url = "jdbc:odbc:Fred";
Connection con = DriverManager.getConnection(url, "Fernanda", "J8");



If you are using a JDBC driver developed by a third party, the documentation will tell
you what subprotocol to use, that is, what to put after jdbc: in the JDBC URL.


Buy It Now