diff --git a/src/.vitepress/sidebars/sqlite.yaml b/src/.vitepress/sidebars/sqlite.yaml index a851c9d..0c5687c 100644 --- a/src/.vitepress/sidebars/sqlite.yaml +++ b/src/.vitepress/sidebars/sqlite.yaml @@ -22,4 +22,23 @@ - text: 创建数据库 link: /database/sqlite/core-concepts/database-operations#创建数据库 - text: 检查数据库 - link: /database/sqlite/core-concepts/database-operations#检查数据库 \ No newline at end of file + link: /database/sqlite/core-concepts/database-operations#检查数据库 + - text: 数据表操作 + link: /database/sqlite/core-concepts/table-operations + items: + - text: 创建表 + link: /database/sqlite/core-concepts/table-operations#创建表 + - text: 查看表 + link: /database/sqlite/core-concepts/table-operations#查看表 + - text: 修改表 + link: /database/sqlite/core-concepts/table-operations#修改表 + items: + - text: 添加列 + link: /database/sqlite/core-concepts/table-operations#添加列 + - text: 重命名列 + link: /database/sqlite/core-concepts/table-operations#重命名列 + - text: 删除列 + link: /database/sqlite/core-concepts/table-operations#删除列 + - text: 删除表 + link: /database/sqlite/core-concepts/table-operations#删除表 + \ No newline at end of file diff --git a/src/database/sqlite/core-concepts/table-operations.md b/src/database/sqlite/core-concepts/table-operations.md new file mode 100644 index 0000000..70c258b --- /dev/null +++ b/src/database/sqlite/core-concepts/table-operations.md @@ -0,0 +1,104 @@ +# 数据表操作 + +## 创建表 + +::: code-group + +```sql [语法] +create table database_name.table_name ( + column1 datatype primary key(one or more columns), + column2 datatype, + ... + columnN datatype +) +``` + +```sql [实例] +create table company ( + id integer primary key not null, + username text not null, + age integer not null, + salary real +) +``` + +::: + +## 查看表 + +使用`.tables`命令查看数据库中的所有表。 + +```bash +sqlite> .tables +company users +``` + +使用`.schema`命令查看表的结构。 + +```bash +sqlite> .schema company +CREATE TABLE company ( + id integer primary key not null, + username text not null, + age integer not null, + salary real +); +``` + +## 修改表 + +### 添加列 + +::: code-group + +```sql [语法] +alter table database_name.table_name add column_name datatype; +``` + +```sql [实例] +alter table company add email text +``` + +::: + +### 重命名列 + +::: code-group + +```sql [语法] +alter table database_name.table_name rename column_name to new_column_name; +``` + +```sql [实例] +alter table company rename username to name; +``` + +::: + +### 删除列 + +::: code-group + +```sql [语法] +alter table database_name.table_name drop column column_name; +``` + +```sql [实例] +alter table company drop column email; +``` + +::: + +## 删除表 + +::: code-group + +```sql [语法] +drop table database_name.table_name +``` + +```sql [实例] +drop table company; +``` + +:::