Sie sind auf Seite 1von 60

Introduction to programming II Programming languages A language in normal human understanding is a means of communication between two people or parties.

In computer technology a language are set programs that are used to accomplish a task. Classification of languages a. Machine languages Consists of strings of digits or numbness that instructs the computer to perform a certain operation. b. Assembly language Close machine language other than human language. They are symbolic representation of machine code. c. High level language It is close to human language or syntax. Allows a problem to be specified in a human understandable and oriented manner. The grammer of high level language is close to human vocabulary. d. 4th generation language They are languages that allow the user of a program to query a database. The syntax of 4th generation languages is natural and user friendly. Uses menus to prompt a non-specialist to retrieve data with easy. e. 5th generation languages These are languages that will allow program developed to mimic and copy human characters. Most of them are still under development. E.g. Expert systems and Artificial intelligence. Programming techniques a. Procedural programming It combines returning sequences of statements into one program. A procedure call is used to invoke the procedure to return one or more values.

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

b. Modular programming The procedures of common functionalities are grouped together into separate modules. The program is then divided into several smaller modules which interact through procedure calls and form a whole program. Advantages of modular programming Modular program are easier to code and debug. Reduces the programming size. Code can be reused in other programs. Problem can be isolated to specific module so easier to find the error and correct it.

c. Unstructured programming A programming technique whereby statements in a program have a specified arrangements of statements. A global statement can be called from any part of a program. d. Structured programming Statements in a program are arranged and executed in a specified order. One statement after another are translated to machine code. e. Object oriented programming A technique where objects communicate with one another while dont know how they are implemented. f. Event driven programming A technique where programs written respond to events. g. Pseudo code This is an artificial and informal language that helps programmers to develop algorithms. Pseudo code programs are not executed in the computer. They help a programmer to think out a problem/program before attempting to write on a computer programming language. h. Monolithic programming 2
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Monolithic programming will not divide the program and it is a single thread of execution. When the program size increases it leads inconvenience and difficult to maintain.

Disadvantages of monolithic programming Difficult to check error on large programs. Difficult to maintain. Code can be specific to a particular problem. i.e. it can not be reused.

i. Jackson structured programming (JSP) Jackson Structured Programming or JSP is a method for structured programming based on correspondences between data stream structure and program structure. The method is closely related in concept to creating a flowchart for a regular expression that describes the data stream structure, but tries to build a program structure that matches more than one data stream and provides guidance and techniques to compensate the limited look ahead and the clashes between the structures of the different data streams. j. Bottom up programming It refers to a style of programming where an application is constructed starting with existing primitives of the programming language, and constructing gradually more and more complicated features, until the all of the application has been written. A bottom-up approach is the piecing together of systems to give rise to grander systems, thus making the original systems sub-systems of the emergent system. In a bottom-up approach the individual base elements of the system are first specified in great detail. These elements are then linked together to form larger subsystems, which then in turn are linked, sometimes in many levels, until a complete top-level system is formed. This strategy often resembles a seed model, whereby the beginnings are small but eventually grow in complexity and completeness. k. Top down programming

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

It refers to a style of programming where an application is constructed starting with a high-level description of what it is supposed to do, and breaking the specification down into simpler and simpler pieces, until a level has been reached that corresponds to the primitives of the programming language to be used. A top-down approach (is also known as step-wise design) is essentially the breaking down of a system to gain insight into its compositional subsystems. In a top-down approach an overview of the system is formulated, specifying but not detailing any first-level subsystems. Each subsystem is then refined in yet greater detail, sometimes in many additional subsystem levels, until the entire specification is reduced to base elements. l. Algorithms This is a step by step process of solving a problem by executing a series of actions in a specific order. A procedure of solving a problem in terms of actions to be executed and the order in which they are to be executed. Algorithms writing tools a. Flowcharts b. Pseudo code c. Decisions tables d. Decision trees e. Jackson structured programming f. Dataflow diagrams etc Writing an algorithms Example Design an algorithm that will be used to code the sum of two numbers. Factors to consider when selecting a programming language a. User friendly. b. Simple. c. Easy to learn and understand. Factors affecting program design 4
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

a. Reliability b. Flexibility c. Simplicity.

Introduction to SQL SQL is a standard computer language for accessing and manipulating databases. Types of databases 1. 2. 3. 4. 5. 6. 7. SQL Access Oracle Sybase, SQL Server DB2 Access

SQL Database Tables A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data. Below is an example of a table called "Persons": LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

The table above contains three records (one for each person) and four columns (LastName, FirstName, Address, and City). SQL Queries With SQL, we can query a database and have a result set returned. 5
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

A query like this: SELECT LastName FROM Persons Gives a result set like this: LastName Hansen Svendson Pettersen

SQL Data Manipulation Language (DML) SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes syntax to update, insert, and delete records. These query and update commands together form the Data Manipulation Language (DML) part of SQL:

