PostgreSQL----Inherit tables Inherits parent table

Source: Internet
Author: User
Tags modifiers postgresql

A new table created with inherits inherits one or more parent tables, the child table inherits only the table structure of the parent table and the not null,default,check three constraints, the primary key, the foreign key and the unique key, and the index are not inherited, so modify the structure of the parent table (additions and deletions), not NULL, Default and check constraints are automatically modified with the schedule.

Example 1.

Create TableTbl_inherits_parent (Aint  not NULL, bvarchar( +) not NULL default 'Got u', Cint Check(c> 0), D date not NULL); test=#Alter TableTbl_inherits_parentAdd constraintPk_tbl_inherits_parent_aPrimary Key(a);ALTER TABLETest=#Alter TableTbl_inherits_parentAdd constraintUk_tbl_inherits_parent_b_dUnique(b,d);ALTER TABLETest=#Create TableTbl_inherits_partition () inherits (Tbl_inherits_parent);CREATE TABLETest=# \d Tbl_inherits_partitionTable" Public. Tbl_inherits_partition "Column |Type|Modifiers--------+-----------------------+---------------------------------------------A| integer               |  not NULLb| character varying( +)|  not NULL default 'Got u'::character varyingC| integer               |D|Date|  not NULLCheckconstraints: "Tbl_inherits_parent_c_check"CHECK(c> 0)inherits:tbl_inherits_parent 

Example 2.

Test=#Alter TableTbl_inherits_parentAdd columnEint  not NULL default 0;ALTER TABLETest=#Alter TableTbl_inherits_parentAlter columnBSet default 'Try Me';ALTER TABLETest=# \d Tbl_inherits_partitionTable" Public. Tbl_inherits_partition "Column |Type|Modifiers--------+-----------------------+----------------------------------------------A| integer               |  not NULLb| character varying( +)|  not NULL default 'Try Me'::character varyingC| integer               |D|Date|  not NULLe| integer               |  not NULL default 0Checkconstraints: "Tbl_inherits_parent_c_check"CHECK(c> 0)inherits:tbl_inherits_parent 

Example 3.

In addition to inheriting the parent table, you can add your own fields when creating child tables

Test=#Create TableTbl_inherits_partition1 (fint) inherits (Tbl_inherits_parent);CREATE TABLETest=# \d Tbl_inherits_partition1Table" Public. Tbl_inherits_partition1 "Column |Type|Modifiers--------+-----------------------+----------------------------------------------A| integer               |  not NULLb| character varying( +)|  not NULL default 'Try Me'::character varyingC| integer               |D|Date|  not NULLe| integer               |  not NULL default 0F| integer               | Checkconstraints: "Tbl_inherits_parent_c_check"CHECK(c> 0)inherits:tbl_inherits_parent 

Example 4. de-Inheritance

Test=#Alter TableTbl_inherits_partition1 no inherit tbl_inherits_parent;ALTER TABLETest=# \d Tbl_inherits_partition1Table" Public. Tbl_inherits_partition1 "Column |Type|Modifiers--------+-----------------------+----------------------------------------------A| integer               |  not NULLb| character varying( +)|  not NULL default 'Try Me'::character varyingC| integer               |D|Date|  not NULLe| integer               |  not NULL default 0F| integer               | Checkconstraints: "Tbl_inherits_parent_c_check"CHECK(c> 0)

PostgreSQL----Inherit tables Inherits parent table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.