Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
115 views

CREATE - Create The Table: Commands

SQL supports four main categories of commands: Data Definition Language (DDL) to define database schema, Data Manipulation Language (DML) to query and modify data, Data Control Language (DCL) to manage permissions, and Transaction Control Language (TCL) to manage transactions. DDL commands like CREATE, ALTER, and DROP are used to create, modify, and delete database objects. DML commands like INSERT, SELECT, UPDATE, and DELETE allow adding, querying, modifying, and removing data.

Uploaded by

kamalmse066072
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

CREATE - Create The Table: Commands

SQL supports four main categories of commands: Data Definition Language (DDL) to define database schema, Data Manipulation Language (DML) to query and modify data, Data Control Language (DCL) to manage permissions, and Transaction Control Language (TCL) to manage transactions. DDL commands like CREATE, ALTER, and DROP are used to create, modify, and delete database objects. DML commands like INSERT, SELECT, UPDATE, and DELETE allow adding, querying, modifying, and removing data.

Uploaded by

kamalmse066072
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL supports the following categories of commands to communicate with the database:-

Commands
Data Definition Language Create, Alter, Drop, Rename & Truncate

Data Manipulation Language Insert, Delete, Update

Data Control Language Grant, Revoke

Transaction Control Language Commit, Rollback, Save point

Syntax

CREATE – Create the table


a. simple creation
CREATE TABLE < tablename> (
<column name1 > < datatype>,
<column name 2> < datatype>,
<column name 3> < datatype>
);
e.g CREATE TABLE emp_demo1
( employee_id NUMBER(6)
, first_name VARCHAR2(20)
, last_name VARCHAR2(25)
, email VARCHAR2(25)
, phone_number VARCHAR2(20)
, job_id VARCHAR2(10)
, salary NUMBER(8,2)
, deptid NUMBER(4)
);

CONSTRAINT DESCRIPTION

NOT NULL Specifies that a column must have some value.

UNIQUE Specifies that columns must have unique values.

PRIMARY KEY Specifies a column or a set of columns that uniquely identifies as


row. It does not allow null values.
FOREIGN KEY Foreign key is a column(s) that references a column(s) of a table.

CHECK Specifies a condition that must be satisfied by all the rows in a


