MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The ALTER DATABASE
statement in MySQL is used to modify the options of an existing database. For instance, you can use it to change the character set and collation of a database.
Prerequisites:
Tutorial:
To start the mysql
command-line client, open your terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
The basic syntax for using the ALTER DATABASE
statement is as follows:
ALTER DATABASE database_name CHARACTER SET = charset_name COLLATE = collation_name;
Replace database_name
with the name of your database, charset_name
with the name of your character set, and collation_name
with the name of your collation.
For example, to change the character set and collation of a database named mydb
to utf8mb4
and utf8mb4_unicode_ci
, you would use:
ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
This will change the default character set and collation of the mydb
database to utf8mb4
and utf8mb4_unicode_ci
, respectively. Note that this doesn't change the character set and collation of the tables and columns inside the database; you would need to use the ALTER TABLE
statement to do that.
EXIT;
By using the ALTER DATABASE
statement in MySQL, you can easily modify the options of an existing database. This can be useful for changing the character set and collation of a database to support different languages and sort orders, among other things.
How to Modify a MySQL Database with ALTER DATABASE:
ALTER DATABASE
statement is used to modify various aspects of a MySQL database.ALTER DATABASE database_name [OPTIONS];
Changing Database Options with ALTER DATABASE in MySQL:
ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Adding Character Set and Collation to a MySQL Database:
ALTER DATABASE mydb CHARACTER SET utf8 COLLATE utf8_general_ci;
Modifying Database Attributes with ALTER DATABASE in MySQL:
ALTER DATABASE
.ALTER DATABASE mydb DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
MySQL ALTER DATABASE Example:
ALTER DATABASE
.ALTER DATABASE mydb CHARACTER SET utf8 COLLATE utf8_general_ci;
Renaming a MySQL Database with ALTER DATABASE:
ALTER DATABASE old_db_name RENAME TO new_db_name;
Altering Default Storage Engine for a MySQL Database:
ALTER DATABASE mydb DEFAULT STORAGE ENGINE = InnoDB;
Setting Default Privileges with ALTER DATABASE in MySQL:
ALTER DATABASE mydb DEFAULT PRIVILEGES ON *.* TO 'new_user'@'localhost';
Adjusting MySQL Database Character Set and Collation:
ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Adding or Removing Database Options with ALTER DATABASE:
ALTER DATABASE mydb COMMENT = 'New comment';
MySQL ALTER DATABASE and Time Zone Considerations:
ALTER DATABASE mydb TIME_ZONE = '+00:00';