SELECT - extracts data from a database table UPDATE - updates data in a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into a database table

SQL Data Definition Language (DDL) The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are:

CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index

Data types Data Type 6 Description


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

integer(size) int(size) smallint(size) tinyint(size) decimal(size,d) numeric(size,d)

Hold integers only. The maximum number of digits are specified in parenthesis.

Hold numbers with fractions. The maximum number of digits are specified in "size". The maximum number of digits to the right of the decimal is specified in "d". Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis.

char(size)

varchar(size)

date(yyyymmdd) Holds a date

Create a Database To create a database: CREATE DATABASE database_name Create database GIT; Create a Table To create a table in a database: CREATE TABLE tablename ( columnname1 datatype, columnname2 datatype, ....... ); Example This example demonstrates how you can create a table named Person, with four columns. The column names will be LastName, FirstName, Address, and Age:

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

CREATE TABLE Person ( LastName varchar, FirstName varchar, Address varchar, Age int ) This example demonstrates how you can specify a maximum length for some columns: CREATE TABLE Person ( LastName varchar(30), FirstName varchar, Address varchar, Age int(3) );

The data type specifies what type of data the column can hold. The table below contains the most common data types in SQL: The INSERT INTO Statement The INSERT INTO statement is used to insert new rows into a table. Syntax INSERT INTO tablename VALUES (value1, value2,.); You can also specify the columns for which you want to insert data: INSERT INTO tablename (column1, column2...) VALUES (value1, value2 ...) Insert a New Row This Persons table: LastName Petersen FirstName Kari 8 Address Storgt 20 City Stavanger

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

And this SQL statement: INSERT INTO Persons VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes') Will give this result: LastName Pettersen Hetland FirstName Kari Camilla Address Storgt 20 Hagabakka 24 City Stavanger Sandnes

Insert Data in Specified Columns This Persons table: LastName Pettersen Hetland FirstName Kari Camilla Address Storgt 20 Hagabakka 24 City Stavanger Sandnes

And This SQL statement: INSERT INTO Persons (LastName, Address) VALUES ('Rasmussen', 'Storgt 67') Will give this result: LastName Pettersen Hetland Rasmussen FirstName Kari Camilla 9 Address Storgt 20 Hagabakka 24 Storgt 67 City Stavanger Sandnes

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

The SQL SELECT Statement The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set) Syntax SELECT columnname(s) FROM tablename;

SQL SELECT Example To select the content of columns named "LastName" and "FirstName", from the database table called "Persons", use a SELECT statement like this: SELECT LastName, FirstName FROM Persons;

The database table Persons: LastName Hansen Svendson Pettersen The result LastName Hansen Svendson Pettersen FirstName Ola Tove Kari FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

10

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Select All Columns To select all columns from the "Persons" table, use a * symbol instead of column names, like this: SELECT * FROM Persons; Result LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

The Result Set The result from a SQL query is stored in a result-set. Most database software systems allow navigation of the result set with programming functions, like: Move-To-FirstRecord, Get-Record-Content, Move-To-Next-Record, etc. Semicolon after SQL Statements Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

Some SQL tutorials end each SQL statement with a semicolon. Is this necessary? We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL statement, but some database programs force you to use it. The SELECT DISTINCT Statement The DISTINCT keyword is used to return only distinct (different) values. The SELECT statement returns information from table columns. But what if we only want to select distinct elements? With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement: 11
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Syntax SELECT DISTINCT columnname(s) FROM tablename; Using the DISTINCT keyword To select ALL values from the column named Company we use a SELECT statement like this: SELECT Company FROM Orders Orders table Company Yu Safaricom Orange Safaricom Result Company Yu Zain Orange Safaricom OrderNumber 3412 2312 4678 6798

To select only DIFFERENT values from the column named Company we use a SELECT DISTINCT statement like this: SELECT DISTINCT Company FROM Orders; Result Company Yu Safaricom Zain 12
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SQL WHERE Clause The WHERE clause is used to specify a selection criterion. The WHERE Clause To conditionally select data from a table, a WHERE clause can be added to the SELECT statement. Syntax SELECT column FROM table WHERE column operator value; With the WHERE clause, the following operators can be used: Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEENBetween an inclusive range LIKE Search for a pattern IN If you know the exact value you want to return for at least one of the columns

Using the WHERE Clause To select only the persons living in the city Sandnes, we add a WHERE clause to the SELECT statement: SELECT * FROM Persons WHERE City='Sandnes'; 13
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Persons table LastName Hansen Svendson Svendson Pettersen Result LastName Hansen Svendson Svendson Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values (most database systems will also accept double quotes). Numeric values should not be enclosed in quotes. For text values: This is correct: SELECT * FROM Persons WHERE FirstName='Tove' This is wrong: SELECT * FROM Persons WHERE FirstName=Tove; FirstName Ola Tove Stale Address Timoteivn 10 Borgvn 23 Kaivn 18 City Sandnes Sandnes Sandnes Year 1951 1978 1980 FirstName Ola Tove Stale Kari Address Timoteivn 10 Borgvn 23 Kaivn 18 Storgt 20 City Sandnes Sandnes Sandnes Stavanger Year 1951 1978 1980 1960

