Computer Applications

Saturday, January 23, 2010

What is MySQL?

1.MySQL is a database.
2.The data in MySQL is stored in database objects called tables.
3.A table is a collections of related data entries and it consists of columns and rows.

Queries:

A query is a question or a request.

With MySQL, we can query a database for specific information and have a recordset returned.

Create a Connection to a MySQL Database

Code:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>

Closing a Connection in PHP & MySQL.

Code:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
?>

Create Database and Tables in MySQL

Code:

$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 in MySQL

Code:

$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_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
( FirstName varchar(15),
LastName varchar(15),
Age int )";
mysql_query($sql,$con);
mysql_close($con);
?>

Insert Data Into a Database Table In MySQL

Code:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)VALUES ('Peter','Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>

Select Data From a Database Table in PHP,MySQL

Code :

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
}
mysql_close($con);
?>

The Where Clause in MySQL

Code:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
}
?>

The ORDER BY Keyword in MySQL

It is is used to sort the data in a record-set.

Code:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons ORDER BY age");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age'];
}
mysql_close($con);
?>

Update Data In a Database in PHP,MySQL

Code :
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);
mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
mysql_close($con);
?>

Delete Data In a Database in PHP,MySQL

Code:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
mysql_close($con);
?>

Tuesday, January 5, 2010

JavaScript Tutorial

JavaScript is The Scripting Language of the Web.

Example:


script type="text/javascript"
document.write("This is my first JavaScript!");
script


Output Is : This is my first JavaScript!

JavaScript is an easy-to-use programming language that can be embedded in the header of your web pages. It can enhance the dynamics and interactive features of your page by allowing you to perform calculations, check forms, write interactive games, add special effects, customize graphics selections, create security passwords and more.

C Tutorial.

C is a programming language of many different dialects, similar to the way that each spoken language has many different dialects.

Example:
#include
int main()
{
int this_is_a_number;
printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
return 0;
}

Output Is:
a = 4 * 6;
a = a + 5;
a == 5.
There are several common compilers: in particular, Borland C++, Microsoft C++, and GNU C. There are also many front-end environments for the different compilers the most common is Dev-C++ around GNU's G++ compiler.

C++ Tutorial

C++ is an object oriented language, which combines the best of the structured programming techniques of C, thus making it a very powerful language.
Example:
# include
int main()
{
std:: cout << “My first C++ program ” << std::endl;
return 0;
}

Output Is: My first C++ program

C++ is a superset of C; therefore most of C language constructs apply in C++ as well. A program in C++ can be written in both C style and Object Oriented style.

SQL (Structured Query Language).

SQL is a standard language for accessing databases.

1. SQL stands for Structured Query Language
2. SQL lets you access and manipulate databases
3. SQL is an ANSI (American National Standards Institute) standard

SQL can do

It can execute queries against a database
It can retrieve data from a database
It can insert records in a database
It can update records in a database
It can delete records from a database
It can create new databases
It can create new tables in a database
It can create stored procedures in a database
It can create views in a database
It can set permissions on tables, procedures, and views

SQL can be divided into two parts:
1.The Data Manipulation Language (DML)
2. The Data Definition Language (DDL).

DML

The query and update commands form the DML part of SQL:
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database

DDL

CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index

Creating Table in SQL

Each row represents one piece of data, and each column can be thought of as representing a component of that piece of data. So, for example, if we have a table for recording customer information, then the columns may include information such as First Name, Last Name, Address, City, Country, Birth Date, and so on.

SYNTAX

CREATE TABLE "table_name"
("column 1" "data_type_for_column_1",
"column 2" "data_type_for_column_2",
... )

Example

CREATE TABLE customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)

Insert Values into Table in SQL

One is to insert it one row at a time, the other is to insert multiple rows at a time. Let's first look at how we may INSERT data one row at a time.

Syntax

INSERT INTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...)

Example

INSERT INTO Store_Information (store_name, Sales, Date)
VALUES ('Los Angeles', 900, 'Jan-10-1999')

Select from Table

A common use is to select data from the tables located in a database. Immediately, we see two keywords: we need to SELECT information FROM a table.

Syntax

SELECT "column_name" FROM "table_name"

Example

SELECT store_name FROM Store_Information
Result:
store_name
Los Angeles
San Diego
Los Angeles
Boston

Update Command

   We can use the UPDATE command for modifying the data.

Syntax:

UPDATE "table_name" SET "column_1" = [new value] WHERE {condition}

Example:

UPDATE Store_Information SET Sales = 500 WHERE store_name = "Los Angeles"AND Date = "Jan-08-1999".

Delete Command

    We may wish to use a query to remove records from a table.

Syntax:

DELETE FROM "table_name" WHERE {condition}

Example :

DELETE FROM Store_Information WHERE store_name = "Los Angeles"