# Create Table 创建表

## 创建表

### CREATE TABLE table\_name (parm1, parm2, ...,parmN);

```sql
CREATE TABLE t_student (
	id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(20) NOT NULL,
	gender ENUM("男","女") DEFAULT "男",
	birthday DATE NOT NULL,
	tel CHAR(11)
) ;



```

我们我们创建了一个 名叫 student 的表格，里面有5个字段(也就是5列)，分别是

1. id， 无符号整型，主键约束。所谓主键就是用来唯一标志这一行的数据的一个变量，在一张表中，主键不允许有重复。AUTO\_INCREMENT,  默认起始值为1，以后依次增加1；
2. name, 字符串类型，最大长度为20，不允许为NULL
3. gender, 枚举，默认 "男”
4. birthday, 日期类型，不允许为NULL
5. tel, 字符串类型，固定长度为11，允许为NULL

我们接着往下创建两个表

```sql
CREATE TABLE t_dept(
	deptno INT UNSIGNED PRIMARY KEY,
	deptname VARCHAR(20) NOT NULL UNIQUE
);

CREATE TABLE t_emp(
	deptno INT UNSIGNED PRIMARY KEY,
	name VARCHAR(20) NOT NULL,
	FOREIGN KEY (deptno) REFERENCES t_dept (deptno)
);
	
```

特别的是 t\_dept 中的 deptname 使用了 **UNIQUE 约束, 表示值必须唯一，但是可以为NULL**

**外键宇约束 FOREIGN KEY （deptno）REFERENCES  t\_dept (deptno), 表示 当期表内的 deptno 与 外部表的 t\_dept内的 deptno产生关联。其中外部的字段必须为PRIMARY KEY。**

### 向表中插入数据

### INSERT INTO table\_name VALUES (value1,value2, ...,valueN);

```sql
INSERT INTO t_student VALUES(1,"张三","男","1995-06-01","12345678900" );



```

### INSERT INTO table\_name （field1,field2, ..., fieldN） VALUES (value1,value2, ...,valueN);

```sql
INSERT INTO t_student (name,gender,birthday,tel) VALUES("李四","男","1998-02-11","01234567890" );
```

## 查看表格结构

```sql
DESC student;
```

## 查看当前数据库内所有表

```sql
SHOW TABLES;
```

## 删除表

```sql
DROP TABLE student;
```


---

# Agent Instructions: 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:

```
GET https://chesterzhang666.gitbook.io/intro-mysql/operation-to-table/create-table-chuang-jian-biao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