For numeric values: This is correct: SELECT * FROM Persons WHERE Year>1965; 14
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

This is wrong: SELECT * FROM Persons WHERE Year>'1965'; The LIKE Condition The LIKE condition is used to specify a search for a pattern in a column. Syntax SELECT column FROM table WHERE column LIKE pattern; A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern. Using LIKE The following SQL statement will return persons with first names that start with an 'O': SELECT * FROM Persons WHERE FirstName LIKE 'O%'; The following SQL statement will return persons with first names that end with an 'a': SELECT * FROM Persons WHERE FirstName LIKE '%a' The following SQL statement will return persons with first names that contain the pattern 'la': SELECT * FROM Persons WHERE FirstName LIKE '%la%';

SQL AND & OR 15


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

AND & OR AND & OR join two or more conditions in a WHERE clause. The AND operator displays a row if ALL conditions listed are true. The OR operator displays a row if ANY of the conditions listed are true. Original Table LastName Hansen Svendson Svendson Example Use AND to display each person with the first name equal to Tove, and the last name equal to Svendson: SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson'; Result: LastName Svendson Example Use OR to display each person with the first name equal to Tove, or the last name equal to Svendson: SELECT * FROM Persons WHERE firstname='Tove' OR lastname='Svendson'; FirstName Tove Address Borgvn 23 City Sandnes FirstName Ola Tove Stephen Address Timoteivn 10 Borgvn 23 Kaivn 18 City Sandnes Sandnes Sandnes

Result: 16
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

LastName Svendson Svendson Example

FirstName Tove Stephen

Address Borgvn 23 Kaivn 18

City Sandnes Sandnes

You can also combine AND and OR (use parentheses to form complex expressions): SELECT * FROM Persons WHERE (FirstName='Tove' OR FirstName='Stephen') AND LastName='Svendson'; Result LastName Svendson Svendson FirstName Tove Stephen Address Borgvn 23 Kaivn 18 City Sandnes Sandnes

SQL IN IN The IN operator may be used if you know the exact value you want to return for at least one of the columns. SELECT columnname FROM tablename WHERE columnname IN (value1,value2,..); Original Table (used in the examples) LastName Hansen Nordmann Pettersen Svendson FirstName Ola Anna Kari Tove Address Timoteivn 10 Neset 18 Storgt 20 Borgvn 23 City Sandnes Sandnes Stavanger Sandnes

17

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Example 1 To display the persons with LastName equal to Hansen or Pettersen, use the following SQL: SELECT * FROM Persons WHERE LastName IN ('Hansen','Pettersen') Result LastName Hansen Pettersen SQL BETWEEN BETWEEN ... AND The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers, text, or dates. SELECT columnname FROM tablename WHERE columnname BETWEEN value1 AND value2; Original Table (used in the examples) LastName Hansen Nordmann Pettersen Svendson Example 1 To display the persons alphabetically between (and including) Hansen and exclusive Pettersen, use the following SQL: SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'; FirstName Ola Anna Kari Tove Address Timoteivn 10 Neset 18 Storgt 20 Borgvn 23 City Sandnes Sandnes Stavanger Sandnes FirstName Ola Kari Address Timoteivn 10 Storgt 20 City Sandnes Stavanger

18

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Result LastName Hansen Nordmann Example 2 To display the persons outside the range used in the previous example, use the NOT operator: SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Hansen' AND 'Pettersen'; Result: LastName Pettersen Svendson FirstName Kari Tove Address Storgt 20 Borgvn 23 City Stavanger Sandnes FirstName Ola Anna Address Timoteivn 10 Neset 18 City Sandnes Sandnes

The Update Statement The UPDATE statement is used to modify the data in a table. Syntax UPDATE tablename SET columnname = newvalue WHERE columnname = somevalue;

Person: LastName Nilsen Rasmussen FirstName Fred Address Kirkegt 56 Storgt 67 City Stavanger

Update one Column in a Row 19


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

We want to add a first name to the person with a last name of "Rasmussen": UPDATE Person SET FirstName = 'Nina' WHERE LastName = 'Rasmussen';

Result: LastName Nilsen Rasmussen FirstName Fred Nina Address Kirkegt 56 Storgt 67 City Stavanger

Update several Columns in a Row We want to change the address and add the name of the city: UPDATE Person SET Address = 'Stien 12', City = 'Stavanger' WHERE LastName = 'Rasmussen'; Result LastName Nilsen Rasmussen FirstName Fred Nina Address Kirkegt 56 Stien 12 City Stavanger Stavanger

