Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Sunday, 24 November 2013

Export Excel data to MySQL database


We will show you how to export Excel data to MySQL db.

The steps are:

1) saved the excel data to csv format, such as below
BK001,Introduction to science,976,85.0
BK002,Human Anatomy,985,200.0
BK003,Concepts in Health,765,100.5

2) create a db table that matches the csv format
mysql> create table book_info
    -> (book_id varchar(20), book_name varchar(40), isbn_no int(11), book_price, int(10));

3) run the mysql command
mysql> load data infile 'c:\\Book1.csv' into table book_info fields terminated by ',' lines terminated by '\n';
Query OK, 3 rows affected (0.07 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

After that, the db contains the Excel data, To verify, issue the command:

mysql> select * from book_info;
+---------+-------------------------+---------+------------+
| book_id | book_name               | isbn_no | book_price |
+---------+-------------------------+---------+------------+
| BK001   | Introduction to science |     976 |         85 |
| BK002   | Human Anatomy           |     985 |        200 |
| BK003   | Concepts in Health      |     765 |        101 |
+---------+-------------------------+---------+------------+
3 rows in set (0.00 sec)

Thursday, 8 August 2013

User login with PHP/MySQL

Firstly, create a database entry and add a value to it:

mysql> CREATE TABLE `members` (
    -> `id` int(4) NOT NULL auto_increment,
    -> `username` varchar(65) NOT NULL default '',
    -> `password` varchar(65) NOT NULL default '',
    -> PRIMARY KEY (`id`)
    -> ) engine=MyISAM AUTO_INCREMENT=2 ;
Query OK, 0 rows affected (0.06 sec)

mysql> insert into members
    -> values
    -> (1, 'john', 'qwerty');
Query OK, 1 row affected (0.06 sec)

Secondly, use PHP to create a login screen:

<p align=center>You must login with the username and password to gain access:</p>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Thirdly, use a PHP to get username and password and compare with value in SQL:

<?php

session_start();
ob_start();

error_reporting(E_ALL);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
//header("location:login_success.php");
echo '<meta http-equiv="refresh" content="0;url=login_success.php">';
}
else {
//echo "Wrong Username or Password";
echo '<meta http-equiv="refresh" content="0;url=main_login.php">';
}
ob_end_flush();
?>

In the login_success.php, you can do any web page design:

<?php
// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.
session_start();
echo $_SESSION['myusername'];
if(isset($_SESSION['myusername'])){
  //header("location:main_login.php");
  } else {
   echo '<meta http-equiv="refresh" content="0;url=main_login.php">';
}
?>

<html>
<body>
<b>Login Successful.</b><br>
Please click this to  <a href=logout.php>logout</a>
</body>
</html>

The logout.php is simple:

<?php
// Put this code in first line of web page.
print "logout";
session_start();
session_destroy();
?>
<p>Please click this to  <a href=main_login.php>login</a></p>

Now we will see how to encrypt the password.

Encrypt password with php

$password="john856";
$encrypt_password=md5($password);
echo $encrypt_password;
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$encrypted_password'";
$result=mysql_query($sql);

Thursday, 25 July 2013

Store a file in MySQL database

Firstly, create a table in database. We use longblob as the data type to store the content of the file.

mysql> CREATE TABLE IF NOT EXISTS `Files` (
    ->   `ID` int(25) NOT NULL auto_increment,
    ->   `Title` varchar(250) NOT NULL default '',
    ->   `File_Name` varchar(250) NOT NULL default '',
    ->   `File_Type` varchar(15) NOT NULL default '',
    ->   `File_Size` varchar(45) NOT NULL default '',
    ->   `File_Content` longblob NOT NULL,
    ->   `File_Extension` varchar(10) NOT NULL default '',
    ->   PRIMARY KEY  (`ID`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.76 sec)

The html form to call the php script. The enctype of the form must be multipart/form-data.

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

The php script to read the file, store it into the database, and copy it to upload folder of the web server.

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "text/html")
|| ($_FILES["file"]["type"] == "text/plain"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }

 // store in SQL
 $user_name = "root";
      $password = "root";
      $database = "test";
      $server = "localhost";

 $fileName = $_FILES['file']['name'];
      $tmpName  = $_FILES['file']['tmp_name'];
      $fileSize = $_FILES['file']['size'];
      $fileType = $_FILES['file']['type'];

 // Slurp the content of the file into a variable            
      $fp = fopen($tmpName, 'r');
      $content = fread($fp, filesize($tmpName));
      $content = addslashes($content);
      fclose($fp);
      if(!get_magic_quotes_gpc()){
          $fileName = addslashes($fileName);
      }
 $file_info = pathinfo($_FILES['file']['name']);

      // Connect to the database
      $dbh = mysql_connect($server,$user_name,$password) or die("There was a problem with the database connection.");
      $dbs = mysql_select_db($database, $dbh) or die("There was a problem selecting the categories.");
 $sql = "INSERT INTO Files SET
                Title = 'Title',
                File_Name = '".$fileName."',
                File_Type = '".$fileType."',
                File_Size = '".$fileSize."',
                File_Content = '".$content."',
                File_Extension = '".$file_info['extension']."'";
      //print $sql;        
             
      $result = mysql_query($sql);
 if(!$result){
        echo "Could not add this file.";  
      }
      else{
        echo  "New file successfully added.";
      }
 mysql_close($dbh);
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Wednesday, 17 July 2013

