search
HomeDatabaseMysql TutorialMySQL Terminal: Alter table, charset and delete

MySQL Terminal: Alter table, charset and delete

ALTER TABLE MODIFY

The ALTER TABLE command with the MODIFY clause in MySQL is used to change the definition of an existing column in a table. Here’s the command structure:

ALTER TABLE table_name MODIFY column_name data_type [options];
  • table_name: the name of the table where the column is being modified.
  • column_name: the name of the column you want to modify.
  • data_type: the new data type for the column.
  • [options]: any other optional specifications, such as NOT NULL, DEFAULT value, etc.

Example

Suppose you have an age column in the users table of type INT, and you want to change it to INT(3) with a default value of 18. The command would be:

ALTER TABLE users MODIFY age INT(3) DEFAULT 18;

This command changes the data type of the age column to INT(3) and sets the default value to 18.

ALTER TABLE CHANGE

The ALTER TABLE command with the CHANGE clause in MySQL is used to rename a column and change its data type or attributes. Here’s the structure of the command:

ALTER TABLE table_name CHANGE old_column_name new_column_name data_type [options];
  • table_name: the name of the table where the column exists.
  • old_column_name: the current name of the column you want to modify.
  • new_column_name: the new name for the column.
  • data_type: the new data type for the column.
  • [options]: any additional specifications, such as NOT NULL, DEFAULT value, etc.

Example

Suppose you have a column age in the users table with type INT, and you want to rename it to current_age and change it to INT(3) with a default value of 18. The command would look like this:

ALTER TABLE users CHANGE age current_age INT(3) DEFAULT 18;

This command renames the age column to current_age, changes its data type to INT(3), and sets the default value to 18.

ALTER TABLE RENAME TO

The ALTER TABLE command with the RENAME TO clause in MySQL is used to rename a table. Here’s the syntax:

ALTER TABLE old_table_name RENAME TO new_table_name;
  • old_table_name: the current name of the table you want to rename.
  • new_table_name: the new name for the table.

Example

Suppose you have a table named users and you want to rename it to customers. The command would be:

ALTER TABLE users RENAME TO customers;

This command renames the table users to customers.

ALTER TABLE CONVERT TO CHARACTER SET

The ALTER TABLE ... CONVERT TO CHARACTER SET command in MySQL is used to change the character set and collation of an entire table. Here’s the syntax:

ALTER TABLE table_name MODIFY column_name data_type [options];
  • table_name: the name of the table whose character set and collation you want to change.
  • charset_name: the new character set for the table, such as utf8mb4.
  • [COLLATE collation_name]: an optional parameter to specify the collation (e.g., utf8mb4_unicode_ci). If omitted, the default collation for the specified character set will be used.

Example

If you want to change the character set of the customers table to utf8mb4 with its default collation, you would use:

ALTER TABLE users MODIFY age INT(3) DEFAULT 18;

Or, if you want to specify a specific collation, like utf8mb4_unicode_ci, you would use:

ALTER TABLE table_name CHANGE old_column_name new_column_name data_type [options];

This command updates all text columns in the table to the specified character set and collation.

DROP TABLE

The DROP TABLE command in SQL is used to permanently delete a table and all of its data from the database. Here’s the syntax:

ALTER TABLE users CHANGE age current_age INT(3) DEFAULT 18;
  • table_name: the name of the table you want to delete.

Example

If you want to delete a table named customers, the command would be:

ALTER TABLE old_table_name RENAME TO new_table_name;

Warning: Using DROP TABLE will permanently remove the table and all of its data. This action cannot be undone, so make sure to back up any important data before executing this command.

The above is the detailed content of MySQL Terminal: Alter table, charset and delete. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),