table.
b. Without constraint name
CREATE TABLE < tablename> (
<column name 1> < datatype>,
<column name 2> < datatype> UNIQUE ,
<column name 3> < datatype> ,
PRIMARY KEY ( <column name2>)
);
e.g CREATE TABLE emp_demo2
( employee_id NUMBER(6) PRIMARY KEY
, first_name VARCHAR2(20) NOT NULL
, last_name VARCHAR2(25) NOT NULL
, email VARCHAR2(25) UNIQUE
, phone_number VARCHAR2(20) UNIQUE
, job_id VARCHAR2(10)
, salary NUMBER(8,2) CHECK(SALARY>0)
, deptid NUMBER(4)
);
c. With constraint name
CREATE TABLE < tablename1> (
<column name 1> < datatype> CONSTRAINT <constraint name1> UNIQUE,
<column name 2> < datatype> CONSTRAINT <constraint name2> NOT NULL,
constraint < constraint name3 > PRIMARY KEY ( <column name1>),
constraint <constraint name4> FOREIGN KEY (<column name2>)
REFERENCES <tablename2> (<column name1>)
);
e.g CREATE TABLE emp_demo3
( employee_id NUMBER(6) CONSTRAINT emp_eid PRIMARY KEY
, first_name VARCHAR2(20)
, last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn NOT NULL
, email VARCHAR2(25) CONSTRAINT emp_email_nn NOT NULL
, phone_number VARCHAR2(20)
, job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL
, salary NUMBER(8,2) CONSTRAINT emp_salary_nn NOT NULL
, deptid NUMBER(4), CONSTRAINT emp_dept FOREIGN KEY(deptid)
REFERENCES department(deptid)
, CONSTRAINT emp_salary_min CHECK (salary > 0)
, CONSTRAINT emp_email_uk UNIQUE (email)
);
d. With check constraint
CREATE TABLE < tablename> (
<column name1 > < datatype> ,
<column name 2> < datatype>,
CHECK ( < column name 1 > in ( values) )
CHECK ( < column name 2 > between <val1> and <val2> )
e.g CREATE TABLE emp_demo4
( emp_id NUMBER(6),
emp_name VARCHAR2(15),
salary NUMBER(10)CHECK (salary between 1000 and 10000)
);

ALTER – Alter the table

1. INSERT
a. ADD –to add new columns
ALTER TABLE <tablename> ADD ( <column name > < datatype>);
Eg: ALTER TABLE emp_demo4 ADD(emp_name varchar2(20));

Adding Constriants
ALTER TABLE <tablename> ADD CONSTRAINT <constraint_name> constriant_type
(<column name>);
ALTER TABLE emp_demo4 ADD CONSTRAINT con_pk1 PRIMARY KEY(emp_id);
ALTER TABLE emp_demo4 ADD CONSTRAINT con_emp_uk UNIQUE(phoneno);
ALTER TABLE emp_demo4 ADD CONSTRAINT con_empfk FOREIGN KEY(DNO)
REFERENCES department(dno);
ALTER TABLE emp_demo4 ADD CONSTRAINT con_emp_ck CHECK ( salary >0 );

ALTER TABLE emp_demo4 MODIFY (<Column name> <datatype> CONSTRAINT


constraint_name NOT NULL);

b. MODIFY - Modify the datatype or increase / decrease the column width


ALTER TABLE <tablename> MODIFY ( <column name > < newdatatype>);
Eg: ALTER TABLE emp_demo4 MODIFY(emp_id varchar2(20));

c. DROP –delete column or remove constraint


ALTER TABLE <tablename> DROP COLUMN < column name>;
Eg: ALTER TABLE emp_demo4 DROP COLUMN salary;

ALTER TABLE <tablename> DROP CONSTRAINT < constraint name >;

The CASCADE clause drops any foreign keys that reference the primary key.
e.g ALTER TABLE departments DROP PRIMARY KEY CASCADE;

If you know that the name of the PRIMARY KEY constraint is pk_dept, then you could also
drop it with the following statement:

e.g ALTER TABLE departments DROP CONSTRAINT pk_dept CASCADE;

Drop the unique key on the email column of the employees table:
e.g ALTER TABLE employees DROP UNIQUE (email);

d. Rename – Rename table


RENAME <oldtablename> TO <newtablename>;
e.g. RENAME employees TO emp_det;

ALTER TABLE <tablename> RENAME COLUMN <oldcolumnname>


TO <newcolumn name>

DML
a. Inserting values from user
INSERT INTO <tablename> VALUES( val1,val2 …);
e.g INSERT INTO emp_demo4 VALUES(3,’anitha’,5000);
b. Inserting interactively
INSERT INTO <tablename> VALUES( &<column name1> , & <column name2> …);
e.g INSERT INTO emp_demo4 VALUES(&emp_id,’&emp_name’,&salary);
c. Inserting null values
INSERT INTO <tablename> VALUES( val1,’ ‘,’ ‘,val4);
e.g INSERT INTO emp_demo4 VALUES(4,NULL, ‘’);

2. SELECT

a. Simple select
SELECT * FROM < tablename>;
SELECT <col1>, <col2> FROM < tab1>;
e.g SELECT emp_id, emp_name FROM emp_demo4;
b. Alias name
SELECT <col1> <alias name 1> , <col2> < alias name 2>FROM < tab1>;
e.g SELECT emp_id “employee id”, emp_name as employee name,
salary*12 salary FROM emp_demo4;

c. With distinct clause


SELECT DISTINCT <col2> FROM < tab1>;
e.g SELECT DISTINCT emp_name FROM emp_demo4;

d. With where clause


SELECT <col1>, <col2> FROM < tab1> WHERE <conditions>;

3. UPDATE
Updating a column value
UPDATE < tablename> SET <col> = < new value>;
Updating a column value with where clause
UPDATE < tablename> SET <col1> = < new value> , <col2> = < new value>
WHERE <conditions>;

4.DELETE
Deleting all rows
DELETE FROM <tablename>;
Deleting specific rows
DELETE FROM <tablename> WHERE <condition>;

You might also like