Hot File

MySQL : Create / Delete a MySQL database

View: 604    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 604

Comment:

To comment you must be logged in members.

Files with category

  • SQL Query to Find Nth Highest Salary of Employee with java

    View: 196    Download: 0   Comment: 0   Author: none  

    SQL Query to Find Nth Highest Salary of Employee with java

    Category: MySQL
    Fields: Other

    0/0 review
    This is a very common interview question if you are going for the role at junior level. Most of the people learn it character by character, but few understand how this query works.

  • World OOP With PHP and MySQL

    View: 956    Download: 0   Comment: 0   Author: none  

    World OOP With PHP and MySQL

    Category: MySQL
    Fields: Other

    0.32142857142857/14 review
    Setting up the skeleton of our class is fairly simple once we figure out exactly what we need.

  • How to Build a Newsletter System With PHP and MySQL

    View: 1183    Download: 0   Comment: 0   Author: none  

    How to Build a Newsletter System With PHP and MySQL

    Category: MySQL
    Fields: Other

    1.125/4 review
    When starting any project, I like to layout the folders before I start coding, so lets do that now.

  • Top 20+ MySQL Best Practices that can not be ignored

    View: 893    Download: 0   Comment: 0   Author: none  

    Top 20+ MySQL Best Practices that can not be ignored

    Category: MySQL
    Fields: Other

    1.6666666666667/3 review
    Database operations often tend to be the main bottleneck for most web applications today.

  • Create a PHP/MySQL Powered Forum from Scratch

    View: 1040    Download: 0   Comment: 0   Author: none  

    Create a PHP/MySQL Powered Forum from Scratch

    Category: MySQL
    Fields: Other

    1.5/3 review
    In this tutorial, we're going to build a PHP/MySQL powered forum from scratch.

  • Triggers Introduction to MySQL

    View: 256    Download: 0   Comment: 0   Author: none  

    Triggers Introduction to MySQL

    Category: MySQL
    Fields: Other

    1.6666666666667/3 review
    I would guess, even armed with this knowledge, that a good many of you are not taking advantage of triggers with MySQL.

  • How to Stored Procedures in MySQL 5

    View: 247    Download: 0   Comment: 0   Author: none  

    How to Stored Procedures in MySQL 5

    Category: MySQL
    Fields: Other

    2.25/2 review
    MySQL 5 introduced a plethora of new features - stored procedures being one of the most significant. In this tutorial, we will focus on what they are, and how they can make your life easier.

  • The strength of the PDO and. MySQLi: Which Should You Use?

    View: 267    Download: 0   Comment: 0   Author: none  

    The strength of the PDO and. MySQLi: Which Should You Use?

    Category: MySQL
    Fields: Other

    1.5/3 review
    When accessing a database in PHP, we have two choices: MySQLi and PDO. So what should you know before choosing one?

 
Newsletter Email

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