Create and manage tables in Oracle

1. Create a table

//Create a student table
create table student(
ID number(6), //student number
NAME varchar2(25), //name
BIRTH date, //birthday
GRADE number(3,1) //The maximum integer part of the grade is 3 digits, and the decimal part is 1 digit
)
//The second way to create a table, use the columns in the existing table as a new table
//Create some columns in the student1 table into a new table
create table student
as
select id,name,age from student1
where 1=2 //Indicates that the created table is an empty table, if this condition is not added, the data in the existing table will also be added to the new table

 2. Add a column to the created table

//Add the AGE column to the student table
alter table student
add (AGE number(2) default 18) //The default is 18 years old

 3. Modify the column

alter table student
modify (ID number(8)) //modify the number of digits in the ID column to 8, if there is data in the table, it cannot be modified

 Fourth, delete the column

alter table student
drop column AGE //delete the AGE column

 5. Rename the column

alter table student
rename column ID to IDCARD //Rename the column ID to IDCARD

 6. Clear the table

//Clear the data in the student table
truncate table student

 7. Annotation comments

(1) Add a comment to the specified table

-- Add comments to the table   
comment on table TABLENAME  
  is 'table comment';  

 (2) Add a comment to the specified column of the specified table.

-- Add comments to the columns   
comment on column TABLENAME.COLUMNNAME  
  is 'specified table specified column comment';  

 (3) Related Views

USER_TAB_COMMENTS
DBA_TAB_COMMENTS
ALL_TAB_COMMENTS
USER_COL_COMMENTS
DBA_COL_COMMENTS
ALL_COL_COMMENTS

 如:select  * from user_tab_comments where table_name = 'student';

Eight, TRUNCATE, DELETE, DROP put together to compare:
TRUNCATE TABLE: delete content, release space but not delete the definition (table structure).
DELETE TABLE: Deleting content does not delete the definition (table structure) and does not release space.
DROP TABLE: Deletes content and definitions (table structure), freeing up space.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326302368&siteId=291194637