Latest Updates:
Upcoming notes on: Java and Android


MySQL

INTRODUCTION OF MYSQL
MySQL is an example of RDBMS (Relational Database Management System) and its an open source application, that is based on SQL (Structured Query Language).



Features of RDBMS
Table: Table is also known as “Relation” and it is responsible to store actual data.

Row: Row is also known as “Tuple” and row is a collection of related fields.

Column: Column is also known as “Attribute” and every column contain similar values.


Primary key: This key consisted by a unique value, that is used to uniquely identify a record from a selected table, for example (Roll_no, Emp_code, Customer code, etc.)

Open MYSQL In XAMPP
1. Open the web browser, like google chrome
2. Type "localhost/phpmyadmin"
3. Press Enter Key

Common Mysql Commands
Show databases
(To show all already existing databases)
Create database abc
(To create a new database)
Drop database abc
(To delete a database)
Use xyz;              
(To open a database)
Show tables
(To show all the tables)
Select * from data
(To show all records from data table)


PHP Program To Create Database
<?php
mysql_connect("localhost","root","");

$s=mysql_query("create database newone");

if($s==true)
{
            echo "Database created successfully";
}
else
{
            echo "Sorry, could not create";
}
?>

Program Explanation: In the above program 'mysql_connect' is a PHP command that is used to connect your PHP program with MYSQL.

localhost: Its a server name.

root: Its a default admin user name of MYSQL.

password: The default password of 'Root' user is blank.

mysql_query: This PHP command is used to insert a mysql command.

PHP Program To Create Table
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");

$data=mysql_query("create table data(id int(10) NOT NULL auto_increment ,Name char(20) NOT NULL, Address varchar(100) NOT NULL, primary key(id)) ");
if($data==true)
{
            echo "connect";
}
else
{
            echo "not connect";
}
?>


Change auto increment default value
alter table Student auto_increment=100;


Program To Insert Records In Table
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$data=mysql_query("insert into data(id,Name,Address) values
('','Ramesh','New Delhi');");
if($data==true)
{
            echo "connect";
}
else
{
            echo "not connect";
}
?>


Program To Show All Record From A Table
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$data=mysql_query("select * from data");

while($rec=mysql_fetch_row($data))
{
echo "$rec[0]  $rec[1] $rec[2]<br>";
}
?>

Program To Delete Record From A Table
<?php

mysql_connect("localhost","root");
mysql_select_db("test");
mysql_query("delete from data where id=2");

?>


Program To Change A Record
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$data=mysql_query("update student set Name='Ashok kumar

',Address='Narela' where roll_no=1");
if($data==true)
{
            echo "Record updated";
}
else
{
            echo "Could not update";
}
?>


Program To Search A Record
<form method="post" action="#">
Enter any number <input type="text" name="num">
<input type="submit" value="Search" name="sub">

<?php
mysql_connect("localhost","root","");
if(isset($_POST['sub']))
{
$s=$_POST['num'];

mysql_select_db("test");

$data=mysql_query("select * from student where roll_no=$s");

while($rec=mysql_fetch_row($data))
{
echo "<br>$rec[0] $rec[1] $rec[2]";
}
}
?>  

No comments:

Post a Comment