Oracle Add NOT NULL constraint

Source: Internet
Author: User

When creating a table, add a NOT NULL constraint to the column in the following form:
column_name Data_type
[constraint constraint_name] NOT NULL
Where constraint constraint_name represents the specified name for the constraint.
You can also add a NOT NULL constraint to a table that you have created, and you need to use the ALTER TABLE ... modify statement in the following form:
ALTER TABLE table_name MODIFY COLUMN_NAME [constraint constraint_name] NOT null;


Remove NOT NULL constraint
If you need to remove the NOT NULL constraint for a laceration in the table, you still use the ALTER TABLE...MODIFY statement, in the following form:
ALTER TABLE table_name MODIFY COLUMN_NAME NULL;


The following are the specific actions:
sql> CREATE TABLE person (
2 PID Number (4) NOT NULL,
3 pname varchar2 (20),
4 Psex char (2)
5);
The table is created.
sql> desc person;
is the name empty? Type
----------------------------------------- -------- -------------------
PID not NULL number (4)
PNAME VARCHAR2 (20)
Psex CHAR (2)
Sql> ALTER TABLE person modify pname NOT null;
The table has changed.
sql> desc person;
is the name empty? Type
----------------------------------------- -------- ------------------
PID not NULL number (4)
PNAME not NULL VARCHAR2 (20)
Psex CHAR (2)
Sql> INSERT into the person values (1, ' aaa ', ' female ');
1 rows have been created.
Sql> INSERT into the person values (1, ' AAA ', NULL);
1 rows have been created.
sql> INSERT into person values (1,null,null);
INSERT into person VALUES (1,null,null) *
An error occurred on line 1th:
ORA-01400: Could not insert NULL ("SYSTEM". " Person "." PNAME ")
Sql> ALTER TABLE person modify PNAME null;
The table has changed.
sql> desc person;
is the name empty? Type
----------------------------------------- -------- ------------------
PID not NULL number (4)
PNAME VARCHAR2 (20)
Psex CHAR (2)
sql> INSERT into person values (1,null,null);
1 rows have been created.
Sql>

Oracle Add NOT NULL constraint

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.