Table of Contents
一、删除,添加或修改表字段
二、修改表名
三、主键和索引修改
Home Database Mysql Tutorial MySql之ALTER命令用法详细解读_MySQL

MySql之ALTER命令用法详细解读_MySQL

Jun 01, 2016 pm 12:59 PM
Order

本文详细解读了MySql语法中Alter命令的用法,这是一个用法比较多的语法,而且功能还是很强大的。

USE learning;(自己要提前建好)
CREATE TABLE student(id INT NOT NULL,
name CHAR(10) NOT NULL,
class INT NOT NULL,
age INT
);
Copy after login
来看看新建好的表
\

一、删除,添加或修改表字段

删除表字段


如下命令使用了 ALTER 命令及 DROP 子句来删除以上创建表的 age字段:

ALTER TABLE student  DROP age;
Copy after login
来看看结果:
\

如果数据表中只剩余一个字段则无法使用DROP来删除字段。

添加表字段

MySQL 中使用 ADD 子句来想数据表中添加列,如下实例在表 student 中添加age字段,并定义数据类型:

ALTER TABLE student  ADD age INT NOT NULL;
Copy after login
执行以上命令后,i 字段会自动添加到数据表字段的末尾。

SHOW COLUMNS FROM student来看表结构

\
如果你需要指定新增字段的位置,可以使用MySQL提供的关键字 FIRST (设定位第一列), AFTER 字段名(设定位于某个字段之后)。
尝试以下 ALTER TABLE 语句, 在执行成功后,使用 SHOW COLUMNS 查看表结构的变化:

ALTER TABLE student  ADD sex CHAR(2) FIRST;
Copy after login

\
FIRST 和 AFTER 关键字只占用于 ADD 子句,所以如果你想重置数据表字段的位置就需要先使用 DROP 删除字段然后使用 ADD 来添加字段并设置位置。
ALTER TABLE student  DROP sex;
ALTER TABLE student  ADD sex CHAR(2) AFTER age;
Copy after login

\

修改表字段

修改字段类型及名称
如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。
例如,把字段 name 的类型从 CHAR(10) 改为 CHAR(100),可以执行以下命令:

ALTER TABLE student  MODIFY age CHAR(100);
Copy after login
\

使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段的类型及名称。尝试如下实例:
ALTER TABLE student CHANGE id  stu_id BIGINT PRIMARY KEY;
Copy after login

\

ALTER TABLE 对 Null 值和默认值的影响
当你修改字段时,你可以指定是否包含只或者是否设置默认值。
以下实例,指定字段sex为 NOT NULL 且默认值为男 。

ALTER TABLE sutdent  MODIFY sex  CHAR(2)  NOT NULL DEFAULT '男';
Copy after login
\
如果你不设置默认值,MySQL会自动设置该字段默认为 NULL。
你也可以使用 ALTER 命令及 DROP子句来删除字段的默认值,如下实例:
ALTER TABLE student ALTER sex DROP DEFAULT;
 SHOW COLUMNS FROM student;
Copy after login

\

修改数据表类型,可以使用 ALTER 命令及 TYPE 子句来完成。尝试以下实例,我们将表 student的类型修改为 MYISAM :
注意:查看数据表类型可以使用 SHOW CREATE TABLE 语句。

ALTER TABLE student ENGINE = MYISAM
SHOW CREATE TABLE student;
Copy after login
\

二、修改表名

如果需要修改数据表的名称,可以在 ALTER TABLE 语句中使用 RENAME 子句来实现。
尝试以下实例将数据表 student 重命名为 student_1:

mysql> ALTER TABLE student RENAME TO student_1;

\

三、主键和索引修改

删除表中主键

ALTER TABLE student  DROP PRIMARY KEY;
Copy after login

\

添加主键

ALTER TABLE student   ADD CONSTRAINT PK_STUDENT  PRIMARY KEY (id,class);
Copy after login

\

添加索引

ALTER TABLE student ADD INDEX index_name (name);
Copy after login

查看索引

SHOW INDEX FROM student;
Copy after login

\

添加唯一限制条件索引

ALTER TABLE student  ADD UNIQUE emp_name(age);
Copy after login

 

\

删除索引

ALTER TABLE student DROP INDEX index_name;

\

 

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run SUDO commands in Windows 11/10 How to run SUDO commands in Windows 11/10 Mar 09, 2024 am 09:50 AM

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

How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 Feb 29, 2024 pm 04:34 PM

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

cmdtelnet command is not recognized as an internal or external command cmdtelnet command is not recognized as an internal or external command Jan 03, 2024 am 08:05 AM

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

Super practical! Sar commands that will make you a Linux master Super practical! Sar commands that will make you a Linux master Mar 01, 2024 am 08:01 AM

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

Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Feb 29, 2024 pm 05:52 PM

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? What is the correct way to restart a service in Linux? Mar 15, 2024 am 09:09 AM

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

Install VMware Workstation on Windows 11 with just one simple command Install VMware Workstation on Windows 11 with just one simple command Sep 12, 2023 pm 08:33 PM

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

Artifact in Linux: Principles and Applications of eventfd Artifact in Linux: Principles and Applications of eventfd Feb 13, 2024 pm 08:30 PM

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

See all articles