Using Python to access MySQL Database

Setup Apache2.4 for Python

In Apache httpd.conf:

edit the Options field, add in ExecCGI.

Options Indexes FollowSymLinks ExecCGI

edit the AddHandler field, add in .py

AddHandler cgi-script .cgi .py

Run Python script in Web browser

If you use Python3 interpreter, the script must be written in Python3 version.

The test.py script to output the Hello World on the browser. Notice the first line #!/Python33/python is the syntax to refer to Windows Python interpreter.

#!/Python33/python

# enable debugging
import cgitb

cgitb.enable()

print("Content-Type: text/plain;charset=utf-8")
print()

print("Hello World!")


The script to output cgi environment.

#!/Python33/python

import cgi
cgi.test()

If you want to access to MySQL database from python, here is another simple script to do so. In the script, the database is called "test", the table is called "doctor".

#!/Python33/python

import datetime
import mysql.connector

config = {
  'user': 'root',
  'password': 'root',
  'host': '127.0.0.1',
  'database': 'test',
  'raise_on_warnings': True,
}

print("Content-Type: text/plain;charset=utf-8")
print()
print("read doctor database")

cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()

query = ("SELECT * FROM doctor")      

cursor.execute(query)

results = cursor.fetchall()
print(results)

cursor.close()
cnx.close()


Tuesday, 16 July 2013

Using PHP to access MySQL database

After we have setup the Apache httpd and php environment, we can write php script to access the MySQL database.

For example, we can build a MySQL database upfront. Then we use a php script to display the data items in the browser.

An example php script to do this. eg: display_worker.php

<?PHP

$user_name = "root";
$password = "xxxxxx";
$database = "dummyonly";
$server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM worker";
$result = mysql_query($SQL);

print "<table border=\"1\" style=\"border:1px solid black;\" cellspacing=\"2\" cellpadding=\"4\">";
print "<tr>";

print "<td>";
print "name<BR>";
print "birth<BR>";
print "skill<BR>";
print "</td>";

while ( $db_field = mysql_fetch_assoc($result) ) {

print "<td>";
print $db_field['name'] . "<BR>";
print $db_field['birth'] . "<BR>";
print $db_field['skill'] . "<BR>";
print "</td>";

}
print "</tr>";
print "</table>";

mysql_close($db_handle);

}
else {

print "Database NOT Found ";
mysql_close($db_handle);

}

?>

We can also use a php script to insert new data to the database.

Firstly, we need the HTML form. eg: insert_worker.html

<html>
<body>

<form action="insert_worker.php" method="post">
Name: <input type="text" name="name">
Birthdate: <input type="text" name="birth">
Skill: <input type="text" name="skill">
<input type="submit">
</form>

</body>
</html>

Then, the php script to insert new data. eg: insert_worker.php

<?php

$user_name = "root";
$password = "xxxxxx";
$database = "dummyonly";
$server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO worker (name, birth, skill)
VALUES
('$_POST[name]','$_POST[birth]','$_POST[skill]')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error($db_handle));
  }
echo "1 record added";

mysql_close($db_handle);

}

?>

Monday, 15 July 2013

SQL Tutorial

Some basic SQLite commands:

.schema table_name
select * from table_name;
select * from table_name where id='640';
.table
delete from devices;
delete from devices where id=25;
alter table devices ADD batteryStatus VARCHAR(255);
create table if not exists table_name (number, name);
create table table_name (Column1 int,
        Column2 varchar(255),
        Column3 varchar(255)
        );
drop table table_name;
.quit

Basic MySQL:

You can access MySQL in command line just by typing:
C:\Program Files\mysql\bin> mysql -u root -p

After which you can type sql commands normally such as:
mysql> SHOW DATABASES;

(Here, I am assuming you mySQL installation directory is "C:\Program Files\mysql")

The sample commands:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.17 sec)

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.12-log |
+------------+
1 row in set (0.03 sec)

mysql> select version(), current_date;
+------------+--------------+
| version()  | current_date |
+------------+--------------+
| 5.6.12-log | 2013-07-16   |
+------------+--------------+
1 row in set (0.07 sec)

