> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/computer-science/databases/mysql/modifying-tables.md).

# Modifying Tables

## Modifying Column

Modify the data type of a column:

```sql
ALTER TABLE tb1C MODIFY name VARCHAR(100);
```

Add a new column to the end:

```sql
ALTER TABLE tb1C ADD birth DATETIME;
```

Add a new column to the beginning:

```sql
ALTER TABLE tb1C ADD birth DATETIME FIRST;
```

Add a new column to a specific location:

```sql
ALTER TABLE tb1C ADD birth DATETIME AFTER empid;
```

Modify the ordering of columns:

```sql
ALTER TABLE tb1C MODIFY birth DATETIME FIRST;
```

Change the name and date type of a column:

```sql
ALTER TABLE tb1C CHANGE birth birthday DATE;
```

Drop a column:

```sql
ALTER TABLE tb1C DROP birthday;
```

## Primary Key and Unique Key

Set primary key (no duplication + no NULL):

```sql
CREATE TABLE t_pk (a INT PRIMARY KEY, b VARCHAR(10));
```

Set unique key (no duplication):

```sql
CREATE TABLE t_uniq (a INT UNIQUE, b VARCHAR(10));
```

## Auto-Increment and Default Value

Set auto-increment:

```sql
CREATE TABLE t_series (a INT AUTO_INCREMENT PRIMARY KEY, b VARCHAR(10));
```

Reset auto-increment index (deletion won't set the counter to 1):

```sql
ALTER TABLE t_series AUTO_INCREMENT=1;
```

Set default value for a column:

```sql
ALTER TABLE tb1G MODIFY name VARCHAR(10) DEFAULT 'nobody';
```

## Index

Create index:

```sql
CREATE INDEX my_ind ON tb1G (empid);
```

Show index (in a nice format):

```sql
SHOW INDEX FROM tb1G\G
```

Drop index:

```sql
DROP INDEX my_ind ON tb1G;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/computer-science/databases/mysql/modifying-tables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
