In Oracle PL/SQL, the CREATE TABLE statement is used to create a new table in a database. The basic syntax for creating a table is as follows:
CREATE TABLE table_name ( column1 datatype constraint, column2 datatype constraint, column3 datatype constraint, .... );
table_name is the name of the table that you want to create.
column1, column2, column3, etc. are the names of the columns in the table.
datatype is the data type for each column (e.g. NUMBER, VARCHAR2, DATE, etc.).
constraint is any constraints you want to apply to the column (e.g. NOT NULL, UNIQUE, PRIMARY KEY, etc.).
Create sequence
CREATE SEQUENCE course_seq MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 20;
Create table
CREATE TABLE course( course_id NUMBER NOT NULL, name VARCHAR2(250) NOT NULL, description VARCHAR2(4000), duration VARCHAR2(25), price NUMBER, CONSTRAINT pk_course_id PRIMARY KEY(course_id) );
Create trigger
CREATE OR REPLACE TRIGGER course_t1 BEFORE INSERT ON course FOR EACH ROW DECLARE BEGIN IF :new.course_id IS NULL THEN SELECT course_seq.nextval INTO :new.course_id FROM dual; END IF; END;
Insert into table
SET DEFINE OFF; Insert into COURSE (NAME,DESCRIPTION,DURATION,PRICE) values ('SQL 1','SQL course for beginners','1 week',10); Insert into COURSE (NAME,DESCRIPTION,DURATION,PRICE) values ('SQL 2','SQL course for advanced','2 weeks',50); Insert into COURSE (NAME,DESCRIPTION,DURATION,PRICE) values ('HTML5','Learn HTML 5','1 week',10); Insert into COURSE (NAME,DESCRIPTION,DURATION,PRICE) values ('PHP','PHP course','4 weeks',75); Insert into COURSE (NAME,DESCRIPTION,DURATION,PRICE) values ('CSS','Learn CSS','2 weeks',20); COMMIT;
Create table backup
CREATE TABLE course_backup AS SELECT * FROM course;
COURSE_ID | NAME | DESCRIPTION | DURATION | PRICE |
---|---|---|---|---|
1 | SQL 1 | SQL course for beginners | 1 week | 10 |
2 | SQL 2 | SQL course for advanced | 2 week | 50 |
3 | HTML5 | Learn HTML 5 | 1 week | 10 |
4 | PHP | PHP course | 4 week | 75 |
5 | CSS | Learn CSS | 2 week | 20 |