Oracle alter table instance

Source: Internet
Author: User

-- In this case, you need to add several constraints to the table. The syntax for adding constraints is as follows:
-- Alter table name add constraint name constraint type (constraint field)
-- Delete constraints:
-- Alter table book drop constraint book_pid_fk;

-- Pk add primary key
Alter table dirk_dept add constraint dept_pk primary key (id );
Alter table dirk_company add constraint comp_pk primary key (id );
Alter table dirk_emp add constraint emp_pk primary key (id );

-- Fk add foreign key
Alter table dirk_dept add constraint dept_fk_comp foreign key (companyid) references dirk_company;
Alter table dirk_emp add constraint emp_fk_dept foreign key (deptid) references dirk_dept; -- you can add on delte cascade
-- Foreign key reference behavior delete cascade/delete set null/(default) delete on action

-- Uk add constraint unique
Alter table dirk_emp add constraint dirk_emp_num_uk unique (num );

-- Ck add check
Alter table dirk_emp add constraint ck_dirk_emp_num check (num between 0 and 250)
Alter table dirk_emp add constraint

-- Add column
Alter table dirk_emp add (score number (20 ));

-- Add comment
Comment on column dirk_emp.score is 'employee score ';

-- Modify column property -- it will current error when exist null value
Alter table dirk_emp modify score not null;
-- Modify default value
Alter table dirk_emp modify score default 100;
-- Modify length or type
Alter table dirk_emp modify name number (20) -- varchar2 (50)

-- Remove column
Alter table dirk_emp drop column score;

-- Contraint disable or enable: enable Constraints
Alter table dirk_emp disable constraint dirk_emp_num_uk;
Alter table dirk_emp enable constraint dirk_emp_num_uk;

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.