mysql> select sin(pi()/4), (4+1)*5;
+--------------------+---------+
| sin(pi()/4)        | (4+1)*5 |
+--------------------+---------+
| 0.7071067811865475 |      25 |
+--------------------+---------+
1 row in set (0.06 sec)

mysql> select version(); select now();
+------------+
| version()  |
+------------+
| 5.6.12-log |
+------------+
1 row in set (0.00 sec)

+---------------------+
| now()               |
+---------------------+
| 2013-07-16 11:14:01 |
+---------------------+
1 row in set (0.00 sec)

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> create database medical;
Query OK, 1 row affected (0.06 sec)

mysql> use medical
Database changed
mysql> show tables;
Empty set (0.01 sec)

mysql> create table doctor (name varchar(25), birth date);
Query OK, 0 rows affected (0.19 sec)

mysql> show tables;
+-------------------+
| Tables_in_medical |
+-------------------+
| doctor            |
+-------------------+
1 row in set (0.00 sec)

mysql> insert into doctor
    -> values ('ken','1999-3-3');
Query OK, 1 row affected (0.04 sec)

mysql> describe doctor;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(25) | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from doctor;
+------+------------+
| name | birth      |
+------+------------+
| ken  | 1999-03-03 |
+------+------------+
1 row in set (0.02 sec)

mysql> insert into doctor
    -> values ('boa','1977-3-3');
Query OK, 1 row affected (0.05 sec)

mysql> update doctor set birth = '1989-3-3' where name = 'ken';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from doctor;
+------+------------+
| name | birth      |
+------+------------+
| ken  | 1989-03-03 |
| boa  | 1977-03-03 |
+------+------------+
2 rows in set (0.00 sec)

mysql> select * from doctor where name = 'ken';
+------+------------+
| name | birth      |
+------+------------+
| ken  | 1989-03-03 |
+------+------------+
1 row in set (0.00 sec)

mysql> select * from doctor where birth >= '1970-1-1';
+------+------------+
| name | birth      |
+------+------------+
| ken  | 1989-03-03 |
| boa  | 1977-03-03 |
+------+------------+
2 rows in set (0.00 sec)

mysql> alter table doctor ADD skill VARCHAR(255);
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe doctor;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(25)  | YES  |     | NULL    |       |
| birth | date         | YES  |     | NULL    |       |
| skill | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> delete from doctor where birth='1111-01-01';
Query OK, 1 row affected (0.04 sec)

mysql> rename table cus_item to patient_item;
Query OK, 0 rows affected (0.14 sec)

mysql> alter table patient_item change bp_time item_time datetime;
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

**SQL script**
use medical;
SET SQL_SAFE_UPDATES=0;
update doctor set skill = 'gastronomy' where name = 'ken';
select * from doctor;

Advanced MySQL commands

** change a column to be a primary key **
mysql> alter table patient add column patient_id int(10) unsigned primary key auto_increment;
Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

** delete a column from table **
mysql> alter table patient drop column patient_id;
Query OK, 2 rows affected (0.02 sec)

Records: 2  Duplicates: 0  Warnings: 0

** Output to Excel format **
mysql> select * from doctor into outfile 'doctor.csv' fields terminated by ',';

** move SQL column order sequence **
mysql> ALTER TABLE Employees MODIFY COLUMN empName VARCHAR(50) AFTER department;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

** change SQL column data type **
mysql> alter table patient modify column Gluco int;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

** select and count **
mysql> select a.*, count(name) as total  from doctor a;
+------+------------+----------+-------+
| name | birth      | skill    | total |
+------+------------+----------+-------+
| ken  | 1989-03-03 | surgeon1 |     3 |
+------+------------+----------+-------+
1 row in set (0.00 sec)

** foreign key, primary key **
mysql> create table patient
    -> (cus_id int NOT NULL, cus_name varchar(50), primary key(cus_id));
Query OK, 0 rows affected (1.14 sec)

mysql> describe patient;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| cus_id   | int(11)     | NO   | PRI | NULL    |       |
| cus_name | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.26 sec)

mysql> create table cus_item
    -> (cus_item_id int NOT NULL, cus_id int, bp int, bp_time datetime, primary
key(cus_item_id), foreign key(cus_id) references `patient`(cus_id));
Query OK, 0 rows affected (0.15 sec)

mysql> describe cus_item;
+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| cus_item_id | int(11)  | NO   | PRI | NULL    |       |
| cus_id      | int(11)  | YES  | MUL | NULL    |       |
| bp          | int(11)  | YES  |     | NULL    |       |
| bp_time     | datetime | YES  |     | NULL    |       |
+-------------+----------+------+-----+---------+-------+
4 rows in set (0.09 sec)

mysql> select * from `patient` o join cus_item oi
    -> on o.cus_id = oi.cus_id
    -> where o.cus_id = 5;
