建表建立索引的sql语句
时间: 2024-05-21 15:09:15 浏览: 13
建表的 SQL 语句格式如下:
```sql
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
table_constraints
);
```
其中 `table_name` 为表名,`column1`、`column2` 等为列名,`datatype` 为数据类型,`constraint` 为约束条件,`table_constraints` 为表约束。
例如,创建一个名为 `users` 的用户表,包含 `id`、`name`、`age` 和 `email` 四个字段,其中 `id` 为主键,可以使用如下 SQL 语句:
```sql
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
email VARCHAR(100) UNIQUE
);
```
建立索引的 SQL 语句格式如下:
```sql
CREATE [UNIQUE] INDEX index_name ON table_name (column1, column2, ...);
```
其中 `index_name` 为索引名,`table_name` 为表名,`(column1, column2, ...)` 为需要建立索引的列名。如果需要建立唯一性索引,则在 `CREATE` 后加上关键字 `UNIQUE`。
例如,在名为 `users` 的用户表的 `name` 和 `email` 字段上分别创建一个非唯一性和一个唯一性索引,可以使用如下 SQL 语句:
```sql
CREATE INDEX idx_name ON users (name);
CREATE UNIQUE INDEX idx_email ON users (email);
```
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)