tft每日頭條

 > 科技

 > 數據庫與表的創建和使用重點

數據庫與表的創建和使用重點

科技 更新时间:2024-07-24 05:08:45

數據庫與表的創建和使用重點(OushuDB基本用法創建數據庫和表)1

本節通過使用OushuDB的命令行工具psql來說明如何創建基本數據庫對象:database和table。因為OushuDB和PostgreSQL兼容,所以使用OushuDB的方式和使用PostgresSQL的方式基本相同,如果OushuDB的文檔有些地方說明不清楚的話,用戶也可以通過查閱PostgresSQL的幫助文檔來了解更多關于OushuDB的信息。

下面這條命令使用psql連接OushuDB缺省安裝的數據庫postgres,然後創建一個新的數據庫test,并在新的數據庫中創建一個表foo。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

changlei:build ChangLei$ psql -d postgres

psql (8.2.15)

Type "help" for help.

postgres=# create database test; # 創建數據庫test

CREATE DATABASE

postgres=# \c test # 連接進入test數據庫

You are now connected to database "test" as user "ChangLei".

test=# create table foo(id int, name varchar); # 創建表foo

CREATE TABLE

test=# \d # 顯示當前數據庫test中所有表

List of relations

Schema | Name | Type | Owner | Storage

-------- ------ ------- ---------- -------------

public | foo | table | ChangLei | append only

(1 row)

test=# insert into foo values(1, 'hawq'),(2, 'hdfs');

INSERT 0 2

test=# select * from foo; # 從表foo中選擇數據

id | name

---- ------

1 | hawq

2 | hdfs

(2 rows)

如果想删除表或者數據庫的話可以使用drop語句。

test=# drop table foo;

DROP TABLE

test=# \d

No relations found.

test=# drop database test; # 因為現在在test數據庫中,所以不能删除

ERROR: cannot drop the currently open database

test=# \c postgres # 首先連接到postgres數據庫,然後删除test數據庫

You are now connected to database "postgres" as user "ChangLei".

postgres=# drop database test;

DROP DATABASE

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved