表的操作
?專欄內(nèi)容:
- postgresql內(nèi)核源碼分析
- 手寫數(shù)據(jù)庫toadb
- 并發(fā)編程
?開源貢獻(xiàn):
- toadb開源庫
個(gè)人主頁:我的主頁
管理社區(qū):開源數(shù)據(jù)庫
座右銘:天行健,君子以自強(qiáng)不息;地勢坤,君子以厚德載物.
系列文章
- 入門準(zhǔn)備
- postgrersql基礎(chǔ)架構(gòu)
- 快速使用
- 初始化集群
- 數(shù)據(jù)庫服務(wù)管理
- psql客戶端使用
- pgAdmin圖形化客戶端
- 數(shù)據(jù)庫的使用
- 創(chuàng)建數(shù)據(jù)庫
- 數(shù)據(jù)庫操作
- 表的使用
- 表的創(chuàng)建
- 表的操作
- 數(shù)據(jù)查詢
- 數(shù)據(jù)查詢
- 多表聯(lián)合查詢
前言
postgresql 數(shù)據(jù)庫是一款通用的關(guān)系型數(shù)據(jù),在開源數(shù)據(jù)庫中能與商業(yè)數(shù)據(jù)媲美,在業(yè)界也越來越流行。
因?yàn)槭情_源數(shù)據(jù)庫,不僅公開源碼,還有很多使用案例,好用的插件,所以它的慢慢變成了數(shù)據(jù)庫的先驅(qū)和標(biāo)準(zhǔn),通過postgresql可以很好從使用到原理,徹底搞懂;
如果是學(xué)習(xí)編程,也可以學(xué)到豐富的編程知識,數(shù)據(jù)結(jié)構(gòu),編程技巧,它里面還有很多精妙的架構(gòu)設(shè)計(jì),分層思想,可以靈活定制的思想。
本專欄主要介紹postgresql 入門使用,數(shù)據(jù)庫維護(hù)管理,通過這些使用來了解數(shù)據(jù)庫原理,慢慢了解postgresql是什么樣的數(shù)據(jù)庫,能做那些事情,以及如何做好服務(wù),最關(guān)鍵的是這些知識都是面試的必備項(xiàng)。
概述
前一篇分享了創(chuàng)建表的相關(guān)內(nèi)容,對于表也有很多屬性,如果這些屬性在創(chuàng)建時(shí)搞錯(cuò)了,那是否需要?jiǎng)h除表再來呢?
本文就從以下幾個(gè)方面,帶大家看看表的各種操作,在平時(shí)數(shù)據(jù)庫開發(fā)和維護(hù)時(shí),不需要再求助別人了。
- 查看表結(jié)構(gòu)
- 查詢數(shù)據(jù)
- 插入數(shù)據(jù)
- 刪除數(shù)據(jù)
- 修改表字段
- 添加和刪除表字段
- 重命名表
- 刪除表
- 查表的OID
查看表的結(jié)構(gòu)
當(dāng)我們使用表時(shí),也要知道表的定義結(jié)構(gòu),有那些字段,以及字段的類型,有沒有主鍵,外鍵等;
如果不是我們自己創(chuàng)建的表,可以在數(shù)據(jù)庫中進(jìn)行查詢表結(jié)構(gòu);
下面使用安裝時(shí)自帶的psql做為客戶端進(jìn)行操作;
首先登錄數(shù)據(jù)庫,我們看到前期已經(jīng)有兩張table存在;
[senllang@hatch bin]$ ./psql -d db_factory2 -U hr
WARNING: permission denied to set role "vp3"
psql (16beta1)
Type "help" for help.
db_factory2=> \d
List of relations
Schema | Name | Type | Owner
--------+---------------------------+----------+-------
public | company | table | hr
public | employees | table | hr
public | employees_employee_id_seq | sequence | hr
(3 rows)
db_factory2=>
接下來看一下這兩張表的結(jié)構(gòu),并驗(yàn)驗(yàn)證查詢和插入數(shù)據(jù)試一下;
db_factory2=> \d company
Table "public.company"
Column | Type | Collation | Nullable | Default
---------+---------------+-----------+----------+---------
id | integer | | not null |
name | text | | not null |
age | integer | | not null |
address | character(50) | | |
salary | real | | |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
可以看到company
這張表的信息通過表格的形式展示出來,第一列是字段史,第二列是類弄,后面是約束,還有默認(rèn)值;
可以看到有五個(gè)字段,id
字段是主鍵,主鍵也是一種索引,這里默認(rèn)采用btree索引類型,還有非空約束;address
字段的長度為50;
查詢數(shù)據(jù)
再來看一下表中是否有數(shù)據(jù),這里可以按字段查,也可以用*
來代替所有字段 ;
db_factory2=> select * from company;
id | name | age | address | salary
----+------+-----+---------+--------
(0 rows)
db_factory2=> select id from company ;
id
----
(0 rows)
表中還沒有數(shù)據(jù);
插入數(shù)據(jù)
插入數(shù)據(jù),我們使用標(biāo)準(zhǔn)SQL語句,如果每個(gè)字段都有值時(shí),可以省略字段名,解析時(shí)會(huì)根據(jù)值順序?qū)?yīng)到每個(gè)字段;
db_factory2=> insert into company (id,name,age,address,salary) values(1,'hongxing',8,'shangdong',4.8),(2,'dongfeng',20,'shenyang',10.22);
INSERT 0 2
db_factory2=> select * from company;
id | name | age | address | salary
----+----------+-----+----------------------------------------------------+--------
1 | hongxing | 8 | shangdong | 4.8
2 | dongfeng | 20 | shenyang | 10.22
(2 rows)
一次插入了兩條數(shù)據(jù),values
后面可以是多個(gè)值域,這樣就可以插入多條,但是值域中字段值數(shù)量必須一樣,否則解析器是沒有辦法對齊到表字段的;
修改字段
對已經(jīng)存在的表的字段進(jìn)行修改,可以對字段名稱,類型進(jìn)行變更;
修改類型
db_factory2=> alter table company alter column name type varchar(255);
ALTER TABLE
db_factory2=> \d company
Table "public.company"
Column | Type | Collation | Nullable | Default
---------+------------------------+-----------+----------+---------
id | integer | | not null |
name | character varying(255) | | not null |
age | integer | | not null |
address | character(50) | | |
salary | real | | |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
將name
字段的類型長度由原來無限制變更為255字符;
類型的修改需要注意,并不是所有類型都可以任意修改,修改后的類型必須兼容之前的類型,最好不存在數(shù)據(jù)丟失的問題;
修改字段名稱
表定義后,老板說這個(gè)名字起的不好,那只好修改一下,可以這樣操作;
db_factory2=> alter table company rename age TO founded;
ALTER TABLE
db_factory2=> \d company
Table "public.company"
Column | Type | Collation | Nullable | Default
---------+------------------------+-----------+----------+---------
id | integer | | not null |
name | character varying(255) | | not null |
founded | integer | | not null |
address | character(50) | | |
salary | real | | |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
添加和刪除字段
雖然上面把age
改成了founded
成立日期,但是類型卻怎么也修改不成功; 那這 。。。
db_factory2=> alter table company alter column founded type timestamp without time zone;
ERROR: column "founded" cannot be cast automatically to type timestamp without time zone
HINT: You might need to specify "USING founded::timestamp without time zone".
注意:在做下列操作之前需要對數(shù)據(jù)進(jìn)行備份,否則數(shù)據(jù)就會(huì)丟失;
遇到這種情況,當(dāng)在初期時(shí),還在測試階段,可以采用刪除字段,再添加的辦法;當(dāng)然這樣做之后,這列的數(shù)據(jù)都會(huì)丟失;
刪除字段
db_factory2=> alter table company drop column founded ;
ALTER TABLE
db_factory2=> \d company
Table "public.company"
Column | Type | Collation | Nullable | Default
---------+------------------------+-----------+----------+---------
id | integer | | not null |
name | character varying(255) | | not null |
address | character(50) | | |
salary | real | | |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
添加字段
之后可以按正確的字段名稱,類型進(jìn)行添加
db_factory2=> alter table company add column founded date;
ALTER TABLE
db_factory2=> \d company
Table "public.company"
Column | Type | Collation | Nullable | Default
---------+------------------------+-----------+----------+---------
id | integer | | not null |
name | character varying(255) | | not null |
address | character(50) | | |
salary | real | | |
founded | date | | |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
現(xiàn)在可以了,那么這一列的數(shù)據(jù)就需要重新錄入了,可以使用update語句批量錄入;
重命名表
如果發(fā)現(xiàn)表名含義不清,可以通過重命名的方式進(jìn)行修改;
db_factory2=> alter table company rename to tbl_company;
ALTER TABLE
這個(gè)操作對于數(shù)據(jù)庫內(nèi)部不會(huì)產(chǎn)生影響,因?yàn)閮?nèi)部是通過OID進(jìn)行引用;但是對于已經(jīng)運(yùn)行的業(yè)務(wù)系統(tǒng),需要修改SQL中的引用方式;
刪除表
刪除一張不再使用的表
db_factory2=> drop table employees ;
DROP TABLE
刪除成功的前提是,這張表沒有被別的數(shù)據(jù)庫對象引用(視圖,外鍵等),比如在這個(gè)表上創(chuàng)建了視圖,那么就先要?jiǎng)h除視圖,或者是采取級聯(lián)刪除的方式一起刪除;
查表的OID
數(shù)據(jù)庫內(nèi)部使用OID引用,OID是唯一標(biāo)識,在磁盤上存儲(chǔ)時(shí)也采用OID來命名,如何知道表的OID呢?
表相關(guān)信息記錄在pg_class這張系統(tǒng)表中;
db_factory2=> select oid,relname from pg_class where relname='tbl_company';
oid | relname
-------+-------------
16510 | tbl_company
(1 row)
總結(jié)
通過本文的分享,對于表的屬性的查詢,變更有了進(jìn)一步了解,數(shù)據(jù)字典中關(guān)于表的信息,如OID,存儲(chǔ)位置,是普通表還是臨時(shí)表等,都記錄在pg_class當(dāng)中,當(dāng)然它還記錄了很多有用的信息,有興趣的同學(xué)可以自己查詢試試。
結(jié)尾
非常感謝大家的支持,在瀏覽的同時(shí)別忘了留下您寶貴的評論,如果覺得值得鼓勵(lì),請點(diǎn)贊,收藏,我會(huì)更加努力!
作者郵箱:study@senllang.onaliyun.com
如有錯(cuò)誤或者疏漏歡迎指出,互相學(xué)習(xí)。文章來源:http://www.zghlxwxcb.cn/news/detail-713806.html
注:未經(jīng)同意,不得轉(zhuǎn)載!文章來源地址http://www.zghlxwxcb.cn/news/detail-713806.html
到了這里,關(guān)于【postgresql 基礎(chǔ)入門】表的操作,表結(jié)構(gòu)查看、修改字段類型、增加刪除字段、重命名表,對表的操作總是比別人棋高一著的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!