DBMS COMMANDS
09 4Sql commands are instructions, coded into sql statements, which are
used to communicate with the database to perform specific tasks, work,
functions and queries with data. It is of two types : DML and DDL
commands.
DDL Commands:
DDL is a short name of Data Definition Language, which deals
with database schemas and descriptions, of how the data should
reside in the database. It has 5 commands namely CREATE,
ALTER, DROP, TRUNCATE, & RENAME.
   CREATE: This command is used to create database
    (MYSQL) and tables.
   ALTER: This command is used to alter the structure of the
    existing table.
   DROP: This command is used to delete or drop the table
    along with its structure.
   TRUNCATE: This command is used to delete all the records
    from the table including the space allotted to the records.
   RENAME: This command is used to rename the table.
DML Commands:
DML is a short name of Data Manipulation Language which deals
with data manipulation, and includes most common sql statements
such as SELECT, DELETE, UPDATE, INSERT etc and it is used
store, modify, retrieve, delete, and update data in tables.
   SELECT: This command is used to retrieve the data from
    table.
   INSERT: This command is used to insert the data in table.
   UPDATE: This command is used to update the existing the
    data in the table.
   DELETE: This command is used to delete all the records
    from a table including the spaces allotted for the records but
    this command can also be used to delete a single record using
    where condition which is not possible using TRUNCATE
    command.
DESCRIBE Command:
This command is used to describe the detailed structure of a table
which includes or shows the fields/columns/attributes, data type of
each column and the constraint used in every column(if used).
DBMS DATATYPES
Every data in database or table is of a particular category. For
example the data in ‘name’ field is of VARCHAR2 type whereas
‘phone number’ field can be of NUMBER type. The data type
available in oracle is: VARCHAR2, NUMBER, & DATE.
   VARCHAR2: This data type is used to insert string data in
    the table. The data while inserting should be represented in
    single quotes.
   NUMBER: This data type is used to insert number in the
    table. The data is written directly without single quotes.
   DATE: This data type is used to insert date in the table in the
    format DD-MM-YY. Note that MM should be written as
    JAN instead of 01 and YY should be written as 97 instead of
    1997 (In case of 2000 write 00). The date is also written in
    single quotes.
DISTINCT STATEMENT:
In a table, a column may contain many duplicate values; and
sometimes you only want to list the different (distinct) values. The
DISTINCT keyword can be used to return only distinct (different)
values.
ORDER BY STATEMENT:
The ORDER BY keyword is used to sort the result-set by one or
more columns. The ORDER BY keyword sorts the records in
ascending order by default. To sort the records in a descending
order, you can use the DESC keyword.
DBMS CONSTRAINTS
SQL constraints are used to specify rules for the data in a table. If
there is any violation between the constraint and the data action,
the action is aborted by the constraint. Constraints can be specified
when the table is created (inside the CREATE TABLE statement)
or after the table is created (inside the ALTER TABLE statement).
In SQL, we have the following constraints:
     NOT NULL: Indicates that a column cannot store NULL
      value.
     UNIQUE: Ensures that each row for a column must have a
      unique value.
     PRIMARY KEY: A combination of a NOT NULL and
      UNIQUE. Ensures that a column (or combination of two or
      more columns) have a unique identity which helps to find a
      particular record in a table more easily and quickly.
     FOREIGN KEY: Ensure the referential integrity of the data in
      one table to match values in another table.
     CHECK: Ensures that the value in a column meets a specific
      condition.
     DEFAULT: Specifies a default value for a column.
DBMS JOINS
SQL joins are used to combine rows from two or more tables.
The types of the different SQL JOINs you can use:
     INNER JOIN: Returns all rows when there is at least one
      match in BOTH tables.
     LEFT JOIN: Return all rows from the left table, and the
      matched rows from the right table.
     RIGHT JOIN: Return all rows from the right table, and the
      matched rows from the left table.
     FULL JOIN: Return all rows when there is a match in ONE
      of the tables.
CREATE COMMAND
1) Syntax for creating a table:
                         2) Creating the table:
