1、Creating a Table With SQL
Creating a Simple Table Using SQL
-- create a simple table for keeping track of birthdays
CREATE TABLE my_birthdays ( first_name VARCHAR2(20), last_name VARCHAR2(25), bday_date DATE );
Creating a Table With NOT NULL Constraints Using SQL
-- create a table with NOT NULL constraints in the HR schema
CREATE TABLE personal_info ( employee_id NUMBER(6,0) NOT NULL, birth_date DATE NOT NULL, social_security_id VARCHAR2(12) NOT NULL, marital_status VARCHAR2(10), dependents_claimed NUMBER(2,0) DEFAULT 1, contact_name VARCHAR2(45) NOT NULL, contact_phone VARCHAR2(20) NOT NULL, contact_address VARCHAR2(80) NOT NULL );
2、Adding, Altering, and Dropping a Table Column With SQL
Adding, Altering, and Dropping a Table Column Using SQL
-- add a new column
ALTER TABLE personal_info ADD (contact_email VARCHAR2(30) NULL);
-- modify a column
ALTER TABLE personal_info MODIFY (contact_email VARCHAR2(40) NOT NULL);
-- drop a column
ALTER TABLE personal_info DROP COLUMN contact_address;
3、Creating and Altering a Constraint With SQL
Creating, Altering, and Dropping Constraints Using SQL
-- add a primary key constraint
ALTER TABLE personal_info ADD CONSTRAINT personal_info_pkey PRIMARY KEY (employee_id);
-- add a foreign key constraint
ALTER TABLE personal_info ADD CONSTRAINT personal_info_fkey FOREIGN KEY (employee_id) REFERENCES employees (employee_id) ON DELETE CASCADE;
-- add a unique constraint
ALTER TABLE personal_info ADD CONSTRAINT personal_info_unique_con UNIQUE (social_security_id);
-- add a check constraint
ALTER TABLE personal_info ADD CONSTRAINT personal_info_check_con CHECK ( dependents_claimed > 0);
-- disable a constraint
ALTER TABLE personal_info DISABLE CONSTRAINT personal_info_check_con;
-- enable a constraint
ALTER TABLE personal_info ENABLE CONSTRAINT personal_info_check_con;
-- drop a constraint
ALTER TABLE personal_info DROP CONSTRAINT personal_info_check_con;
4、Renaming a Table With SQL
Renaming a Table Using SQL
-- rename the my_birthdays table
ALTER TABLE my_birthdays RENAME to birthdays;
5、Renaming a Table With SQL
Dropping a Table Using SQL
-- drop tables from the database
-- use caution when use the DROP statement!
DROP TABLE birthdays; DROP TABLE personal_info;
6、Creating, Altering, and Dropping an Index With SQL
Creating, Modifying, and Dropping an Index Using SQL
-- create an index on a single column to make queries faster on that column
CREATE INDEX emp_hiredate_idx ON employees (hire_date); -- rename the index
ALTER INDEX emp_hiredate_idx RENAME TO emp_hire_date_idx;
-- drop the index
DROP INDEX emp_hire_date_idx; -- create an index on two columns to make queries faster on the first column
-- or both columns
CREATE INDEX emp_mgr_id_ix ON employees (employee_id, manager_id); DROP INDEX emp_mgr_id_ix;
-- a function-based index precalculates the result and speeds up queries that
-- use the function for searching or sorting, in this case UPPER(last_name)
CREATE INDEX emp_upper_last_name_ix ON employees (UPPER(last_name)); DROP INDEX emp_upper_last_name_ix;
7、Creating, Altering, and Dropping an Index With SQL
Creating a View Using SQL
-- create a view to display data from departments and employees
CREATE OR REPLACE VIEW my_emp_view AS SELECT d.department_id, d.department_name, d.manager_id, e.employee_id, e.first_name, e.last_name FROM employees e, departments d WHERE d.manager_id = e.employee_id;
Dropping a View Using SQL
-- drop the view with the DROP VIEW statement
DROP VIEW my_emp_view;
8、Creating and Dropping a Sequence With SQL
Creating a Sequence Using SQL
-- create a new sequence to use with the employees table
-- this sequence starts at 1000 and increments by 1
CREATE SEQUENCE new_employees_seq START WITH 1000 INCREMENT BY 1; -- to use the sequence, first initialize the sequence with NEXTVAL
SELECT new_employees_seq.NEXTVAL FROM DUAL;
-- after initializing the sequence, use CURRVAL as the next value in the sequence
INSERT INTO employees VALUES
(new_employees_seq.CURRVAL, 'Pilar', 'Valdivia', 'pilar.valdivia',
'555.111.3333', '01-SEP-05', 'AC_MGR', 9100, .1, 101, 110);
-- query the employees table to check the current value of the sequence
-- which was inserted used as employee_id in the previous INSERT statement
SELECT employee_id, last_name FROM employees WHERE last_name = 'Valdivia';
Dropping a Sequence Using SQL
-- drop the sequence
DROP SEQUENCE new_employees_seq;
9、Creating and Dropping a Synonym With SQL
Creating a Synonym Using SQL
-- create a synonym for the employees table
CREATE SYNONYM emps for HR.employees; -- query the employees table using the emps synonym
SELECT employee_id, last_name FROM emps WHERE employee_id < 105;
Dropping a Synonym Using SQL
-- drop the synonym
DROP SYNONYM emps;