SQL ALTER TABLE ALTER TABLE The ALTER TABLE statement is used to add or drop columns in an existing table. ALTER TABLE tablename ADD columnname datatype;

ALTER TABLE tablename DROP COLUMN columnname;

20

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Some database systems don't allow the dropping of a column in a database table (DROP COLUMN columnname).

Person: LastName Pettersen Example To add a column named City in the Person table: ALTER TABLE Person ADD City varchar(30); FirstName Kari Address Storgt 20

Result: LastName Pettersen Example To drop the Address column in the Person table: ALTER TABLE Person DROP COLUMN Address Result: LastName Pettersen The DELETE Statement The DELETE statement is used to delete rows in a table. 21
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

FirstName Kari

Address Storgt 20

City

FirstName Kari

City

Syntax DELETE FROM tablename WHERE columnname = somevalue; Person LastName Nilsen Rasmussen FirstName Fred Nina Address Kirkegt 56 Stien 12 City Stavanger Stavanger

Delete a Row Nina Rasmussen is going to be deleted: DELETE FROM Person WHERE LastName = 'Rasmussen' Result LastName Nilsen Delete All Rows It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact: DELETE FROM tablename; or DELETE * FROM tablename; SQL sorting The ORDER BY keyword is used to sort the result. Sort the Rows The ORDER BY clause is used to sort the rows. Orders FirstName Fred Address Kirkegt 56 City Stavanger

22

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Company Orange Airtel Zain Safaricom Example

OrderNumber 3412 5678 6798 2312

To display the company names in alphabetical order: SELECT Company, OrderNumber FROM Orders ORDER BY Company;

Result Company Orange Airtel Zain Safaricom Example To display the company names in alphabetical order AND the OrderNumber in numerical order: SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber; Result Company Orange Airtel Zain Safaricom Example 23
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

OrderNumber 5678 3412 6798 2312

OrderNumber 5678 3412 2312 6798

To display the company names in reverse alphabetical order: SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC Result: Company Zain Zain Sega ABC Shop OrderNumber 6798 2312 3412 5678

Example To display the company names in reverse alphabetical order AND the OrderNumber in numerical order: SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC Result: Company Zain Zain Sega ABC Shop OrderNumber 2312 6798 3412 5678

Notice that there are two equal company names in the result above. The only time you will see the second column in ASC order would be when there are duplicated values in the first sort column, or a handful of nulls. SQL Alias With SQL, aliases can be used for column names and table names. 24
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Column Name Alias The syntax is: SELECT column AS columnalias FROM table; Table Name Alias The syntax is: SELECT column FROM table AS tablealias;

Example: Using a Column Alias This table (Persons): LastName Hansen Svendson Pettersen And this SQL: SELECT LastName AS Family, FirstName AS Name FROM Persons; Returns this result: Family Hansen Svendson Pettersen Name Ola Tove Kari 25
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

FirstName Ola Tove Kari

Address Timoteivn 10 Borgvn 23 Storgt 20

City Sandnes Sandnes Stavanger

Example: Using a Table Alias This table (Persons): LastName Hansen Svendson Pettersen And this SQL: SELECT LastName, FirstName FROM Persons AS Employees; Returns this result: Table Employees: LastName Hansen Svendson Pettersen FirstName Ola Tove Kari FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

SQL JOIN Joins and Keys Sometimes we have to select data from two or more tables to make our result complete. We have to perform a join. Tables in a database can be related to each other with keys. A primary key is a column with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table. In the Employees table below, the EmployeeID column is the primary key, meaning that no two rows can have the same EmployeeID. The EmployeeID distinguishes two persons even if they have the same name. When you look at the example tables below, notice that: 26
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

The EmployeeID column is the primary key of the Employees table The ProdID column is the primary key of the Orders table The EmployeeID column in the Orders table is used to refer to the persons in the Employees table without using their names

Employees EmployeeID 01 02 03 04 Orders ProdID 234 657 865 Product Printer Table Chair EmployeeID 01 03 03 Name Hansen, Ola Svendson, Tove Svendson, Stephen Pettersen, Kari

Referring to Two Tables We can select data from two tables by referring to two tables Example Who has ordered a product, and what did they order? SELECT Employees.Name, Orders.Product FROM Employees, Orders WHERE Employees.EmployeeID=Orders.EmployeeID; Result Name Product 27
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Hansen, Ola Svendson, Stephen Svendson, Stephen Example

Printer Table Chair

SELECT Employees.Name FROM Employees, Orders WHERE Employees.EmployeeID=Orders.EmployeeID AND Orders.Product='Printer'; Result Name Hansen, Ola Using Joins INNER JOIN The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed.

Syntax SELECT field1, field2, field3 FROM firsttable INNER JOIN secondtable ON firsttable.keyfield = secondtable.foreignkeyfield; Example SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders 28
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

