SQL Help
SQL Help
CREATE TABLE
The Create Table Command: - it defines each column of the table uniquely. Each has minimum of three
attributes, a name , data type and size.
Syntax:
CREATE TABLE <TABLE NAME> (<COL1> <DATATYPE>(<SIZE>),<COL2> <DATATYPE><SIZE>));
Example:
1. CREATE TABLE EMP(EMPNO NUMBER(4) PRIMARY KEY, ENAME CHAR(10));
2. CREATE TABLE PROG20 (PNAME VARCHAR2(20) NOT NULL, DOJ DATE NOT NULL,
DOB DATE NOT NULL, SEX VARCHAR(1) NOT NULL, PROF1 VARCHAR(20),
PROF2 VARCHAR(20),SALARY NUMBER(7,2) NOT NULL);
Rules:
1. Oracle reserved words cannot be used.
3. Underscore, numerals, letters are allowed but not blank space.
3. Maximum length for the table name is 30 characters.
4. 2 different tables should not have same name.
5. We should specify a unique column name.
6. We should specify proper data type along with width.
7. We can include “not null” condition when needed. By default it is ‘null’.
ALTER TABLE
Alter command is used to:
1. Add a new column.
2. Modify the existing column definition.
3. To include or drop integrity constraint.
Example:
1. ALTER TABLE EMP ADD (PHONE_NO CHAR (20));
2. ALTER TABLE EMP MODIFY(PHONE_NO NUMBER (10));
3. ALTER TABLE EMP ADD CONSTRAINT Pkey1 PRIMARY KEY (EmpNo);
DROP TABLE
It will delete the table structure provided the table should be empty.
Syntax: DROP TABLE <TABLENAME>;
Example: DROP TABLE PROG20; //Here prog20 is table name
TRUNCATE TABLE
If there is no further use of records stored in a table and the structure has to be retained
then the records alone can be deleted.
Syntax: TRUNCATE TABLE <TABLE NAME>;
Example: TRUNCATE TABLE CUSTOMER;
DESC
This is used to view the structure of the table.
Syntax: DESC <TABLENAME>;
Example: DESC EMP;
NAME NULL? TYPE
EMPNO NOT NULL NUMBER(5)
ENAME NOT NULL VARCHAR(15)
JOB NOT NULL CHAR(10)
DEPTNO NOT NULL NUMBER(3)
PHONE_NO NUMBER (10)
DML COMMANDS
DML commands are the most frequently used SQL commands and is used to query and
manipulate the existing database objects. Some of the commands are :
Insert
Select
Update
Delete.
INSERT Command
Insert Command This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The values must be
entered in the same order as they are defined.
Example:
First create a table named STD
CREATE TABLE STD (SNO NUMBER(5),SNAME VARCHAR2(20), AGE NUMBER(5),
SDOB DATE,SM1 NUMBER(4,2),SM2 NUMBER(4,2),SM3 NUMBER(4,4));
SELECT Command
Select query is used to retrieve data from a tables. It is the most used SQL query. We can retrieve complete
tables, or partial by mentioning conditions using WHERE clause.
Syntax:
The above query will fetch information of S_ID, S_NAME and AGE column from Student table
A special character asterisk * is used to address all the data(belonging to all columns) in a query.
The above query will show all the records of Student table that means it will show complete Student table
as result.
The above command will display a new column in the result, showing 3000 added into existing salaries of
the employees.
UPDATE command
Update command is used to update a row of a table. A single column may be updated or more than
one column could be updated.
Example:
UPDATE STUDENT SET AGE=18 WHERE S_ID=102;
S_ID S_NAME AGE
101 ADAM 15
102 ALEX 18
103 CHRIS 14
DELETE command
Delete command is used to delete data from a table. Delete command can also
be used with condition to delete a particular row. The delete command consists
of a from clause followed by anoptional where clause.
Following is
its general
syntax,
DELETE
from table-
name;