1) Explain Alter and Create Command With One Example: Syntax
1) Explain Alter and Create Command With One Example: Syntax
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
b) alter-drop column
Syntax:-
->ALTER TABLE table_name
DROP COLUMN column_name;
Example:-
->ALTER TABLE Customers
DROP COLUMN Email;
CREATE Command
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The datatype parameter specifies the type of data the column can hold (e.g. varchar, char,
date,number )
The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:
Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL constraints are used to specify rules for the data in a table.
The following constraints are commonly used in SQL:
•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in
a table
•FOREIGN KEY - Uniquely identifies a row/record in another table
•CHECK - Ensures that all values in a column satisfies a specific condition
•DEFAULT - Sets a default value for a column when no value is specified
•INDEX - Used to create and retrieve data from the database very quickly
Arithmetic Operators
Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides numerator by de-numerator
** Exponentiation operator, raises one operand to
the power of other
Comparison Operators
Operators Description
= Checks if the values of two operands are equal or not, if yes then
condition becomes true.
!= Checks if the values of two operands are equal or not
<> If values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
Logical Operators
If we want to check for more than one condition in a select query then we need to
use Boolean operators. Boolean operators can return only True or False for a given
condition.
Operators Description
AND Called the logical AND operator. If both the operands are true
then condition becomes true.
OR Called the logical OR Operator. If any of the two operands is true
then condition becomes true.
NOT Called the logical NOT Operator. Used to reverse the logical state
of its operand. If a condition is true then Logical NOT operator will
make it false.
4) Explain the difference between group by and sub query with one
example
Group By Sub query
The GROUP BY statement is used with the SQL sub queries are most frequently used
SQL SELECT statement. with the Select statement.
The GROUP BY statement follows the A sub query can be placed in a number of
WHERE clause in a SELECT statement and SQL clauses like WHERE clause, FROM
Precedes the ORDER BY clause. clause,
HAVING clause.
The GROUP BY statement is used with We can use Sub query with SELECT, UPDATE,
aggregation function. INSERT, DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN,
etc.
Example:
Group By: SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
Sub query: SELECT ProductName
FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE
Quantity = 10);
Syntax:
IF condition THEN
statements;
ELSE
else_statements;
END IF;
Example:
DECLARE
Num1 NUMBER;
Num2 NUMBER;
BEGIN
Num1:=12; Num2:=10;
IF Num1<Num2 THEN
DBMS_OUTPUT.PUT_LINE(Num1||’ is less than ‘ ||Num2);
ELSE
DBMS_OUTPUT.PUT_LINE(Num1||’ is greater than ‘||Num2);
END IF;
END;
/
IF-THEN-ELSIF-THEN-ELSE statements
Syntax:
IF condition_1 THEN
statements_1
ELSIF condition_2 THEN
statements_2
[ ELSIF condition_3 THEN
statements_3
]
...
[ ELSE
else_statements
]
END IF;
Example:
DECLARE
percent NUMBER;
BEGIN
percent:=60;
IF percent > 75 THEN
DBMS_OUTPUT.PUT_LINE(‘Excellent’);
ELSIF percent > 50 THEN
DBMS_OUTPUT.PUT_LINE(‘Good’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Poor’);
END IF;
END;
/
Procedure:
It is a sub program that consist of group, of PL/SQL each
procedure in oracle has its own unique name. This is stored as a
database object for future reference.
Syntax:
CREATE [OR REPLACE] PROCEDURE pro_name (paral, para2,
…)
IS
Declare statements
BEGIN
Executable statements
END procedure name;
/
Example:
CREATE OR REPLACE PROCEDURE p1(e in number)
IS
BEGIN
Dbms_output.put_line(‘square is’||e*e);
END;
/
Function:
It is stand alone PL/SQL sub program it is mainly use for
calculation purpose it use keyword to return the value
Syntax
CREATE [OR REPLACE] FUNCTION
function_name(parameter 1, parameter 2…) RETURN
datatype
IS
Declare variable, constant etc.
BEGIN
Executable Statements
Return (Return value);
END;
Example:
CREATE OR REPLACE FUNCTION f1(n in number)
Return varchar2
IS
BEGIN
If(m mod 2=0)then
Return(‘even’);
ELSE
Return(‘odd’);
End if;
END;
/