ON Employees.EmployeeID=Orders.EmployeeID; Result Name Hansen, Ola Svendson, Stephen Svendson, Stephen LEFT JOIN The LEFT JOIN returns all the rows from the first table (Employees), even if there are no matches in the second table (Orders). If there are rows in Employees that do not have matches in Orders, those rows also will be listed. Syntax SELECT field1, field2, field3 FROM firsttable LEFT JOIN secondtable ON firsttable.keyfield = secondtable.foreignkeyfield; Example SELECT Employees.Name, Orders.Product FROM Employees LEFT JOIN Orders ON Employees.EmployeeID=Orders.EmployeeID; Product Printer Table Chair

Result

Name Hansen, Ola Svendson, Tove

Product Printer

29

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Svendson, Stephen Svendson, Stephen Pettersen, Kari RIGHT JOIN

Table Chair

The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table (Employees). If there had been any rows in Orders that did not have matches in Employees, those rows also would have been listed. Syntax SELECT field1, field2, field3 FROM firsttable RIGHT JOIN secondtable ON firsttable.keyfield = secondtable.foreignkeyfield; Example SELECT Employees.Name, Orders.Product FROM Employees RIGHT JOIN Orders ON Employees.EmployeeID=Orders.EmployeeID; Result

Name Hansen, Ola Svendson, Stephen Svendson, Stephen

Product Printer Table Chair

Example 30
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SELECT Employees.Name FROM Employees INNER JOIN Orders ON Employees.EmployeeID=Orders.EmployeeID WHERE Orders.Product = 'Printer'; Result Name Hansen, Ola SQL UNION and UNION ALL UNION The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. SQL Statement 1 UNION SQL Statement 2; Employees1 EmpID 1 2 3 4 5 EmpName John Emilly James Mary Johnson

Employees2 EmpID 1 2 3 4 5 EmpName Daniel Donny Kenny Smith Faith

31

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Using the UNION Command Example SELECT EmpName FROM Employees1 UNION SELECT EmpName FROM Employees2; This command cannot be used to list all employees in Norway and USA. In the example above we have two employees with equal names, and only one of them is listed. The UNION command only selects distinct values. UNION ALL The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values. SQL Statement 1 UNION ALL SQL Statement 2; Using the UNION ALL Command Example SELECT EmpName FROM Employees1 UNION ALL SELECT EmpName FROM Employees2; Create Index Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes; they are just used to speed up queries. Updating a table containing indexes takes more time than updating a table without, this is because the indexes also need an update. So, it is a good idea to create indexes only on columns that are often used for a search. A Unique Index

32

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Creates a unique index on a table. A unique index means that two rows cannot have the same index value.

CREATE UNIQUE INDEX indexname ON tablename (columnname); The columnname specifies the column you want indexed. A Simple Index Creates a simple index on a table. When the UNIQUE keyword is omitted, duplicate values are allowed. CREATE INDEX indexname ON tablename (columnname) The columnname specifies the column you want indexed. Example This example creates a simple index, named PersonIndex, on the LastName field of the Person table: CREATE INDEX PersonIndex ON Person (LastName); If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name. CREATE INDEX PersonIndex ON Person (LastName DESC); If you want to index more than one column you can list the column names within the parentheses, separated by commas. CREATE INDEX PersonIndex ON Person (LastName, FirstName);

SQL Drop Index, Table and Database 33


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Drop Index You can delete an existing index in a table with the DROP INDEX statement.

Syntax for MySQL: ALTER TABLE tablename DROP INDEX indexname; Delete a Table or Database To delete a table (the table structure attributes, and indexes will also be deleted): DROP TABLE tablename;

To delete a database DROP DATABASE databasename; Truncate a Table What if we only want to get rid of the data inside a table, and not the table itself? Use the TRUNCATE TABLE command (deletes only the data inside the table): TRUNCATE TABLE tablename;

SQL Functions SQL has many built-in functions for performing calculations on data. SQL Aggregate Functions SQL aggregate functions return a single value, calculated from values in a column. Useful aggregate functions in SQL

AVG( ) - Returns the average value 34


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

COUNT( ) - Returns the number of rows FIRST( ) - Returns the first value in a table column LAST( ) - Returns the last value in a table column MAX( ) - Returns the largest value in a table column MIN( ) - Returns the smallest value in a table column SUM( ) - Returns the total in a table column

SQL Scalar functions SQL scalar functions return a single value, based on the input value. Useful scalar functions

UCASE( ) - Converts a field to upper case LCASE( ) - Converts a field to lower case MID( ) - Extract characters from a text field LEN( ) - Returns the length of a text field ROUND( ) - Rounds a numeric field to the number of decimals specified NOW( ) - Returns the current system date and time FORMAT( ) - Formats how a field is to be displayed TODAY Returns the current system date

