Professional Documents
Culture Documents
Advanced Database DDL
Advanced Database DDL
Advanced Database DDL
The FIRST() function returns the first value of the selected column.
FirstOrderPrice
1000
The LAST() function returns the last value of the selected column.
SELECT LAST(column_name) FROM table_name
SQL LAST() Example
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Now we want to find the last value of the "OrderPrice" column.
We use the following SQL statement:
SELECT LAST(OrderPrice) AS
LastOrderPrice FROM Orders
LastOrderPrice
100
SQL Concatenate
Result:
'EastBoston'
The HAVING Clause
The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
Now we want to find if any of the customers have a total order of less than
2000.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
The result-set will look like this:
Customer SUM(OrderPrice)
Nilsen 1700
Now we want to find if the customers "Hansen" or "Jensen" have a total
order of more than 1500.
We add an ordinary WHERE clause to the SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
Customer SUM(OrderPrice)
Hansen 2000
Jensen 2000
The GROUP BY Statement
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the
customers.
We use the
SELECT following SQL statement: FROM Orders
Customer,SUM(OrderPrice)
GROUP BY Customer Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
The TOP Clause
The TOP clause is used to specify the number of records to return.
The TOP clause can be very useful on large tables with thousands of
records. Returning a large number of records can impact on
performance.
Note: Not all database systems support the TOP clause.
CREATE DATABASE
my_db
Database tables can be added with the CREATE TABLE
statement.
You can enable this feature interactively with the tee command. Without a
parameter, the previous file is used. The tee file can be disabled with the
notee command. Executing tee again re-enables logging.
The P_Id column is of type int and will hold a number. The LastName,
FirstName, Address, and City columns are of type varchar with a
maximum length of 255 characters.
Constraints are used to limit the type of
SQL NOT NULL Constraint data that can go into a table.
MySQL: MS Access:
CREATE TABLE Persons
CREATE TABLE Persons
(
(
P_Id int NOT NULL,
P_Id int NOT NULL PRIMARY
LastName varchar(255) NOT NULL,
KEY,
FirstName varchar(255),
LastName varchar(255) NOT
Address varchar(255),
NULL,
City varchar(255),
FirstName varchar(255),
PRIMARY KEY (P_Id)
Address varchar(255),
)
City varchar(255)
)
The DROP TABLE Statement
The DROP TABLE statement is used to delete a table.
What if we only want to delete the data inside the table, and not the table
itself?
Then, use the TRUNCATE TABLE statement:
TRUNCATE TABLE table_name
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in
an existing table.
To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
DateOfBirt
P_Id LastName FirstName Address City
h
Timoteivn
1 Hansen Ola Sandnes
10
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Change Data Type Example
Now we want to change the data type of the column named "DateOfBirth"
in the "Persons" table.
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year
Notice that the "DateOfBirth" column is now of type year and is going to
hold a year in a two-digit or four-digit format.
DROP COLUMN Example
Next, we want to delete the column named "DateOfBirth" in the "Persons"
table.
ALTER TABLE Persons
DROP COLUMN DateOfBirth
Syntax:
SELECT * from
View_Name DROP VIEW view_name
A STORED PROCEDURE is a set of SQL statements that can be stored in the
server.
Once this has been done, clients don't need to keep reissuing the individual
statements but can refer to the stored procedure instead.
Stored routines can provide improved performance because less information
needs to be sent between the server and the client. This also helps in
decreasing the traffic in the networks and also reduces the CPU load.
The Delimiter is the character or string of characters that you’ll use to tell
the mySQL client that you’ve finished typing in an SQL statement.
BEGIN and END statements are used to delimit the stored procedure body
DELIMITER $$
DELIMITER ;
DELIMITER ;
CALL procedure_name(); - executes the just-created stored
procedure and displays the returned result.
After they are created, stored procedures remain on the server, ready for use,
until dropped. The drop command removes the stored procedure from the
server.
To remove the stored procedure we just created, use the following statement:
This removes the just-created stored procedure. Notice that the trailing () is not
used; here just the stored procedure name is specified.
Drop Only If It Exists DROP PROCEDURE will throw an error if the named
procedure does not actually exist. To delete a procedure if it exists (and not throw
an error if it does not), use DROP PROCEDURE IF EXISTS.
SHOW CREATE PROCEDURE procedure_name;
To display the CREATE statement used to create a stored procedure, use the
SHOW CREATE PROCEDURE statement:
The LOAD DATA INFILE statement reads rows from a text file into a table
at a very high speed.
Because you are beginning with an empty table, an easy way to
populate it is to create a text file containing a row
For example, the following statements create a table and an INSERT trigger.
The trigger sums the values inserted into one of the table's columns:
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(50));
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48 |
+-----------------------+
trigger_time is the trigger action time. It can be BEFORE or AFTER to
indicate that the trigger activates before or after the statement that
activated it.
The above example will copy all of the information from Table1 into
Table2.
mysqldump — A Database Backup Program
MySQL provides several techniques to keep the backup of our important
data.
1.Open the Command Line utility by Clicking on 'Start' and 'Run' and typing
'CMD' and pressing 'ok'
2.Type 'mysqldump'
3.Type –h and your hostname '-h localhost'
4.Type -u and your username '-u root'
5.Type -p and your password '-p password'
6.Type your database name 'mydatabase'
7.Type a greater-than sign to put the results in to a file '>'
8.Type the name of the file to put the results in 'mybackupfile.sql'
Import SQL dump into MySQL