3) Meaning of this command:
4) Result:
After you press execute button in Oracle ISQL plus server the
following line should be printed:
DESCRIBE COMMAND
Now you have created the table so it’s time to check that how the
table looks like or what is the structure of table. To view the
structure of table use DESCRIBE or DESC command.
1) Syntax for using DESCRIBING or DESC command:
2) Writing the command:
3) Result:
      NAME                NOT NULL?                 TYPE
      PERSONID                                   NUMBER(03)
     LASTNAME                                   VARCHAR2(25)
     FIRSTNAME                                  VARCHAR2(25)
      ADDRESS                                   VARCHAR2(25)
        CITY                                    VARCHAR2(25)
                                     **Structure of the above created table.
INSERT COMMAND
1) Syntax for INSERT command
2) Inserting the records in table:
3) Meaning of this command:
4) Result of the INSERT command:
After you press the execute button in ORACLE isql plus this
message will be displayed if there is no error in the code.
SELECT COMMAND
1) Syntax for SELECT command:
                                    And
2) Selecting or retrieving the records from table:
3) Meaning of the SELECT command:
4) Result of the SELECT command:
PERSONID LASTNAME FIRSTNAME ADDRESS                         CITY
    100            RAI            SAHIL              LPU   JALANDHAR
ALTER COMMAND
1) Syntax of ALTER command:
  a) To ADD a column: To add a new column in a table.
 b) To DROP a column: To drop any column from a table.
   c) To MODIFY a column: To modify the size of column i.e. the number of
  characters which a column accepts. Remember the size can only be increased.
2) Using ALTER command:
 a) Adding a column:
  b) Drop a column:
 c) Modifying a column:
     *Previous size of gender column was less than six. Using DESC command the
     change in size can be observed.
3) Meaning of ALTER command:
  a) ADD:
 b) DROP:
c) MODIFY:
4) Result:
  a) ADD:
  b) DROP:
  c) MODIFY:
UPDATE COMMAND
1) Syntax of UPDATE command:
2) Using UPDATE command:
3) Meaning of the UPDATE command:
4) Result:
DELETE COMMAND
1) Syntax of DELETE command:
2) Using DELETE command:
3) Meaning of DELETE command:
4) Result:
DROP COMMAND
1) Syntax of DROP command:
2) Using DROP command:
3) Meaning of DROP command:
4) Result:
TRUNCATE COMMAND
1) Syntax of TRUNCATE command:
2) Using TRUNCATE command:
3) Meaning of TRUNCATE command:
4) Result:
RENAME COMMAND
1) Syntax of RENAME command:
2) Using RENAME command:
3) Meaning of RENAME command:
4) Result:
NOT NULL CONSTRAINT
1) Using NOT NULL:
2) Checking of NOT NULL:
I) Using the DESCRIBE command:
       NAME                NOT NULL?           TYPE
       EMP_ID                    NOT NULL    NUMBER(03)
     EMP_NAME                    NOT NULL   VARCHAR2(20)
      EMP_DOB                                  DATE
II) Trying to insert a NULL value:
UNIQUE KEY CONSTRAINT
1) Using UNIQUE KEY:
 ** For defining a UNIQUE constraint on multiple columns:
2) UNIQUE constraint on ALTER table:
** For defining a UNIQUE constraint on multiple columns:
3) DROP a UNIQUE constraint:
**To DROP UNIQUE constraint on multiple columns:
4) Checking of UNIQUE KEY constraint:
 **Inserting a duplicate record in a column having UNIQUE key:
PRIMARY KEY CONSTRAINT
1) Using PRIMARY KEY constraint:
** For defining a PRIMARY KEY constraint on multiple columns:
2) PRIMARY KEY constraint on ALTER table:
** For defining a PRIMARY KEY constraint on multiple columns:
3) DROP PRIMARY constraint:
**To DROP PRIMARY constraint on multiple columns:
4) Checking of PRIMARY KEY constraint:
**Inserting a PRIMARY KEY in a table already having primary key
FOREIGN KEY CONSTRAINT
1) Using FOREIGN KEY constraint:
Create table course ( name varchar20, roll int references
student(roll)
** For defining a FOREIGN KEY constraint on multiple columns:
2) Inserting records in table having FOREIGN KEY constraint:
3) FOREIGN KEY constraint using ALTER table:
** For defining a PRIMARY KEY constraint on multiple columns:
3) DROP FOREIGN KEY constraint:
4) Checking the FOREIGN KEY constraint:
** Dropping the table having PRIMARY KEY which is referred as
FOREIGN KEY to the other table:
CHECK CONSTRAINT
1) Using CHECK constraint:
**For defining a CHECK constraint on multiple columns:
2) CHECK constraint on ALTER table:
**For defining a CHECK constraint on multiple columns:
3) DROP CHECK constraint:
4) Checking the CHECK constraint:
DEFAULT CONSTRAINT
1) Using DEFAULT constraint:
2) DEFAULT constraint using ALTER table:
3) DROP DEFAULT constraint:
4) Checking DEFAULT constraint:
  **Type in the INSERT command like this to access DEFAULT
value:
RENAMING A COLUMN
1) Syntax of RENAMING a column:
2) Renaming the column:
3) Meaning of the command:
4) Result:
CREATING NEW TABLE FROM AN
EXSISTING TABLE
1) Syntax:
2) Using the command:
** create a SQL table from another table without copying any
values from the old table:
Syntax:
Using the command:
3) Result:
ORDER BY CLAUSE
1) Syntax:
2) Using ORDER BY:
3) Result:
CUST_ID CUST_NAME CUST_ADDRESS   CUST_DOB
   105        AAKASH    LPU       16-JAN-97
   103       BINDESH    LPU       23-NOV-96
   101       CINTHIYA   LPU       24-FEB-97
   104         SAHIL    LPU       16-MAR-97
   102       ZAKARYA    LPU        22-SEP-96
LIKE CLAUSE
1) Syntax:
Or
2) Using LIKE Clause:
Or
3) Result:
CUST_ID CUST_NAME CUST_ADDRESS   CUST_DOB
     108      CHARLIE    LPU      16-JAN-97
     111     CHANDRA.V   LPU      23-NOV-96
     116      CHATUR     LPU      24-FEB-97
Or
CUST_ID CUST_NAME CUST_ADDRESS   CUST_DOB
     105      AAKASH     LPU      16-JAN-97
     104       SAHIL     LPU      16-MAR-97
     107      ASHISH     LPU       22-SEP-96
IN CLAUSE
1) Syntax:
2) Using IN CLAUSE:
3) Result:
CUST_ID CUST_NAME CUST_ADDRESS                             CUST_DOB
    120          SAMEER                  LPU                 16-JAN-97
    131          SAMEIRA                 LPU                 16-MAR-97
    115          REHMAN                  LPU                  22-SEP-96
*there is one more clause called ‘NOT IN’ clause which is just reverse of ‘IN’
clause where details of all the records will be displayed except those
records/names/values provided in ‘NOT IN’ clause.(Syntax and usage is same as
‘IN’ clause).
GROUP BY CLAUSE
1) Syntax:
2) Use of GROUP BY Clause:
3) Result:
      EMP_NO               COUNT(ENAME)
             101                1
             102                1
             103                1
             104                1
             105                1
HAVING CLAUSE
1) Syntax:
2) Use of HAVING Clause:
3) Result:
      EMPNAME                               COUNT(DEPTNO)
            ABC                                        1
            PQR                                        1
SQL FUNCTIONS
**for implementing every sql functions we will use dummy table named as dual.
1) abs(number)
This function Returns modulus of negative number.
Syntax:
SELECT abs(-126) “ABSOLUTE” FROM dual;
OUTPUT:
                                 ABSOLUTE
                                    126
2) power (m,n)
This function returns the power of a given number where m is number and n is
power.
Syntax:
SELECT power(3,3) “POWER” FROM dual;
OUTPUT:
                                    POWER
                                      27
3) Round(number)
This function Returns round off value for a decimal number. If decimal point is
greater than or equal to 0.5 then the next higher value is returned else the same
number is returned.
Syntax:
SELECT round(178.499) “ROUND” FROM dual;
OUTPUT:
                                      ROUND
                                       178
4) Truncate(n,m)
This function Returns the number after mentioning the number of decimal parts to
be shown where n is the number and m is the number of decimal parts to be
included.
Syntax:
SELECT trunc(178.599,2) “TRUNCATE” FROM dual;
OUTPUT:
                                  TRUNCATE
                                    178.59
5) sqrt(number)
This function Returns square root of any given number.
Syntax:
SELECT sqrt(25) “SQUARE ROOT” FROM dual;
OUTPUT:
                                SQUARE ROOT
                                     5
6) Greatest (number1, number2, number3….)
This function Returns the greatest number amongst the group of numbers.
Syntax:
SELECT greatest (14,22,7) “GREATEST” FROM dual;
OUTPUT:
                                  GREATEST
                                     22
7) Least(number1, number2, number3…..)
This function Returns the least number amongst all the numbers.
Syntax:
SELECT least(14,22,7) “LEAST” FROM dual;
OUTPUT:
                                    LEAST
                                      7
7) Modulus (n,m)
This function Returns remainder of the two provided numbers where n is the
number divided by m.
Syntax:
SELECT mod(15,7) “MODULUS” FROM dual;
OUTPUT:
                                  MODULUS
                                     1
8) Floor(number)
This function Returns the same base value of any provided number.
Syntax:
SELECT floor(126.99) “FLOOR” FROM dual;
OUTPUT:
                                     FLOOR
                                       126
9) Ceil(number)
This function Returns the next higher value for any given number.
Syntax:
SELECT ceil(126.12) ”CEIL” FROM dual;
OUTPUT:
                                      CEIL
                                       127
STRING FUNCTIONS
1) Lower (character/word/sentence)
This function Returns the provided character in lower case.
Syntax:
SELECT lower(‘SAHIL’) “LOWER” FROM dual;
OUTPUT:
                                     LOWER
                                      Sahil
2) Initcap(character/word/sentence)
This function Returns the word with its first character being capitalized.
Syntax:
SELECT initcap(‘sahil rai’) “INITCAP” FROM dual;
OUTPUT:
                                      INITCAP
                                      Sahil Rai
3) Upper(character/word/sentence)
This function Returns the character or word with complete upper case.
Syntax:
SELECT upper(‘sahil rai’) “UPPER” FROM dual;
OUTPUT:
                                    UPPER
                                   SAHIL RAI
4) Length(character/word/sentence)
This function Returns the length of the word or sentence in integer or number.
Syntax:
SELECT length(‘sahil rai’) “LENGTH” FROM dual;
OUTPUT:
                                    LENGTH
                              9(counts the space also)
5) Ltrim(word/sentence, character(to be removed))
This function Returns the string after omitting the character mentioned in the
function from the left.
Syntax:
SELECT ltrim(‘SAHIL’,’S’) “LTRIM” FROM dual;
OUTPUT:
                                      LTRIM
                                       AHIL
6) Rtrim(word/sentence, character(to be removed))
This function Returns the string after omitting the character the mentioned in the
function from the right.
Syntax:
SELECT rtrim(‘SAHIL’,’L’) “RTRIM” FROM dual;
OUTPUT:
                                      RTRIM
                                       SAHI
7) Trim(TRAILING ‘character’ from word/sentence)
                        Or
   Trim(LEADING ‘character’ from word/sentence)
                        Or
   Trim(BOTH ‘character’ from word/sentence)
This function Returns the word or sentence after removing the specified character
from left or front in case of LEADING, from right or last in case of TRAILING and
from both the ends in case of BOTH.
Syntax:
SELECT trim(TRAILING ‘M’ from MADAM) “TRIM” FROM dual;
SELECT trim(LEADING ‘M’ from MADAM) “TRIM” FROM dual;
SELECT trim(BOTH ‘M’ from MADAM) “TRIM” FROM dual;
OUTPUT:
                                      TRIM
                                      MADA
                                      TRIM
                                      ADAM
                                       TRIM
                                       ADA
AVERAGE FUNCTIONS
1) Average(column name)
This function Returns the average of all the numeric values in a particular column.
Syntax:
SELECT avg(salary) “AVERAGE SALARY” FROM employ;
OUTPUT:
                               AVERAGE SALARY
                                    47500
2) Minimum (column name)
This function Returns the minimum value amongst all the values in a column.
Syntax:
SELECT min(SALARY) “MINIMUM SALARY” FROM employ;
OUTPUT:
                             MINIMUM SALARY
                                  23000
3) Maximum(column name)
This function Returns the maximum value multiple values in a column.
Syntax:
SELECT max(SALARY) “MAXIMUM SALARY” FROM employ;
OUTPUT:
                             MAXIMUM SALRY
                                 90000
4) Sum(column name)
This function Returns the sum of the values in a column.
Syntax:
SELECT sum(SALARY) “SUM SALARY” FROM employ;
OUTPUT:
                                 SUM SALARY
                                   380000
5) Count(column name) & Count(*)
The difference between count(column name) and count(*) is that the count(column
name) will count the values present in that column whereas the count(*) will count
all the rows in a table.
Syntax:
SELECT count(SALARY) “COUNT SALARY” FROM employ;
OUTPUT:
                                COUNT SALARY
                                      8