Oracle Edit Table Usage

Source: Internet
Author: User

Syntax for adding a field: ALTER TABLE tablename Add (column datatype [default value][null/not null],....);

Modify the syntax of the field: ALTER TABLE tablename modify (column datatype [default value][null/not null],....);

Syntax for deleting a field: ALTER TABLE tablename drop (column);

To create a table structure:
CREATE TABLE Test1
(ID varchar2 () not NULL);

Add a field:

ALTER TABLE Test1
Add (name VARCHAR2 () default ' anonymous ' not NULL);

Add three fields using one SQL statement at a time:

ALTER TABLE Test1
Add (name VARCHAR2 () default ' anonymous ' NOT NULL,

The age integer default is not NULL,

Has_money Number (9,2)

);

Modify a field

ALTER TABLE Test1
Modify (name VARCHAR2 (+) Default ' unknown ');

Delete a field

ALTER TABLE Test1
Drop column name;

Oracle modifies table field names and lengths in a way that is different from standard SQL, and it needs to add specific keywords.


Use the rename keyword to modify the field name: ALTER TABLE name rename column Old field name to new field name;

Alert Table WTC rename column qy to Qcompany;

Use the modify keyword to implement modifications to the data type: ALTER TABLE name modify field name data type;

Alert Table WTC modify column Qcompany varchar2 (500);

Advanced usage:

Renaming a table
ALTER TABLE
table_name RENAME to New_table_name;

Modify the name of a column

Grammar:
ALTER TABLE table_name RENAME COLUMN supplier_name to sname;

Example:
ALTER TABLE s_dept Rename column age to Age1;

Attached: Creating a table with a primary key >>

CREATE TABLE Student (
StudentID int PRIMARY key NOT NULL,
Studentname varchar (8),
age int);

1. Create a PRIMARY KEY constraint while creating a table
(1) No naming
CREATE TABLE Student (
StudentID int PRIMARY key NOT NULL,
Studentname varchar (8),
age int);
(2) have a name
CREATE TABLE Students (
StudentID int,
Studentname varchar (8),
Age int,
Constraint yy primary KEY (StudentID));


2. Delete the existing PRIMARY KEY constraint in the table
(1) No naming
Available as SELECT * from User_cons_columns;
Find primary key name in table student table with primary key named sys_c002715
ALTER TABLE student drop constraint sys_c002715;
(2) have a name
ALTER TABLE students drop constraint yy;


3. Add a PRIMARY KEY constraint to the table
ALTER TABLE student ADD constraint Pk_student primary key (StudentID);

Oracle Edit Table Usage

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.