MySQL DDL Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 41: Line 41:
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='some_database'"
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='some_database'"
</syntaxhighlight>
</syntaxhighlight>
If run in shell (mysql) and the database does not exist, the command has an exit code 1 and sends an error message at stderr.


==Create Database==
==Create Database==

Revision as of 05:59, 30 March 2021

Internal

Server Version

SELECT VERSION();

Users

Display Users

SELECT USER FROM mysql.user;

Create User

CREATE USER 'someuser'@'localhost' IDENTIFIED BY 'somepassword';

Grant to User All Privileges on a Database

GRANT ALL PRIVILEGES ON some_database.* TO 'someuser'@'localhost';
FLUSH PRIVILEGES;

Show Privileges

SHOW GRANTS FOR 'someuser'@'localhost';

Delete User

DROP USER 'someuser'@'localhost';

Database

Display Databases

SHOW DATABASES;

Check if a Database Exists

SHOW DATABASES LIKE 'some_database';
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='some_database'"

If run in shell (mysql) and the database does not exist, the command has an exit code 1 and sends an error message at stderr.

Create Database

CREATE DATABASE some_database;

Delete Database

DROP DATABASE some_database;