MySql之ALTER命令用法详细解读_MySQL
本文详细解读了MySql语法中Alter命令的用法,这是一个用法比较多的语法,而且功能还是很强大的。
USE learning;(自己要提前建好) CREATE TABLE student(id INT NOT NULL, name CHAR(10) NOT NULL, class INT NOT NULL, age INT );

一、删除,添加或修改表字段
删除表字段
如下命令使用了 ALTER 命令及 DROP 子句来删除以上创建表的 age字段:
ALTER TABLE student DROP age;

如果数据表中只剩余一个字段则无法使用DROP来删除字段。
添加表字段MySQL 中使用 ADD 子句来想数据表中添加列,如下实例在表 student 中添加age字段,并定义数据类型:
ALTER TABLE student ADD age INT NOT NULL;
SHOW COLUMNS FROM student来看表结构
如果你需要指定新增字段的位置,可以使用MySQL提供的关键字 FIRST (设定位第一列), AFTER 字段名(设定位于某个字段之后)。
尝试以下 ALTER TABLE 语句, 在执行成功后,使用 SHOW COLUMNS 查看表结构的变化:
ALTER TABLE student ADD sex CHAR(2) FIRST;

FIRST 和 AFTER 关键字只占用于 ADD 子句,所以如果你想重置数据表字段的位置就需要先使用 DROP 删除字段然后使用 ADD 来添加字段并设置位置。
ALTER TABLE student DROP sex; ALTER TABLE student ADD sex CHAR(2) AFTER age;
修改字段类型及名称
如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。
例如,把字段 name 的类型从 CHAR(10) 改为 CHAR(100),可以执行以下命令:
ALTER TABLE student MODIFY age CHAR(100);

使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段的类型及名称。尝试如下实例:
ALTER TABLE student CHANGE id stu_id BIGINT PRIMARY KEY;
ALTER TABLE 对 Null 值和默认值的影响
当你修改字段时,你可以指定是否包含只或者是否设置默认值。
以下实例,指定字段sex为 NOT NULL 且默认值为男 。
ALTER TABLE sutdent MODIFY sex CHAR(2) NOT NULL DEFAULT '男';

如果你不设置默认值,MySQL会自动设置该字段默认为 NULL。
你也可以使用 ALTER 命令及 DROP子句来删除字段的默认值,如下实例:
ALTER TABLE student ALTER sex DROP DEFAULT; SHOW COLUMNS FROM student;
修改数据表类型,可以使用 ALTER 命令及 TYPE 子句来完成。尝试以下实例,我们将表 student的类型修改为 MYISAM :
注意:查看数据表类型可以使用 SHOW CREATE TABLE 语句。
ALTER TABLE student ENGINE = MYISAM SHOW CREATE TABLE student;

二、修改表名
如果需要修改数据表的名称,可以在 ALTER TABLE 语句中使用 RENAME 子句来实现。
尝试以下实例将数据表 student 重命名为 student_1:
mysql> ALTER TABLE student RENAME TO student_1;
三、主键和索引修改
删除表中主键
ALTER TABLE student DROP PRIMARY KEY;
ALTER TABLE student ADD CONSTRAINT PK_STUDENT PRIMARY KEY (id,class);
添加索引
ALTER TABLE student ADD INDEX index_name (name);
查看索引
SHOW INDEX FROM student;
添加唯一限制条件索引
ALTER TABLE student ADD UNIQUE emp_name(age);
删除索引
ALTER TABLE student DROP INDEX index_name;


Hot 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot 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



The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

The cmd window prompts that telnet is not an internal or external command. This problem must have deeply troubled you. This problem does not appear because there is anything wrong with the user's operation. Users do not need to worry too much. All it takes is a few small steps. Operation settings can solve the problem of cmd window prompting telnet is not an internal or external command. Let’s take a look at the solution to the cmd window prompting telnet is not an internal or external command brought by the editor today. The cmd window prompts that telnet is not an internal or external command. Solution: 1. Open the computer's control panel. 2. Find programs and functions. 3. Find Turn Windows features on or off on the left. 4. Find “telnet client

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager

Step 1: Open PowerShell or Command Prompt on your Windows 11 or 10 system, go to the search box and type CMD or Powershell as per your choice. Here we use PowerShell. When it appears in the results, select "Run as administrator." This is because we need administrator user access to run commands to install any software on Windows. Step 2: Check Winget Availability Well, although all latest versions of Windows 10 and 11 come with Winget tool by default. But let's first check if it works. Type: winget In return you will see that it can be used with the command

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features
