Showing posts with label MySQL Tutorial. Show all posts
Showing posts with label MySQL Tutorial. Show all posts

Friday, May 29, 2009

DROP USER Database MySQL

The DROP USER statement removes one or more MySQL accounts. It removes privilege rows for the account from all grant tables. To use this statement, you must have the global CREATE USER privilege or the DELETE privilege for the mysql database. Each account is named using the same format as for the GRANT statement; for example, 'jeffrey'@'localhost'. If you specify only the user name part of the account name, a host name part of '%' is used. For additional information about specifying account names, see Section 12.5.1.3, “GRANT Syntax”.

With DROP USER, you can remove an account and its privileges as follows:

DROP USER user [, user] ...

example :

DROP USER ainun;

Important

DROP USER does not automatically close any open user sessions. Rather, in the event that a user with an open session is dropped, the statement does not take effect until that user's session is closed. Once the session is closed, the user is dropped, and that user's next attempt to log in will fail. This is by design.

DROP USER does not automatically delete or invalidate any database objects that the user created. This applies to tables, views, stored routines, triggers, and events.

How-To create a MySQL database and set privileges to a user

MySQL is a widely spread SQL database management system mainly used on LAMP (Linux/Apache/MySQL/PHP) projects.

In order to be able to use a database, one needs to create: a new database, give access permission to the database server to a database user and finally grant all right to that specific database to this user.

This tutorial will explain how to create a new database and give a user the appropriate grant permissions.

Here's an example of what I did recently to actually create a new database and user. Of course the database name, username, and password have been changed:
mysql -u root -p
(here I enter 'my_root_password' to get through the mysql prompt)

create database my_database;

GRANT ALL PRIVILEGES
ON my_database.*
TO 'my_user'@'localhost'
IDENTIFIED BY 'my_password'
WITH GRANT OPTION;
All of this is pretty much straight from the manual, but hopefully one more example helps.

Thursday, May 28, 2009

Understanding MySQL Table Types

MySQL supports various of table types or storage engines to allow you to optimize your database. The table types are available in MySQL are:

  • ISAM
  • MyISAM
  • InnoDB
  • BerkeleyDB (BDB)
  • MERGE
  • HEAP

The most important feature to make all the table types above distinction is transaction-safe or not. Only InnoDB and BDB tables are transaction safe and only MyISAM tables support full-text indexing and searching feature. MyISAM is also the default table type when you create table without declaring which storage engine to use. Here are some major features of each table types:

ISAM

ISAM had been deprecated and removed from version 5.x. All of it functionality entire replace by MyISAM. ISAM table has a hard size 4GB and is not portable.

MyISAM

MyISAM table type is default when you create table. MyISAM table work very fast but not transaction-safe. The size of MyISAM table depends on the operating system and the data file are portable from system to system. With MyISAM table type, you can have 64 keys per table and maximum key length of 1024 bytes.

InnoDB

Different from MyISAM table type, InnoDB table are transaction safe and supports row-level locking. Foreign keys are supported in InnoDB tables. The data file of InnoDB table can be stored in more than one file so the size of table depends on the disk space. Like the MyISAM table type, data file of InnoDB is portable from system to system. The disadvantage of InnoDB in comparison with MyISAM is it take more disk space.

BDB

BDB is similar to InnoDB in transaction safe. It supports page level locking but data file are not portable.

MERGE

Merge table type is added to treat multiple MyISAM tables as a single table so it remove the size limitation from MyISAM tables.

HEAP

Heap table is stored in memory so it is the fastest one. Because of storage mechanism, the data will be lost when the power failure and sometime it can cause the server run out of memory. Heap tables do not support columns with AUTO_INCREMENT, BLOB and TEXT characteristics.

Manage Database in MySQL

In this tutorial you will be able to Manage Database, Ok Let Get Started.

Creating Database

To create a database in MySQL, you use the CREATE DATABASE statement as follows:

CREATE DATABASE [IF NOT EXISTS] database_name;

CREATE DATABASE statement will create the database with the given name you specified. IF NOT EXISTS is an option part of the statement, this part prevents you from error if there is a database with the given name exists on the database server. In our tutorial, for example, to create classicmodels database, you just only apply CREATE DATABASE statement above as follows:

 CREATE DATABASE classicmodels; 

After executing the statement, the MySQL will returns you a message to indicate whether the execution are successful or not.

Showing Databases

SHOW DATABASE statement will show all databases in your server. You can use this statement to check the database you've created or to see all the databases' name on the server before you create a new database.

SHOW DATABASES;

On my server, the output is :

+--------------------+
| Database |
+--------------------+
| information_schema |
| classicmodels |
| mysql |
+--------------------+
8 rows in set (0.00 sec)

Selecting Database

To select a database which you will work with, you use this statement

USE database_name;

you can select our sample database by using USE statement as follows:

USE classicmodels;

From now you can query the tables' data and do whatever you want inside the selected database

Removing Database

Removing database means you delete the database, all the data and related objects inside the database permanently and cannot undo it. So it is very important to execute this query with cares. To remove the database you can use DROP DATABASE statement as follows :

DROP DATABASE [IF EXISTS] database_name;

Like CREATE DATABASE statement, IF EXIST part is an optional part to prevents you from removing database which is not existed. In order to practice with DROP DATABASE statement, you can create a temporary database, show the database on the database server, and drop it step by step as follows :

CREATE DATABASE IF NOT EXISTS temp_database;
SHOW DATABASES;
DROP DATABASE IF EXISTS temp_database;
Source from : mysqltutorial@org

Popular Posts