SQL AVG () Function The AVG () Function The AVG ( ) function returns the average value of a numeric column. SQL AVG ( ) Syntax SELECT AVG(columnname) FROM tablename; SQL AVG ( ) Example Sales

35

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

OrderId 1 2 3 4 5 6

OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04

OrderPrice 1000 1600 700 300 2000 100

Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

SELECT AVG(OrderPrice) FROM sales; The result-set will look like this: OrderAverage 950 Now we want to find the customers that have an OrderPrice value higher than the average OrderPrice value. We use the following SQL statement: SELECT Customer FROM sales WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Sales) The result-set will look like this: Customer Hansen Nilsen Jensen

SQL COUNT () Function

36

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

The COUNT () function returns the number of rows that matches a specified criteria. SQL COUNT (columnname) Syntax The COUNT (columnname) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(columnname) FROM tablename; SQL COUNT (*) Syntax The COUNT (*) function returns the number of records in a table: SELECT COUNT(*) FROM tablename;

SQL COUNT (DISTINCT columnname) Syntax The COUNT (DISTINCT columnname) function returns the number of distinct values of the specified column: SELECT COUNT(DISTINCT columnname) FROM tablename Note: COUNT (DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.

SQL COUNT(columnname) Example We have the following Sales table: OrderId 1 2 3 4 5 6 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

Now we want to count the number of Sales from "Customer Nilsen". 37


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

We use the following SQL statement: SELECT COUNT(Customer) AS Customer Nilsen FROM Sales WHERE Customer='Nilsen'; The result of the SQL statement above will be 2, because the customer Nilsen has made 2 Sales in total: CustomerNilsen 2

SQL COUNT(*) Example If we omit the WHERE clause, like this: SELECT COUNT(*) AS NumberofSales FROM Sales The result-set will look like this: NumberOfSales 6 SQL COUNT(DISTINCT columnname) Example Now we want to count the number of unique customers in the Sales table. We use the following SQL statement: SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Sales The result-set will look like this: NumberOfCustomers 3 38
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

This is the number of unique customers (Hansen, Nilsen, and Jensen) in the Sales table. SQL FIRST () Function The FIRST () Function The FIRST () function returns the first value of the selected column. SQL FIRST () Syntax SELECT FIRST(columnname) FROM tablename;

SQL FIRST () Example Sales OrderId 1 2 3 4 5 6 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

Now we want to find the first value of the OrderPrice column. We use the following SQL statement: SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Sales Workaround if FIRST () function is not supported: 39

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SELECT OrderPrice FROM Sales ORDER BY orderid LIMIT 1; The result-set will look like this: FirstOrderPrice 1000

SQL LAST () Function The LAST () Function The LAST () function returns the last value of the selected column. SQL LAST () Syntax SELECT LAST(columnname) FROM tablename;

SQL LAST () Example We have the following Sales table: OrderId 1 2 3 4 5 6 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

We use the following SQL statement: SELECT LAST(orderPrice) AS Last orderPrice FROM Sales; Workaround if LAST () function is not supported: 40
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SELECT OrderPrice FROM Sales ORDER BY orderid DESC LIMIT 1; The result-set will look like this: LastOrderPrice 100

SQL MAX() Function The MAX () Function The MAX () function returns the largest value of the selected column. SQL MAX () Syntax SELECT MAX(columnname) FROM tablename;

SQL MAX () Example Sales OrderId 1 2 3 4 5 6 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

Now we want to find the largest value of the OrderPrice column. We use the following SQL statement: 41
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Sales The result-set will look like this: LargestOrderPrice 2000

SQL MIN() Function The MIN() Function The MIN() function returns the smallest value of the selected column. SQL MIN() Syntax SELECT MIN(columnname) FROM tablename;

SQL MIN () Example Sales OrderId 1 2 3 4 5 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 OrderPrice 1000 1600 700 300 2000 Customer Hansen Nilsen Hansen Hansen Jensen

Now we want to find the smallest value of the "OrderPrice" column. 42


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

We use the following SQL statement: SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Sales The result-set will look like this: SmallestOrderPrice 100 The SUM ( ) Function The SUM ( ) function returns the total sum of a numeric column. Syntax SELECT SUM(columnname) FROM tablename;

SQL SUM () Example Sales OrderId 1 2 3 4 5 6 OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 43 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Now we want to find the sum of all OrderPrice fields. We use the following SQL statement: SELECT SUM(OrderPrice) AS OrderTotal FROM Sales The result-set will look like this: OrderTotal 5700

SQL GROUP BY Statement Aggregate functions often need an added GROUP BY statement. The GROUP BY Statement The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

SQL GROUP BY Syntax SELECT columnname, aggregatefunction(columnname) FROM tablename WHERE columnname operator value GROUP BY columnname; SQL GROUP BY Example Sales 44
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

OrderId 1 2 3 4 5 6

OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04

OrderPrice 1000 1600 700 300 2000 100

Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

We will have to use the GROUP BY statement to group the customers. We use the following SQL statement: SELECT Customer,SUM(OrderPrice) FROM Sales GROUP BY Customer The result-set will look like this: Customer Hansen Nilsen Jensen SUM(OrderPrice) 2000 1700 2000

SELECT Customer, SUM(OrderPrice) FROM Sales Result Customer Hansen Nilsen SUM(OrderPrice) 5700 5700 45
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Hansen Hansen Jensen Nilsen

5700 5700 5700 5700

Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM (OrderPrice). The SUM (OrderPrice) returns a single value (that is the total sum of the OrderPrice column), while Customer returns 6 values (one value for each row in the Sales table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem. GROUP BY More Than One Column We can also use the GROUP BY statement on more than one column, like this: SELECT Customer,OrderDate,SUM(OrderPrice) FROM Sales GROUP BY Customer,OrderDate SQL HAVING Clause The HAVING Clause The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. SQL HAVING Syntax SELECT columnname, aggregatefunction(columnname) FROM tablename WHERE columnname operator value GROUP BY columnname HAVING aggregatefunction(columnname) operator value

SQL HAVING Example Sales

46

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

OrderId 1 2 3 4 5 6

OrderDate 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04

OrderPrice 1000 1600 700 300 2000 100

Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

SELECT Customer, SUM(OrderPrice) FROM Sales GROUP BY Customer HAVING SUM(OrderPrice)<2000 Result Customer Nilsen SUM(OrderPrice) 1700

We add an ordinary WHERE clause to the SQL statement: SELECT Customer,SUM(OrderPrice) FROM Sales WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500 Result Customer Hansen Jensen SUM(OrderPrice) 2000 2000

47

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SQL UCASE () Function The UCASE () Function The UCASE () function converts the value of a field to uppercase. SQL UCASE () Syntax SELECT UCASE(columnname) FROM tablename; Syntax for SQL Server SELECT UPPER(columnname) FROM tablename; SQL UCASE () Example We have the following Persons table: PId 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

Now we want to select the content of the LastName and FirstName columns above, and convert the LastName column to uppercase. We use the following SELECT statement: SELECT UCASE(LastName) as LastName,FirstName FROM Persons; The result-set will look like this: LastName HANSEN SVENDSON PETTERSEN FirstName Ola Tove Kari

48

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SQL LCASE () Function The LCASE () Function The LCASE () function converts the value of a field to lowercase. SQL LCASE () Syntax SELECT LCASE(columnname) FROM tablename; Syntax for SQL Server SELECT LOWER(columnname) FROM tablename; SQL LCASE () Example Persons PId 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

Now we want to select the content of the LastName and "FirstName columns above, and convert the LastName column to lowercase. We use the following SELECT statement: SELECT LCASE(LastName) as LastName, FirstName FROM Persons; Result LastName hansen svendson pettersen FirstName Ola Tove Kari

49

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SQL MID () Function The MID () Function The MID () function is used to extract characters from a text field. SQL MID () Syntax SELECT MID(columnname,start[,length]) FROM tablename; Parameter columnname start length Description Required. The field to extract characters from Required. Specifies the starting position (starts at 1) Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text

SQL MID () Example Persons PId 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

Now we want to extract the first four characters of the "City" column above. We use the following SELECT statement: SELECT MID(City,1,4) as SmallCity FROM Persons Result SmallCity Sand Sand 50
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Stav

SQL LEN () Function The LEN () Function The LEN () function returns the length of the value in a text field. SQL LEN () Syntax SELECT LEN(columnname) FROM tablename; SQL LEN () Example Persons PId 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

Now we want to select the length of the values in the Address column above. We use the following SELECT statement: SELECT LEN(Address) as LengthOfAddress FROM Persons; The result-set will look like this: LengthOfAddress 12 9 9 SQL ROUND () Function The ROUND () Function The ROUND () function is used to round a numeric field to the number of decimals specified. 51
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SQL ROUND () Syntax SELECT ROUND(columnname,decimals) FROM tablename; Parameter columnname decimals Description Required. The field to round. Required. Specifies the number of decimals to be returned.

SQL ROUND () Example Products ProdID 1 2 3 ProductName Bread Margarine Milk Unit 1000 g 1000 g 1000 g UnitPrice 10.45 32.56 15.67

SELECT ProductName, ROUND(UnitPrice,0as UnitPrice FROM Products; Result ProductName Bread Margarine Milk SQL NOW () Function The NOW () Function 52
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

UnitPrice 10 33 16

The NOW () function returns the current system date and time. SQL NOW () Syntax SELECT NOW() FROM tablename;

SQL NOW () Example We have the following Products table: ProdID 1 2 3 ProductName Bread Margarine Milk Unit 1000 g 1000 g 1000 g UnitPrice 10.45 32.56 15.67

We use the following SELECT statement: SELECT ProductName, UnitPrice, Now() as PerDate FROM Products; The result-set will look like this: ProductName Bread Margarine Milk UnitPrice 10.45 32.56 15.67 PerDate 10/7/2008 11:25:02 AM 10/7/2008 11:25:02 AM 10/7/2008 11:25:02 AM

SQL FORMAT () Function The FORMAT () Function The FORMAT () function is used to format how a field is to be displayed.

SQL FORMAT () Syntax 53


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SELECT FORMAT(columnname,format) FROM tablename; Parameter columnname format Description Required. The field to be formatted. Required. Specifies the format.

SQL FORMAT () Example We have the following Products table: ProdID 1 2 3 ProductName Bread Margarine Milk Unit 1000 g 1000 g 1000 g UnitPrice 10.45 32.56 15.67

Now we want to display the products and prices per today's date (with today's date displayed in the following format YYYY-MM-DD). We use the following SELECT statement: SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate FROM Products The result-set will look like this: ProductName Bread Margarine Milk UnitPrice 10.45 32.56 15.67 PerDate 2008-10-07 2008-10-07 2008-10-07

SQL PRIMARY KEY Constraint 54


SQL TUTORIAL PREPARED BY: KABA N. DANIEL

SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only ONE primary key.

SQL PRIMARY KEY Constraint on CREATE TABLE The following SQL creates a PRIMARY KEY on the PId column when the Persons table is created: MySQL: CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) ) SQL Server / Oracle / MS Access: CREATE TABLE Persons ( P_Id int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) 55
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns. MySQL / SQL Server / Oracle / MS Access: CREATE TABLE Persons ( empno NOT NULL, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT empno PRIMARY KEY (empno) );

