暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

AntDB 快速入门 / 一个实践例子

tocata 2024-08-16
54

实践例子

首先创建一个名为 t_art 的表,包含以下字段:

  • id(每篇文章的唯一标识符)
  • title(文章的标题)
  • content(文章的内容)
  • author(文章的作者)
  • date(文章的发布日期)

在 AntDB 中,可以使用以下 SQL 命令来创建这个表:

CREATE TABLE t_art (
    id SERIAL PRIMARY KEY,
    title VARCHAR(200),
    content TEXT,
    author VARCHAR(100),
    date DATE
);

这个 CREATE TABLE 命令创建了一个新的表,名为 t_art。这个表有五个字段:idtitlecontentauthor 和 dateid 字段是自动增长的,用于唯一标识每篇文章。

现在,假设要添加一篇文章。可以使用 INSERT INTO 命令,如下所示:

INSERT INTO t_art (title, content, author, date) 
VALUES ('My First Article', 'This is the content of my first article.', 'John Doe', '2023-07-14');

这个命令将向 t_art 表中插入一条新的记录。

如果想要更新这篇文章的内容,可以使用 UPDATE 命令,如下所示:

UPDATE t_art 
SET content = 'This is the updated content of my first article.' 
WHERE id = 1;

这个命令将更新 id 为 1 的文章的 content 字段。

如果想要查询特定的文章,可以使用 SELECT 命令,如下所示:

SELECT * FROM t_art WHERE id = 1;

这个命令将返回 id 为 1 的文章的所有信息。

最后,如果想要删除这篇文章,可以使用 DELETE 命令,如下所示:

SELECT * FROM t_art WHERE id = 1;

这个命令将删除 id 为 1 的文章。

运行效果

testdb=# CREATE TABLE t_art (
testdb(#     id SERIAL PRIMARY KEY,
testdb(#     title VARCHAR(200),
testdb(#     content TEXT,
testdb(#     author VARCHAR(100),
testdb(#     date DATE
testdb(# );
CREATE TABLE
testdb=# INSERT INTO t_art (title, content, author, date)
testdb-# VALUES ('My First Article', 'This is the content of my first article.', 'John Doe', '2023-07-14');
INSERT 0 1
testdb=# SELECT * FROM t_art;
 id |      title       |                 content                  |  author  |    date
----+------------------+------------------------------------------+----------+------------
  1 | My First Article | This is the content of my first article. | John Doe | 2023-07-14
(1 行记录)


testdb=# UPDATE t_art
testdb-# SET content = 'This is the updated content of my first article.'
testdb-# WHERE id = 1;
UPDATE 1
testdb=# SELECT * FROM t_art;
 id |      title       |                     content                      |  author  |    date
----+------------------+--------------------------------------------------+----------+------------
  1 | My First Article | This is the updated content of my first article. | John Doe | 2023-07-14
(1 行记录)


testdb=# SELECT * FROM t_art WHERE id = 1;
 id |      title       |                     content                      |  author  |    date
----+------------------+--------------------------------------------------+----------+------------
  1 | My First Article | This is the updated content of my first article. | John Doe | 2023-07-14
(1 行记录)


testdb=# DELETE FROM t_art WHERE id = 1;
DELETE 1
testdb=# SELECT * FROM t_art WHERE id = 1;
 id | title | content | author | date
----+-------+---------+--------+------
(0 行记录)


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论