What is the SQL keyword that indicates modifying a database object?
The SQL keyword that indicates modifying a database object is: ALTER. In the database, you can use the "ALTER TABLE" statement to change the structure of the original table, and add, delete, or modify columns in the existing table.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
The SQL keyword indicating modifying a database object is: alte.
SQL ALTER DATABASE statement
In MySQL, you can use ALTER DATABASE to modify Relevant parameters of the database that has been created or exists. The syntax format for modifying the database is:
ALTER DATABASE [数据库名] { [ DEFAULT ] CHARACTER SET <字符集名> | [ DEFAULT ] COLLATE <校对规则名>}
The syntax description is as follows:
ALTER DATABASE is used to change the global characteristics of the database.
Using ALTER DATABASE requires database ALTER permissions.
The database name can be ignored, and the statement corresponds to the default database.
The CHARACTER SET clause is used to change the default database character set.
Example:
View the execution results of the definition statement of the test_db database
mysql> SHOW CREATE DATABASE test_db; +----------+--------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------+ | test_db | CREATE DATABASE `test_db` /*!40100 DEFAULT CHARACTER SET utf8 */| +----------+--------------------------------------------------------+ 1 row in set (0.05 sec)
Use the command line tool to modify the specified character set of the database test_db to gb2312 , the default proofreading rule is modified to gb2312_unicode_ci
mysql> ALTER DATABASE test_db -> DEFAULT CHARACTER SET gb2312 -> DEFAULT COLLATE gb2312_chinese_ci; mysql> SHOW CREATE DATABASE test_db; +----------+--------------------------------------------------------+ | Database | ALTER Database | +----------+--------------------------------------------------------+ | test_db | ALTER DATABASE `test_db` /*!40100 DEFAULT CHARACTER SET gb2312 */| +----------+--------------------------------------------------------+ 1 row in set (0.00 sec)
(Recommended tutorial: mysql video tutorial)
SQL ALTER TABLE statement
SQL ALTER TABLE
statement is used to add, delete, or modify columns in an existing table. Use the ALTER TABLE
statement to change the structure of the original table, such as adding or deleting columns, changing the original column type, renaming columns or tables, etc.
The syntax format is as follows:
ALTER TABLE <表名> [修改选项]
The syntax format for modifying options is as follows:
{ ADD COLUMN <列名> <类型> | CHANGE COLUMN <旧列名> <新列名> <新列类型> | ALTER COLUMN <列名> { SET DEFAULT <默认值> | DROP DEFAULT } | MODIFY COLUMN <列名> <类型> | DROP COLUMN <列名> | RENAME TO <新表名> | CHARACTER SET <字符集名> | COLLATE <校对规则名> }
Modify table name
MySQL via ALTER TABLE statement is used to modify the table name. The syntax rules are as follows:
ALTER TABLE <旧表名> RENAME [TO] <新表名>;
Among them, TO is an optional parameter, and whether it is used or not does not affect the result.
Modify the table character set
MySQL uses the ALTER TABLE statement to modify the table character set. The syntax rules are as follows:
ALTER TABLE 表名 [DEFAULT] CHARACTER SET <字符集名> [DEFAULT] COLLATE <校对规则名>;
Among them, DEFAULT is Optional parameters, whether used or not does not affect the results.
Modify field names
The syntax rules for modifying table field names in MySQL are as follows:
ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>;
Among them:
-
Old field name: refers to the field name before modification;
New field name: refers to the modified field name;
New Data type: refers to the modified data type. If you do not need to modify the data type of the field, you can set the new data type to be the same as the original one, but the data type cannot be empty.
Modify field data type
Modifying the data type of a field is to convert the data type of the field into another data type. The syntax rules for modifying field data types in MySQL are as follows:
ALTER TABLE <表名> MODIFY <字段名> <数据类型>
Among them:
Table name: refers to the name of the table where the field whose data type is to be modified is located;
Field name: refers to the field that needs to be modified;
Data type: refers to the new data type of the modified field.
Delete a field
Deleting a field is to remove a field in the data table from the table. The syntax format is as follows:
ALTER TABLE <表名> DROP <字段名>;
Among them, "field name" refers to the name of the field that needs to be deleted from the table.
Add fields
1. Add fields at the end
A complete field includes field name, data type and constraints. The syntax format for adding fields in MySQL is as follows:
ALTER TABLE <表名> ADD <新字段名><数据类型>[约束条件];
The syntax format is explained as follows:
is the name of the data table;
is the name of the field to be added; is the field that can store data Data type;
[Constraints] is optional and is used to constrain the added fields.
This syntax format adds a new field at the last position of the table (after the last column) by default.
2. Add a field at the beginning
MySQL adds a new field at the last position of the table by default. If you want to add a new field at the beginning (in front of the first column), you can use the FIRST key word, the syntax format is as follows:
ALTER TABLE <表名> ADD <新字段名> <数据类型> [约束条件] FIRST;
Copy after loginFIRST keyword is generally placed at the end of the statement.
3. Add fields in the middle position
In addition to allowing fields to be added at the beginning and end of the table, MySQL also allows fields to be added in the middle position (after the specified field). At this time You need to use the AFTER keyword, the syntax format is as follows:
ALTER TABLE <表名> ADD <新字段名> <数据类型> [约束条件] AFTER <已经存在的字段名>;
Copy after loginThe function of AFTER is to add a new field after an existing field.
Note that you can only add a new field after an existing field, but not in front of it.
For more knowledge about computer programming, please visit: Programming Video! !
The above is the detailed content of What is the SQL keyword that indicates modifying a database object?. For more information, please follow other related articles on the PHP Chinese website!
Statement of this WebsiteThe 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.cnHot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution3 weeks ago By DDDWhat's New in Windows 11 KB5054979 & How to Fix Update Issues2 weeks ago By DDDWhere to find the Crane Control Keycard in Atomfall3 weeks ago By DDDAssassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation1 months ago By DDDRoblox: Dead Rails - How To Complete Every Challenge3 weeks ago By DDDHot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
CakePHP Tutorial1387
52
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM
Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps
Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM
How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())
How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM
To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM
Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.
How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM
Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM
Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.
PHP connections to different databases: MySQL, PostgreSQL, Oracle and more Jun 01, 2024 pm 03:02 PM
PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.
How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM
JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.