SQL PRIMARY KEY Constraint on ALTER TABLE To create a PRIMARY KEY constraint on the "P_Id" column when the table is already created, use the following SQL: MySQL / SQL Server / Oracle / MS Access: ALTER TABLE Persons ADD PRIMARY KEY (PId) To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: MySQL / SQL Server / Oracle / MS Access: ALTER TABLE Persons ADD CONSTRAINT pkPersonID PRIMARY KEY (P_Id,LastName) Note: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created). To DROP a PRIMARY KEY Constraint To drop a PRIMARY KEY constraint, use the following SQL: 56
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

MySQL: ALTER TABLE Persons DROP PRIMARY KEY SQL Server / Oracle / MS Access: ALTER TABLE Persons

SQL FOREIGN KEY Constraint SQL FOREIGN KEY Constraint A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Let's illustrate the foreign key with an example. Look at the following two tables:

The Persons table: PId 1 2 3 Sales OrderId 1 2 3 4 OrderNo 77895 44678 22456 24562 PId 3 3 2 1 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

Note that the PId column in the "Sales" table points to the "P_Id" column in the "Persons" table. The PId column in the Persons table is the PRIMARY KEY in the Persons table. 57
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

The PId column in the Sales table is a FOREIGN KEY in the Sales table. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents that invalid data form being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.

