ALTER table adds a column and adds a check constraint

Source: Internet
Author: User

Oracle's Constraints

* If a constraint works only on separate fields, you can define constraints at the field level, or you can define constraints at the table level, but if a constraint acts on more than one field,
Constraints must be defined at the table level
* The constraint can be defined by the CONSTRAINT keyword, and if not specified, Oracle automatically establishes the default name for the constraint

Define PRIMARY KEY constraints (single field)
CREATE TABLE Employees (empno number (5) primary key,...)

Sql>
Sql> CREATE TABLE EMP (empno number (4) is not NULL,
2 ename varchar2 (10),
3 Job Varchar2 (9),
4 Mgr Number (4),
5 HireDate Date,
6 Sal Number (7, 2),
7 Comm Number (7, 2),
8 Deptno Number (2));

Table created.

Sql>
sql> INSERT INTO EMP values (7369, ' Smith ', ' clerk ', 7902, to_date (' 17-dec-1980 ', ' dd-mon-yyyy '), 20;

1 row created.

sql> INSERT INTO EMP values (7499, ' Allen ', ' salesman ', 7698, to_date (' 20-feb-1981 ', ' dd-mon-yyyy '), 1600, 300, 30);

1 row created.

sql> INSERT INTO EMP values (7521, ' Ward ', ' salesman ', 7698, to_date (' 22-feb-1981 ', ' dd-mon-yyyy '), 1250, 500, 30);

1 row created.

sql> INSERT INTO EMP values (7566, ' Jones ', ' manager ', 7839, to_date (' 2-apr-1981 ', ' dd-mon-yyyy '), 2975, NULL, 20);

1 row created.

sql> INSERT INTO EMP values (7654, ' Martin ', ' salesman ', 7698,to_date (' 28-sep-1981 ', ' dd-mon-yyyy '), 1250, 1400, 30);

1 row created.

sql> INSERT INTO EMP values (7698, ' Blake ', ' manager ', 7839,to_date (' 1-may-1981 ', ' dd-mon-yyyy '), 2850, NULL, 30);

1 row created.

sql> INSERT INTO EMP values (7782, ' Clark ', ' manager ', 7839,to_date (' 9-jun-1981 ', ' dd-mon-yyyy '), 2450, NULL, 10);

1 row created.

sql> INSERT INTO EMP values (7788, ' Scott ', ' analyst ', 7566,to_date (' 09-dec-1982 ', ' dd-mon-yyyy '), 3000, NULL, 20);

1 row created.

sql> INSERT INTO EMP values (7839, ' king ', ' president ', null,to_date (' 17-nov-1981 ', ' dd-mon-yyyy '), 5000, NULL, 10);

1 row created.

sql> INSERT INTO EMP values (7844, ' Turner ', ' salesman ', 7698,to_date (' 8-sep-1981 ', ' dd-mon-yyyy '), 1500, 0, 30);

1 row created.

sql> INSERT INTO EMP values (7876, ' Adams ', ' Clerk ', 7788,to_date (' 12-jan-1983 ', ' dd-mon-yyyy '), 1100, NULL, 20);

1 row created.

sql> INSERT INTO EMP values (7900, ' James ', ' Clerk ', 7698,to_date (' 3-dec-1981 ', ' dd-mon-yyyy '), 950, NULL, 30);

1 row created.

sql> INSERT INTO EMP values (7902, ' Ford ', ' Analyst ', 7566,to_date (' 3-dec-1981 ', ' dd-mon-yyyy '), 3000, NULL, 20);

1 row created.

sql> INSERT INTO EMP values (7934, ' Miller ', ' clerk ', 7782,to_date (' 23-jan-1982 ', ' dd-mon-yyyy '), 1300, NULL, 10);

1 row created.

Sql>
Sql> select * from EMP;

empno ename Job Mgr HireDate Sal Comm Deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 Smith Clerk 7902 17-dec-80 800 20
7499 Allen salesman 7698 20-feb-81 1600 300 30
7521 ward salesman 7698 22-feb-81 1250 500 30
7566 Jones manager 7839 02-apr-81 2975 20
7654 Martin salesman 7698 28-sep-81 1250 1400 30
7698 Blake Manager 7839 01-may-81 2850 30
7782 Clark Manager 7839 09-jun-81 2450 10
7788 Scott analyst 7566 09-dec-82 3000 20
7839 King President 17-nov-81 5000 10
7844 Turner salesman 7698 08-sep-81 1500 0 30
7876 Adams Clerk 7788 12-jan-83 1100 20

empno ename Job Mgr HireDate Sal Comm Deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7900 James Clerk 7698 03-dec-81 950 30
7902 Ford analyst 7566 03-dec-81 3000 20
7934 Miller Clerk 7782 23-jan-82 1300 10

Rows selected.

Sql>
sql> ALTER TABLE EMP
2 Add (
3 gender varchar (10));

Table Altered.

Sql>
sql> ALTER TABLE EMP
2 Add Constraint Ck_gender
3 Check (Gender in (' Male ', ' female '));

Table Altered.

Sql>
sql> Update emp Set gender = ' Male ' where mod (empno,2) = 0;

Ten rows updated.

Sql>
Sql> select * from EMP;

empno ename Job Mgr HireDate Sal Comm DEPTNO Gender
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7369 Smith Clerk 7902 17-dec-80 800 20
7499 Allen salesman 7698 20-feb-81 1600 300 30
7521 ward salesman 7698 22-feb-81 1250 500 30
7566 Jones manager 7839 02-apr-81 2975 Male
7654 Martin salesman 7698 28-sep-81 1250 1400
7698 Blake Manager 7839 01-may-81 2850 Male
7782 Clark Manager 7839 09-jun-81 2450 Male
7788 Scott analyst 7566 09-dec-82 3000
7839 King President 17-nov-81 5000 10
7844 Turner salesman 7698 08-sep-81 1500 0 Male
7876 Adams Clerk 7788 12-jan-83 1100

empno ename Job Mgr HireDate Sal Comm DEPTNO Gender
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7900 James Clerk 7698 03-dec-81 950 Male
7902 Ford analyst 7566 03-dec-81 3000
7934 Miller Clerk 7782 23-jan-82 1300-Male

Rows selected.

Sql>
sql> Update emp Set gender = ' female ' where mod (empno,2) = 1;

4 rows updated.

Sql>
Sql> Select ename, Job, gender from EMP order by gender, ename;

ename      job       Gender
----------------------- ------
allen      salesman  female
king       President Female
smith      clerk     female
ward        salesman  female
adams      clerk      Male
blake      manager   male
clark       manager   male
ford       analyst   male
James       clerk     male
jones      Manager    male
martin     salesman  male

ENAME Job Gender
---------- --------- ----------
Miller Clerk Male
Scott Analyst Male
Turner salesman Male

Rows selected.

Sql>
sql> INSERT INTO EMP (empno, ename, gender) VALUES (8001, ' Pat ', ' unknown ');
INSERT INTO EMP (empno, ename, gender) VALUES (8001, ' Pat ', ' unknown ')
*
Error at line 1:
Ora-02290:check constraint (Sys.ck_gender) violated


Sql>
sql> drop table emp;

Table dropped.

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.