SQL Commands
SQL Commands
Creating a database:
To connect to a database:
\c databasename;
To delete a database:
Creating table:
SQL Commands 1
To delete complete table:
To erase all the contents from the table, but the structure remains same:
SQL Commands 2
Table after using Delete command
Deleting a column:
SQL Commands 3
ALTER TABLE table_name
DROP COLUMN column_name;
Rename a table
Renaming a column:
Update command:
UPDATE table_name
SET marks=100
WHERE roll_number=25;
Retrieval Queries:
To display Complete-table:
SQL Commands 4
select * from table_name;
To display a column:
Another example:
SQL Commands 5
Display from ceratin conditions:
Emkonni examples:
SQL Commands 6
Very important example:
SQL Commands 7
Distinct Command
SQL Commands 8
Order By Command:
SQL Commands 9
SQL Alias: We can give a table or a column another name by using an alias
SQL Alias Syntax for Columns:
SQL Commands 10
WHERE clause:
Example:
SQL Commands 11
Wildcard Characters
Example:
SQL Commands 12
IN Operator: IN operator is a shorthand for multiple OR conditions.
SQL Commands 13
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example:
SELECT column_name(s)
FROM table_name
SQL Commands 14
WHERE column_name BETWEEN value1 AND value2;
Aggregate Functions:
SQL Commands 15
Group by statement:
Example:
SQL Commands 16
HAVING Clause:
The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
Example:
SQL Commands 17
SQL Commands 18
Summary:
Next session
TCL commands:
COMMIT: Commit command is used to permanently save any transaction into the
database.
COMMIT;
ROLLBACK: This command restores the database to the last committed state. It is
also used with the savepoint command to jump to a save point in a transaction.
ROLLBACK TO savepoint_name;
SQL Commands 19
SAVEPOINT savepoint_name;
Example:
SQL Commands 20
SQL Commands 21
the above code can also be written as:
SQL Commands 22
ALL operator:
??
??
SQL Commands 23
SQL Commands 24
SQL Commands 25