Data Definition Language - Create (database or table) - Alter ( Table) - Drop ( Database or Table) - Rename (Table) DROP – DDL command that allows us to remove entire database objects from the RDMS. This also allows to remove the entire table structure from the database. Syntax: DROP TABLE tablename DROP DATABASE database name Note : Use this command with care!!!! ALTER = command allows you to make changes to the structure of a table without deleteing and creating it. ALTER TABLE Clauses Clause Usage Meaning Example ADD COLUMN ALTER TABLE tablename ADD COLUMN column name datatype() Add new column to the end of the table ALTER TABLE Employee ADD COLUMN Salary int(10) CHANGE COLUMN ALTER TABLE tablename CHANGE COLUMN column name1 column name2 datatype Allows you to change the data type and column name ALTER TABLE Employee CHANGE COLUMN Salary SAL Decimal(10.2) DROP COLUMN ALTER TABLE tablename DROP COLUMN column name Removes column from a table including of its data ALTER TABLE Employee DROP COLUMN Contact ADD INDEX ALTER TABLE tablename ADD INDEX index name (column name) Add new index on column name ALTER TABLE Employee ADD INDEX Sal_Index (SAL)) DROP INDEX ALTER TABLE tablename DROP INDEX index name Removes existing index ALTER TABLE Employee DROP INDEX Sal_Index RENAME AS ALTER TABLE tablename RENAME AS new tablename Change the name of the table ALTER TABLE Employee RENAME AS Emp; NOTE: - ALTER command could have serious repercussions on a table you should always back-up the table before execution. - The word “column” in most ALTER statement is optional - When adding a new column to a table, you can use the “AFTER” command to indicate where in the table the new column should be placed. o ALTER TABLE clients
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
ORDER BY DEPT_ID ASC LIMIT 1; This will show only 1 record which is the least number of
DEPT_ID.
Aggregate Functions
Aggregate functions return a single value based upon a set of other values. If used among many other expressions in the item list of a SELECT statement, the SELECT must have a GROUP BY clause. No GROUP
BY clause is required if the aggregate function is the only value retrieved by the SELECT statement. Thesupported aggregate functions and their syntax are listed in Table 4-1.
Table 4-1: SQL Aggregate Functions
Function Usage
AVG(expression) Computes the average value of a column by the expression
COUNT(expression) Counts the rows defined by the expression
COUNT(*) Counts all rows in the specified table or view
MIN(expression) Finds the minimum value in a column by the expression
MAX(expression) Finds the maximum value in a column by the expression
SUM(expression) Computes the sum of column values by the expression
AVG and SUM
The AVG function computes the average of values in a column or an expression. SUM computes the sum. Bothfunctions work with numeric values and ignore NULL values. They also can be used to compute the average or sum of all distinct values of a column or expression.
AVG and SUM are supported by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL.
Example
The following query computes average year-to-date sales for each type of book:
SELECT type, AVG( ytd_sales ) AS "average_ytd_sales"
FROM titlesGROUP BY type;
This query returns the sum of year-to-date sales for each type of book:
SELECT type, SUM( ytd_sales )FROM titlesGROUP BY type;
The COUNT function has three variations. COUNT(*) counts all the rows in the target table whether theyinclude nulls or not. COUNT(expression) computes the number of rows with non-NULL values in a specificcolumn or expression. COUNT(DISTINCT expression) computes the number of distinct non-NULL values in acolumn or expression.
Examples
This query counts all rows in a table:
SELECT COUNT(*) FROM publishers;
The following query finds the number of different countries where publishers are located:
SELECT COUNT(DISTINCT country) "Count of Countries"FROM publishers
MIN and MAX
MIN(expression) and MAX(expression) find the minimum and maximum value (string, datetime, or numeric) in
a set of rows. DISTINCT or ALL may be used with these functions, but they do not affect the result.
MIN and MAX are supported by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL.
MySQL also supports the functions LEAST( ) and GREATEST( ), providing the same capabilities.
Examples
The following query finds the best and worst sales for any title on record:
Aggregate functions are used often in the HAVING clause of queries with GROUP BY . The following queryselects all categories (types) of books that have an average price for all books in the category higher than$15.00:
SELECT type 'Category', AVG( price ) 'Average Price'FROM titlesGROUP BY typeHAVING AVG(price) > 15
Built-in Scalar Functions
Built-in scalar functions identify the current user session, and also characteristics of the current user session,such as the current session privileges. Built-in scalar functions are almost always nondeterministic. The firstthree functions listed in Table 4-3 are built-in functions that fall into the date-and-time category of functions.Although the four vendors provide many additional functions beyond these SQL built-ins, the SQL standarddeclares only those listed in Table 4-3.
CURRENT_TIMESTAMP Identifies the current date and time.
CURRENT_USER Identifies the currently active user within the database server.
SESSION_USER Identifies the currently active Authorization ID, if it differs from the user.
SYSTEM_USER Identifies the currently active user within the host operating system.
Microsoft SQL Server supports all the built-in scalar functions.
Example
The following queries retrieve the values from built-in functions. Notice that the various vendors return dates intheir native formats:
/* On MySQL */SELECT CURRENT_TIMESTAMP;-> '2009-12-15 23:50:26' /* On Microsoft SQL Server */SELECT CURRENT_TIMESTAMPGO-> 'Dec 15,2009 23:50:26' /* On Oracle */SELECT USER FROM dual;-> dylan
String Functions
Basic string functions offer a number of capabilities and return a string value as a result set. Some stringfunctions are dyadic, indicating that they operate on two strings at once. SQL99 supports the string functionslisted in Table 4-6.
Table 4-6: SQL String Functions
Function Usage
CONCATENATE
(expression ||expression)
Appends two or more literal expressions, column values, or variables together into one
string.
CONVERT Converts a string to a different representation within the same character set.
LOWER Converts a string to all lowercase characters.
SUBSTRING Extracts a portion of a string.
TRANSLATE Converts a string from one character set to another.
TRIM Removes leading characters, trailing characters, or both from a character string.
UPPER Converts a string to all uppercase characters.
The CONCATENATE function appends two or more strings together, producing a single output string.PostgreSQL and Oracle support the double-pipe concatenation operator. Microsoft SQL Server uses the plussign (+) concatenation operator.
MySQL Syntax
CONCAT(str1, str2, [,...n])
If any of the concatenation values are null, the entire returned string is null. Also, if a numeric value isconcatenated, it is implicitly converted to a character string:
SELECT CONCAT('My ', 'bologna ', 'has ', 'a ', 'first ', 'name...');-> 'My bologna has a first name...'SELECT CONCAT('My ', NULL, 'has ', 'first ', 'name...');-> NULL
LOWER and UPPER
The functions LOWER and UPPER allow the case of a string to be altered quickly and easily, so that all thecharacters are lower- or uppercase, respectively. These functions are supported in all the databaseimplementations covered in this book.
Example
SELECT LOWER('You Talkin To ME?'), UPPER('you talking to me?!');-> you talking to me?, YOU TALKIN TO ME?!
The various database vendors also support a variety of other text formatting functions that are specific to their implementation.
SUBSTRING
MySQL Syntax and Variations
SUBSTRING(extraction_string FROM starting_position)
MySQL's implementation assumes that the characters are to be extracted from the starting position continuingto the end of the character string.
The TRIM function removes leading spaces, trailing characters, or both from a specified character string. Thisfunction also removes other types of characters from a specified character string. The default function is to trimthe specified character from both sides of the character string. If no removal string is specified, TRIM removesspaces by default.