SQL FOREIGN KEY Constraint on CREATE TABLE The following SQL creates a FOREIGN KEY on the PId column when the Sales table is created:

MySQL: CREATE TABLE Sales ( OrderId int NOT NULL, OrderNo int NOT NULL, PId int, PRIMARY KEY (OrderId), FOREIGN KEY (PId) REFERENCES Persons(PId) ) SQL Server / Oracle / MS Access: CREATE TABLE Sales ( OorderId int NOT NULL PRIMARY KEY, OrderNo int NOT NULL, PId int FOREIGN KEY REFERENCES Persons(PId) ) 58
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: MySQL / SQL Server / Oracle / MS Access: CREATE TABLE Sales ( OrderId int NOT NULL, OrderNo int NOT NULL, PId int, PRIMARY KEY (OrderId), CONSTRAINT fkPerSales FOREIGN KEY (PId) REFERENCES Persons(PId) ) SQL FOREIGN KEY Constraint on ALTER TABLE To create a FOREIGN KEY constraint on the "P_Id" column when the "Sales" table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access: ALTER TABLE Sales ADD FOREIGN KEY (PId) REFERENCES Persons(PId) To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: MySQL / SQL Server / Oracle / MS Access: ALTER TABLE Sales ADD CONSTRAINT fkPerSales FOREIGN KEY (PId) REFERENCES Persons(PId)
SQL TUTORIAL PREPARED BY: KABA N. DANIEL

59

To DROP a FOREIGN KEY Constraint To drop a FOREIGN KEY constraint, use the following SQL: MySQL: ALTER TABLE Sales DROP FOREIGN KEY fkPerSales SQL Server / Oracle / MS Access: ALTER TABLE Sales DROP CONSTRAINT fkPerSales

60

SQL TUTORIAL PREPARED BY: KABA N. DANIEL

Das könnte Ihnen auch gefallen