Empty set (0.11 sec)

mysql> insert into patient values
    -> (5,'poi');
Query OK, 1 row affected (0.08 sec)

mysql> insert into cus_item values
    -> (1, 5, 89, '1999-01-01 00:00:00');
Query OK, 1 row affected (0.01 sec)

mysql> insert into cus_item values
    -> (2, 5, 69, '1999-02-01 00:00:00');
Query OK, 1 row affected (0.04 sec)

mysql> select * from `patient` o join cus_item oi
    -> on o.cus_id = oi.cus_id
    -> where o.cus_id = 5;
+--------+----------+-------------+--------+------+---------------------+
| cus_id | cus_name | cus_item_id | cus_id | bp   | bp_time             |
+--------+----------+-------------+--------+------+---------------------+
|      5 | poi      |           1 |      5 |   89 | 1999-01-01 00:00:00 |
|      5 | poi      |           2 |      5 |   69 | 1999-02-01 00:00:00 |
+--------+----------+-------------+--------+------+---------------------+
2 rows in set (0.00 sec)

** save a file directly into MySQL **
mysql> CREATE TABLE IF NOT EXISTS `Files` (
    ->   `ID` int(25) NOT NULL auto_increment,
    ->   `Title` varchar(250) NOT NULL default '',
    ->   `File_Name` varchar(250) NOT NULL default '',
    ->   `File_Type` varchar(15) NOT NULL default '',
    ->   `File_Size` varchar(45) NOT NULL default '',
    ->   `File_Content` longblob NOT NULL,
    ->   `File_Extension` varchar(10) NOT NULL default '',
    ->   PRIMARY KEY  (`ID`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.76 sec)

** insert a row into the Files table **
mysql> insert into Files set title = 'me', file_name = 'me.txt', file_type = 'text', file_size = '4k', file_content = '444555', file_extension = 'txt';
Query OK, 1 row affected (0.08 sec)

** update a column with new datetime data Format: YYYY-MM-DD HH:MM:SS **
mysql> update patient_item set item_time = '2012-8-1 15:21:11' where patient_item_id = 107;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

** select data based on time and id **
mysql> select * from cus_item where bp_time <='2012-08-02 08:00:00' and cus_id=5;
+-------------+--------+------+---------------------+-------------+
| cus_item_id | cus_id | bp   | bp_time             | doc_message |
+-------------+--------+------+---------------------+-------------+
|           1 |      5 |   89 | 1999-01-01 00:00:00 | NULL        |
|           2 |      5 |   69 | 1999-02-01 00:00:00 | NULL        |
+-------------+--------+------+---------------------+-------------+
2 rows in set (0.05 sec)


Change Primary Key Size

***change primary key size****
mysql> create table ref (ref_id int unsigned not null, something varchar(30), primary key(ref_id));
Query OK, 0 rows affected (0.21 sec)

mysql> describe ref;
+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| ref_id    | int(10) unsigned | NO   | PRI | NULL    |       |
| something | varchar(30)      | YES  |     | NULL    |       |
+-----------+------------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> alter table ref drop primary key , add column newcol int PRIMARY KEY  ;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe ref;
+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| ref_id    | int(10) unsigned | NO   |     | NULL    |       |
| something | varchar(30)      | YES  |     | NULL    |       |
| newcol    | int(11)          | NO   | PRI | NULL    |       |
+-----------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> alter table ref modify column ref_id int(20) unsigned;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table ref drop primary key;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe ref;
+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| ref_id    | int(20) unsigned | YES  |     | NULL    |       |
| something | varchar(30)      | YES  |     | NULL    |       |
| newcol    | int(11)          | NO   |     | NULL    |       |
+-----------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table ref add column ref_id int(20) unsigned primary key auto_incre
ment;
ERROR 1060 (42S21): Duplicate column name 'ref_id'
mysql> alter table ref modify column ref_id int(20) unsigned primary key auto_in
crement;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe ref;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| ref_id    | int(20) unsigned | NO   | PRI | NULL    | auto_increment |
| something | varchar(30)      | YES  |     | NULL    |                |
| newcol    | int(11)          | NO   |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Change MySQL root password

You can change the MySQL root password using mysqladmin command as shown below. Please note that there is no space between -p and currentpassword.

# mysqladmin -u root -pcurrentpassword password 'newpassword'


Copy MySQL Database from one server to another server

You create a database backup file at the local server
$ mysqldump -u user -p db-name > db-name.out

Using whatever method, copy the database backup file to the remote server

You restore database at remote server
$ mysql -u user -p db-name < db-name.out


SQL Like Opeartor

The Like Operatoe is able to do partial matching with the sql table.

Select * from profile where last_name like "%Mary%" or first_name LIKE "%Mary%"
select * from profile where last_name like '%M%';