MySQL : Create / Delete a MySQL database

View: 757    Dowload: 0   Comment: 0   Post by: naruto  
Author: none   Category: MySQL   Fields: Other

9 point/3 review File has been tested

In this case we have to connect to the MySQL server with the account has permission to create a database file ( usually use account "root " ) .

Introduction

1 . Creating a database

a. Create a database using mysqladmin library :

            In this case we have to connect to the MySQL server with the account has permission to create a database file ( usually use account "root " ) .

Example 1 : To create database files hscb ( personnel records ) we can do as follows :

root @ ubuntu :/ # mysqladmin - u root - p Create hscb

Enter password : ******

or :

root @ ubuntu :/ # mysql - u root - p

Enter password : ******

mysql > create databe hscb ;

b . Create a database using PHP script :

            In PHP , we use the function mysql_query ( ) to create / delete MySQL databases . This function needs two parameters and returns to one of two values ​​: True : OK - False : Failure . The syntax is :

bool mysql_query ( sql , connection ) ;

Among them :

Sql parameter ( mandatory ) : The SQL query to create / delete MySQL databases .

Connect Parameters (optional ) : If the command appears in the last connection opened by mysql_connect function ( ) will be used .

Example 2 : The following is a PHP script functions to create database HSCB :

<head>

<title> Create MySQL database < / title>

< / head>

<body>

< ? php

$ dbhost = ' localhost : 3036 ' ;

$ dbuser = ' root' ;

$ dbpass = ' a123456789z ' ;

/ / ------------------------------------------------ -------------

$ conn = mysql_connect ( $ dbhost , $ dbuser , $ dbpass ) ;

Else if ( ! $ Conn )

{

die ( ' connection failed ' . mysql_error ( ) ) ;

}

echo ' successfully connected <br /> ' ;

/ / ------------------------------------------------

$ sql = ' CREATE DATABASE HSCB ' ;

$ retval = mysql_query ( $ sql , $ conn ) ;

Else if ( ! $ Retval )

{

  die ( ' Could not create database : ' . mysql_error ( ) ) ;

}

echo " HSCB database was created \ n " ;

/ / ------------------------

mysql_close ( $ conn ) ;

? >

< / body>

< / html >

 

-------------------------------------------------- -----------

SQL commands to create a database is transmitted to the HSCB mysql_query function through the variable $ sql .

Values ​​obtained from the variable $ retval said database creation succeeded HSCB ( True ) or unsuccessful ( False ) .

We should also pay attention to the use of the variable $ conn argument to the function mysql_query ( ) .

In both examples above , the database files are created are stored in the default directory / var / lib / mysql .

2 . Delete database

a. Delete the database using mysqladmin library :

When we delete a database , all data will be lost .

In this case we have to connect to the MySQL server with the account has permission to delete the database file ( usually use account "root " ) .

Example 3 : To delete the database file hscb ( personnel records ) we can do as follows :

root @ ubuntu :/ # mysqladmin - u root - p drop hscb

Enter password : ******

or :

root @ ubuntu :/ # mysql - u root - p

Enter password : ******

mysql > drop databe hscb ;

b . Delete database using PHP script :

Example 4 : Using the function mysql_query ( ) to delete a database :

<html>

<head>

<title> Delete MySQL Database < / title>

< / head>

<body>

< ? php

$ dbhost = ' localhost : 3036 ' ;

$ dbuser = ' root' ;

$ dbpass = ' a123456789z ' ;

$ conn = mysql_connect ( $ dbhost , $ dbuser , $ dbpass ) ;

Else if ( ! $ Conn )

{

die ( ' connection failed ' . mysql_error ( ) ) ;

}

echo ' successfully connected <br /> ' ;

-------------------------------------------------- -----

$ sql = ' DROP DATABASE HSCB ' ;

$ retval = mysql_query ( $ sql , $ conn ) ;

Else if ( ! $ Retval )

{

die ( ' Could not delete the database : ' . mysql_error ( ) ) ;

}

echo " HSCB database has been deleted \ n " ;

-------------------------------------------------- -------

mysql_close ( $ conn ) ;

? >

< / body>

< / html >

 

----------------------------------

When we delete the library database mysqladmin lieuvoi will be reminded to be sure to delete or not . Meanwhile , the PHP script does not , so we really have to be careful in this case .

3 . Select / open ( use ) database

After connecting to the MySQL server , need to select the database that we need to manipulate on it . This is often referred to as open ( open ) database .

a. Open the database prompt :

            We can use SQL commands at the prompt use mysql > to open a database . Syntax : use <database name> ;

Example 5 :

root @ ubuntu :/ # mysql - u root - p

Enter password : ******

mysql > use hscb ;

Database changed

mysql >

Note: the database name , table name and the field name is case sensitive - lowercase , so we have access to the names used in SQL statements

b . Open the database using PHP script :

The function mysql_select_db ( ) of PHP is used to open a MySQL database . This function needs two parameters and returns one of two values ​​: True : OK - False : Failure . The syntax is :

bool mysql_select_db ( DB_NAME , connection ) ;

Among them :

Parameter db_name (required ) : The name of the MySQL database to open .

Connect Parameters (optional ) : If used , the last connection opened by mysql_connect ( ) will be used .

Example 6 :

<html>

<head>

Mo <title> MySQL database < / title>

< / head>

<body>

< ? php

$ dbhost = ' localhost : 3036 ' ;

$ dbuser = ' root' ;

$ dbpass = ' a123456789z ' ;

$ conn = mysql_connect ( $ dbhost , $ dbuser , $ dbpass ) ;

Else if ( ! $ Conn )

{

die ( ' connection failed ' . mysql_error ( ) ) ;

}

echo ' successfully connected ' ;

------------------------------------

mysql_select_db ( " HSCB " ) ;

------------------------------------

mysql_close ( $ conn ) ;

? >

< / body>

< / html >

 

-------------------------------------------

If we execute the command to open a database while a database is open , then PHP will automatically close the database is open it again .

MySQL : Create / Delete a MySQL database

MySQL : Create / Delete a MySQL database Posted on 24-03-2014  In this case we have to connect to the MySQL server with the account has permission to create a database file ( usually use account "root " ) . 3/10 757

Comment:

To comment you must be logged in members.

Files with category

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd