49
SQL (DDL & DML Commands)

SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Embed Size (px)

Citation preview

Page 1: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

SQL (DDL & DML Commands)

Page 2: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Modifying a Table (Alter Command)

• To change the definition of the table. • This can be done by using ALTER TABLE

command. This command may have one of the following clauses.

• ADD| MODIFY| DROP

Page 3: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Alter Command

• The general syntax for the ALTER TABLE is as follows:

ALTER TABLE <table name>[ADD|MODIFY| DROP]

(Constraint | | column specification)

Page 4: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Alter Command (ADD)

• ADD Clause: The ADD clause is used to add a column and/or constraints to an existing table.

• To add a column say part_full_time to emp table, the syntax will be as follows:

ALTER TABLE emp ADD (part_full_time CHAR(1));• If this table is having some existing data

corresponding to other columns, then it will take NULL for this column corresponding to those records.

Page 5: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Alter Command (ADD)

• The following syntax will be executed only in that case if table is empty:

• ALTER TABLE emp ADD (part_full_time CHAR (1) NOT NULL);

Otherwise error is generated.Adding Multiple Columns: ALTER TABLE emp ADD (part_time CHAR (1),

full_time varchar(2));

Page 6: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

MODIFY Clause• This clause is used to modify the column specifications and the

constraints. •In case of constraints, only possibilities are to modify a NULL to NOT NULL and NOT NULL to NULL. Other constraints should be first deleted and then recreated with the modification.• Suppose we want to increase the width of a column, syntax is ALTER TABLE emp MODIFY (sal NUMBER(5));

• It will increase the width of sal column from NUMBER(4) to NUMBER(5).

However, for decreasing the width of a column or changing the data type of the column, the column must not contain any data.

Page 7: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

MODIFY Clause

• For modification in the constraint, the syntax is: ALTER TABLE emp MODIFY(sal NUMBER(5) NOT NULL);Now, sal column will become NOT NULL column.

Page 8: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

DROP Clause• We can remove a column from table directly by

using the DROP clause For example, ALTER TABLE emp DROP COLUMN part_full_time; This will drop the part_full_time column from the

table along with the data.• DROP TABLE emp; // To drop table along with

data permanently.

Page 9: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

DROP Clause

• To drop a constraint, the different syntaxes are ALTER TABLE emp DROP CONSTRAINT chk_sal;// To drop Check Constraint.

• ALTER TABLE emp DROP PRIMARY KEY; Or ALTER TABLE emp DROP constraint pk_empno;//To drop primary key.

Page 10: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

DROP Clause

• ALTER TABLE emp DROP PRIMARY KEY CASCADE;

//This command will drop all the dependencies of the Primary Key (i.e. the Foreign Key constraints in different table, which are based on this Primary Key) and then will drop this Primary key in a single step.

Page 11: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

TRUNCATE Command

• The SQL TRUNCATE TABLE command is used to delete complete data from an existing table but structure remains.

• Syntax: TRUNCATE TABLE table_name;• DROP TABLE command delete complete table

and remove complete table structure from the database, while TRUNCATE TABLE do not effect table structure .

Page 12: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

COMMENT• Use the COMMENT statement to add a comment

about a table, view or column into the data dictionary.

• Example to add comment on table employee: COMMENT ON TABLE employees IS 'abbreviated

job title';

Page 13: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

COMMENT

• Example To insert an explanatory remark on the job_id column of the employees table, issue the following statement:

COMMENT ON COLUMN employees.job_id IS 'abbreviated job title';

• To drop this comment from the database, issue the following statement:

COMMENT ON COLUMN employees.job_id IS ' ';

Page 14: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

RENAME

RENAME TABLE:RENAME TABLE tbl_name TO new_tbl_name;Where tbl_name is table that exists in the current database, and new_tbl_name is new table name.

RENAME COLUMN: ALTER TABLE tablename RENAME COLUMN OldName TO NewNam

Page 15: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Data Manipulation Language (DML)

• Data Manipulation Language (DML):- Data Manipulation Language contains the commands, which are used to manipulate the data in the tables. These commands include INSERT, UPDATE, DELETE, and SELECT.

Page 16: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

INSERT CommandINSERT Command:- This command is used to insert rows into table. The basic syntax of this command is as given INSERT INTO <tablename> (Column1, Column2,…. Column n )

VALUES (Value1, Value2…., Value n) For example ,

INSERT INTO emp(empno, ename, job, sal, hiredate, deptno)

VALUES(‘1’,’neha’, ‘student’, ‘34566’, 12-jul-2013,’cse’);

This statement will add a new record in the emp table.

Page 17: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

INSERT Command• When data is not to be entered into every column

in the table, then either enter NULL corresponding to all those columns which do not require the value or specify only those columns which require a value.

Example: INSERT INTO emp(empno, ename, job, sal, hiredate, deptno) VALUES (‘1’,’neha’, ‘NULL’, ‘34566’, 12-jul-2013,’NULL’);

INSERT INTO emp(empno, ename, sal, hiredate) VALUES (‘1’,’neha’, ‘34566’, 12-jul-2013);

Page 18: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

INSERT Command• One more style for INSERT command is

without specifying column names as given in the following:

INSERT INTO emp VALUES (‘1’, ’neha’, ‘student’, ‘34566’, 12-jul-2013,’cse’);

• In this case, values must be in the same sequence as specified at the table creation time.

Page 19: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Changing Table ContentsTo change the value of a column or a group of columns in a table corresponding to some search criteria, UPDATE command is used.

Update can be used for:•All the rows from a table.•A select set of rows from a table.

Page 20: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Update CommandThe general syntax of this command is as:UPDATE <tablename> SET <columnname1> = <newvalue1>, <columnname2> = <newvalue2> WHERE <search criteria>

For example to update the salary of king to 6000 in emp table, the statement is:UPDATE emp SET sal = 6000 WHERE ename=

‘king’;

Page 21: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Update Command

• Ex2: Update the Net_sal column in emp table.• Update emp set Net_sal = Net_sal + basic_sal*0.15;

Page 22: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Deleting records from the tableRecords from the table can be deleted individually or in groups by using DELETE Command. Delete can be used to:•Delete all rows from a table.•A select set of rows from a table.The general syntax is :DELETE FROM <tablename> WHERE <search condition> For example:

DELETE FROM emp WHERE deptno=10; In absence of WHERE Clause, this syntax will delete all the records from the table. Ex: DELETE FROM emp;

Page 23: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Delete Command

The subqueries can also be used in DELETE Command for example, if we have to delete all the records from Accounts Department then the syntax will be as follows:

DELETE FROM emp where deptno IN (SELECT deptno from dept WHERE dname= ‘Accounts’);

Page 24: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

SQL SelectTo view the global table data.To view filtered table data• Selected column and all rows• Selected rows and all columns• Selected columns and selected rows.

Page 25: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

To view the global table data

Syntax:Select * from table_name;

Select column1, column2………column n from table_name;

Page 26: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

To view filtered table data

Selected column and all rows Select column1, column2 from table_name;

Selected rows and all columnsSelect *From tablenameWhere <search condition>

Page 27: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

To view filtered table data

Selected columns and selected rows

Select column1, column2From tablenameWhere <search condition>;

Page 28: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Arithmetic Operators

• Create expressions with number and date data by using arithmetic operators.

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

Page 29: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Relational Operators= Equal to

< or > Less than or greater than

<= Less than or equal to

>= Greater than or equal to

<> Not equal to

Page 30: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Using Arithmetic Operators

SELECT last_name, salary, salary + 300FROM employees;

Page 31: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Operator Precedence

SELECT last_name, salary, 12*salary+100FROM employees;

1

SELECT last_name, salary, 12*(salary+100)FROM employees;

2

Page 32: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Select

SELECT last_name, salary, salary + 300 "incremented sal"

FROM employees; Column names will be incremented sal instead

of salary + 300 in previous table.

Page 33: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Defining a Column Alias•A column alias:– Renames a column heading– Is useful with calculations– Immediately follows the column name (There

can also be the optional AS keyword between the column name and alias.)

– Requires double quotation marks if it contains spaces or special characters, or if it is case-sensitive

Page 34: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Using Column Aliases

SELECT last_name "Name" , salary*12 "Annual Salary"FROM employees;

SELECT last_name AS name, commission_pct commFROM employees;

Page 35: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

To eliminate Duplicate Rows•The default display of queries is all rows, including duplicate rows.•Distinct is used for elimination of duplicate rows.

SELECT department_idFROM employees;

SELECT DISTINCT department_idFROM employees;

1 2

Page 36: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Logical OperatorsLogical Operators:• AND• OR• NOT• BETWEEN• LIKE• IN and NOT IN

Page 37: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

AND

• The AND operator joins two or more conditions and displays a row only if that row’s data satisfies ALL conditions listed (i.e. all conditions hold true)

Example: to display all managers earning more than Rs, 3000.

SELECT * FROM emp WHERE desig =’Manager’ AND sal>3000;

Page 38: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

OR• The OR operator joins two or more conditions, but

return a rows if ANY of the conditions listed is true. Project all those rows in which salary is less than

3000 or department number is 10: SELECT * FROM emp WHERE sal<3000

OR deptno=10;AND and OR can be combined, for exampleSELECT * FROM emp WHERE desig = ’Manager’AND sal>3000 OR deptno =10

Page 39: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

NOT• The oracle engine will process all rows in a table

and display result only when none of the conditions specified using NOT operator are satisfied.

• SELECT * FROM emp WHERE NOT(designation =‘Manager’ OR designation=‘Sales Person’);

This will retrieve all the employees who are other than Manager and Sales Person.

Page 40: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

BETWEEN…. ANDTo retrieve the data within a specified range of values, the BETWEEN operator is used. BETWEEN operator can be used with both numeric and character data types.The range coded after the word BETWEEN is inclusive.For example• SELECT * FROM emp WHERE sal BETWEEN 5000

AND 10000;

Page 41: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

NOT BETWEEN…. AND• We can also use NOT BETWEEN to exclude the

above range. Example: SELECT * FROM emp WHERE sal NOT

BETWEEN 5000 AND 10000; In that case it will display those employees who

are getting less than 5000 or more than 10000(exclusive of both the values).

Page 42: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

LIKE (Pattern matching)It allows for a comparison of one string value

with another string value which is not identical. We use two wildcards (% and _ ) for this purpose:

• %: The percent sign matches any string.• _: The underscore matches any single character.

Page 43: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

LIKE (Examples) SELECT * FROM emp WHERE ename LIKE ‘A%’; It gives the details of those employees whose name starts from

character A. Example: Anu

SELECT * FROM emp WHERE ename LIKE ‘%a’; It returns the rows in which the value in ename column ends with

character ‘a’. Example: deepika

SELECT * FROM emp WHERE ename LIKE ‘%r%’; It will display the information about those employees who include ‘r’

in their names. Example: priya

Page 44: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

LIKE (Example)

• SELECT * FROM emp WHERE ename LIKE ‘Taru_’;

• It will display the information of those employees where length of name is just five characters and first four letters include ‘T’ , ‘a’, ’r’, ‘u’ respectively.

Page 45: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

Using the LIKE Condition– You can combine pattern-matching characters:

– You can use the ESCAPE identifier to search for the actual % and _ symbols.

SELECT last_nameFROM employeesWHERE last_name LIKE '_o%' ;

Page 46: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

IN and NOT ININ: To compare a list of values against a column, we have to use IN operator. While, ‘=‘ operator is used to compare a single value to another single value. For example: SELECT * FROM emp WHERE deptno IN (10,20);This syntax will give the information of all those employees who are either in department number 10 or 20.

The equivalent WHERE clause is to this:WHERE deptno =10 or deptno=20;

Page 47: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

NOT IN• Opposite of the ‘IN’ Predicate.• Select all rows where values do not match all

of the values in the list.• SELECT * FROM emp WHERE date_join NOT IN(‘10-JAN-02’, ‘01-JAN-01’, ‘10-

MAR-02’); This will project all rows excluding all the

values in the list.

Page 48: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

The concatenation Operator (||)

• It allows columns to be linked to others columns, arithmetic expressions or constant values to create a character expression. Columns on either side of the operator are combined to make one single column.

• SELECT ename||‘ IS ’||designation||‘ . ’FROM emp;

Page 49: SQL (DDL & DML Commands). Modifying a Table (Alter Command) To change the definition of the table. This can be done by using ALTER TABLE command. This

The concatenation Operator (||)

ENAME||'IS'||DESIG --------------------------------------- Axay IS CEO Ashish IS Manager Sparsh IS Manager Meenal IS Sales Person Tanu IS Sales Person Rahul IS Manager Vivek IS Admin Officer 7 rows selected.