From: http://www.w3schools.com/sql SQL From: http://www.w3schools.com/sql.

Slides:



Advertisements
Similar presentations
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Advertisements

Introduction to Structured Query Language (SQL)
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Introduction to Structured Query Language (SQL)
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Fox MIS Spring 2011 Database Week 5 SQL basics SELECT, INSERT, UPDATE, DELETE.
Lab_03: Basic SQL.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
CHAPTER 9 SQL อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Chapter 11 Database and SQL. Flat Files and Databases Flat files Databases Advantages Efficient use of resources Access control Disadvantages Security.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Rob Gleasure robgleasure.com
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Rob Gleasure robgleasure.com
Introduction to Structured Query Language(SQL)
SQL Implementation & Administration
SQL Tutorial.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
JOINS (Joinining multiple tables)
Introduction To Structured Query Language (SQL)
SQL Queries Chapter No 3.
SQL DATA CONSTRAINTS.
Lesson Plan Instructional Objective Learning Objective
Introduction To Structured Query Language (SQL)
Structured Query Language
PHP and MySQL.
JOINS (Joinining multiple tables)
Presentation transcript:

From: http://www.w3schools.com/sql SQL From: http://www.w3schools.com/sql

SQL It is a standard language for accessing and manipulating databases MySQL, SQL Server, Access, Oracle, Sybase, DB2, and others SQL stands for Structured Query Language SQL is an ANSI (American National Standards Institute) standard There are many variations and different systems have their own extensions But the major commands are same

SQL SQL include The query and update commands for DML Data Manipulation Language (DML) Data Definition Language (DDL) The query and update commands for DML SELECT: extracts data from a database UPDATE: updates data in a database DELETE: deletes data from a database INSERT INTO: insert new data into a database DDL: permits database tables to be created or deleted, define indexes (keys), impose constraints between tables. Some important statements: CREATE DATABASE: creates a new database ALTER DATABASE: modifies a database CREATE TABLE: creates a new table ALTER TABLE: modifies a table DROP TABLE: deletes a table CREATE INDEX: creates an index DROP INDEX: deletes an index

MySQL Serve at SOIC First, please login to IU VPN Groups if you are outside of IU (http://vpn.iu.edu/groups) Then, go to SOIC MySQL Server via phpAdmin (https://phpmyadmin.soic.indiana.edu/soic-phpmyadmin/), Login using your username and password

SOIC MySQL PhPAdmin

SOIC MySQL phpAdmin

Create Persons Table Persons Table P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 3 Pettersen Kari Storgt 20 Stavanger CREATE SCHEMA test; PostgreSQL CREATE DATABASE test; MySQL CREATE TABLE persons ( P_Id INT, LastName VARCHAR(25), FirstName VARCHAR(25), Address VARCHAR(25), City VARCHAR(15), PRIMARY KEY (P_Id) ); INSERT INTO persons VALUES (1, 'Hansen', 'Ola', 'Timoteivn10', 'Sandnes'); INSERT INTO persons VALUES (2, 'Svendson', 'Tove', 'Borgvn23','Sandnes'); INSERT INTO persons VALUES (3, 'Pettersen', 'Kari', 'Storgt20', 'Stavanger'); PostgreSQL/MySQL

SQL Basic

SELECT SELECT is used to select data from a database The result is stored in a result table, called the result-set SQL is not case sensitive SELECT syntax SELECT column_name(s) FROM table_name; SELECT * FROM table_name;

SELECT Persons Table P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 3 Pettersen Kari Storgt 20 Stavanger SELECT FirstName, LastName FROM persons; SELECT *FROM persons;

SELECT DISTINCT SELECT DISTINCT column_name(s) FROM table_name; SELECT DISTINCT city FROM persons;

WHERE clause SELECT column_name(s) FROM table_name WHERE column_name operator value; SELECT * FROM persons WHERE city='Sandnes';

WHERE Clause Text values should be quoted by single quotes or double quotes Numeric values do not need to be enclosed in quotes SELECT * FROM persons WHERE city='Sandnes'; Or WHERE P_Id=1; SELECT * FROM persons WHERE city='Sandnes'; Or WHERE city="Sandnes"; WHERE P_Id=1; PostgreSQL MySQL Double quotes are not used for text values in PostgreSQL

WHERE Clause Description Operator Equal = Not equal <> Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between 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

AND or OR AND, OR operators are used to filter records based on more than one condition AND=both the first and the second conditions is true OR=either the first or the second condition is true

AND or OR SELECT * FROM persons WHERE firstname='Tove' AND lastname='Svendson'; SELECT * FROM persons WHERE firstname='Tove' OR firstname='Ola'; SELECT * FROM persons WHERE lastname='Svendson' AND (firstname='Tove' OR firstname='Ola');

ORDER BY The ORDER BY keyword is used to sort the result-set by a specified column It sorts the records in ascending order by default Use DESC for a descending order SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC;

ORDER BY INSERT INTO persons VALUES (4, 'Nilsen', 'Tom', 'Vingvn23', 'Stavanger'); SELECT * FROM persons ORDER BY lastname; SELECT * FROM persons ORDER BY lastname DESC;

INSERT INTO Use to insert new records in a table INSERT INTO table_name VALUES (value1, value2, value3,…); INSERT INTO table_name (column1, column2, column3, … VALUES (value1, value2, value3,…); INSERT INTO persons VALUES (4, 'Nilsen', 'Tom', 'Vingvn23', 'Stavanger'); INSERT INTO persons (P_Id, lastname, firstname) VALUES (5, 'Tjessem', 'Jakob');

UPDATE Update records in a table UPDATE table_name SET column=value, column2=value2,… WHERE some_column=some_value; UPDATE persons SET Address='Nissestien 67', city='Sandnes' WHERE lastname='Tjessem' AND firstname='Jakob'; Warning: if you forget to add WHERE clause, all the address and city will be set to ‘Nissestien 67’ and ‘Sandnes’.

DELETE statement Used to delete records in a table DELETE FROM table_name WHERE some_column=some_value; DELETE FROM persons WHERE lastname='Tjessem' AND firstname='Jakob'; DELETE FROM table_name; Or DELETE * FROM table_name;

SQL Advanced

LIMIT clause Used to specify the number of records to return SELECT column_name(s) FROM table_name LIMIT number; SELECT * FROM persons LIMIT 2; Oracle, SQL Server: Top number|percent (e.g., Top 2, or Top 50 PERCENT Oracle: ROWNUM<=number (e.g., ROWNUM<=5)

LIKE operator Used in a WHERE clause to search for a specified pattern in a column SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern; SELECT * FROM persons WHERE city LIKE 'S%'; %: define wildcards (missing letters) both before and after the pattern SELECT * FROM persons WHERE city LIKE '%s';

SQL Wildcards SQL Wildcards can substitute for one or more characters when searching for data in a database SQL wildcards must be used with the SQL LIKE operator Wildcard Description % A substitute for zero or more characters _ A substitute for exactly one character

SQL Wildcards SELECT * FROM persons WHERE city LIKE 'Sa%'; SELECT * FROM persons WHERE city LIKE '%nes%'; SELECT * FROM persons WHERE firstname LIKE '_la'; SELECT * FROM persons WHERE lastname LIKE 'S_end_on';

IN operator To specify multiple values in a WHERE clause SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2,…) SELECT * FROM persons WHERE lastname IN ('Hansen', 'Pettersen')

BETWEEN operator Used in a WHERE clause to select a range of data between two values SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value 2; SELECT * FROM persons WHERE lastname BETWEEN 'Hansen' AND 'Pettersen'; Warning: different database systems have different ways of processing BETWEEN operator SELECT * FROM persons WHERE lastname NOT BETWEEN 'Hansen' AND 'Pettersen';

Alias An alias name can be given to a table or a column SELECT column_name(s) FROM table_name AS alias_name; Or SELECT column_name AS alias_name FROM table_name; SELECT po.OrderID, p.LastName, p.FirstName FROM persons AS p, Product_Orders AS po WHERE p.LastName='Hansen' AND p.FirstNam='Ola'; Without alias SELECT Product_Orders.OrderID, persons.LastName, persons.FirstName FROM persons, Product_Orders WHERE persons.LastName= 'Hansen' AND persons.FirstName= 'Ola';

JOIN Used to query data from two or more tables JOIN: return rows when there is at least one match in both tables LEFT JOIN: return all rows from the left table, even if there are no matches in the right table RIGHT JOIN: return all rows from the right table, even if there are no matches in the left table FULL JOIN: return rows where there is a match in one of the tables

JOIN O_Id OrderNo P_Id 1 77895 3 2 44678 22456 4 24562 5 34764 15 CREATE SCHEMA test; PostgreSQL CREATE TABLE test; MySQL CREATE TABLE orders ( O_Id INT, OrderNO INT, P_Id INT, PRIMARY KEY (O_Id) ); INSERT INTO orders VALUES (1, 77895, 3); INSERT INTO orders VALUES (2, 44678, 3); INSERT INTO orders VALUES (3, 22456, 1); INSERT INTO orders VALUES (4, 24562, 1); INSERT INTO orders VALUES (5, 34764, 15); PostgreSQL/MySQL Same as INNER JOIN

INNER JOIN SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name; SELECT persons.lastname, persons.firstname, orders.orderNo From persons INNER JOIN orders ON persons.P_Id=orders.P_Id ORDER BY persons.Lastname; The INNER JOIN keyword returns rows where there is at least one match in both tables. If there are rows in Persons that do not have matches in Orders, those rows will NOT be listed.

LEFT JOIN Returns all rows from the left table, even if there are no matches in the right table SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name; In some databases, LEFT JOIN is called LEFT OUTER JOIN SELECT persons.lastname, persons.firstname, orders.orderno FROM persons LEFT JOIN orders ON persons.P_Id=orders.P_Id ORDER BY persons.lastname;

RIGHT JOIN SELECT column_name(s) Returns all rows from the right table (table 2), even if there are no matches in the left table (table 1) SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name; In some databases, RIGHT JOIN is called RIGHT OUTER JOIN SELECT persons.lastname, persons.firstname, orders.orderno FROM persons RIGHT JOIN orders ON persons.P_Id=orders.P_Id ORDER BY persons.lastname;

FULL JOIN Return rows when there is a match in one of the tables SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name; FULL JOIN is not supported by MySQL. FULL JOIN = LEFT JOIN UNION RIGHT JOIN SELECT persons.lastname, persons.firstname, orders.orderno FROM persons FULL JOIN orders ON persons.P_Id=orders.P_Id ORDER BY persons.lastname; SELECT persons.lastname, persons.firstname, orders.orderno FROM persons LEFT JOIN orders ON persons.P_Id=orders.P_Id UNION FROM persons RIGHT JOIN orders

UNION operator Combines two or more SELECT statements Each SELECT statement must have the same columns (same name, same data types, in the same order) UNION selects only distinct values by default. To allow duplicate values, use UNION ALL. SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2; SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2; SELECT P_Id FROM persons UNION SELECT P_Id FROM orders;

CREATE DATABASE Used to create a database CREATE DATABASE database_name; CREATE SCHEMA my_db;

CREATE TABLE Used to create a table within a database CREATE TABLE table_name (column_name1 data_type, column_name2 data_type, column_name3 data_type, ….); CREATE TABLE persons ( P_Id INT, LastName VARCHAR(25), FirstName VARCHAR(25), Address VARCHAR(25), City VARCHAR(15), PRIMARY KEY (P_Id) );

Constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT Used to limit the type of data that can go into a table Can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement) Type of constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT

NOT NULL Enforce a column to not accept NULL values CREATE TABLE Persons ( P_Id INT NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) );

UNIQUE Uniquely identifies each record in a database table UNIQUE and PRIMARY KEY both provide a guarantee for uniqueness for a column or set of columns A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

UNIQUE CREATE TABLE Persons ( P_Id INT NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) UNIQUE (P_Id) ); CREATE TABLE Persons ( P_Id INT NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ); MySQL SQL Server/Oracle/MS Access CREATE TABLE Persons ( P_Id INT NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) CONSTRAINT uc_PersonID UNIQUE (P_Id, LastName) ); MySQL/SQL Server/Oracle/MS Access

UNIQUE When Persons table has already been created, use Alter to add new constraints. ALTER TABLE Persons ADD UNIQUE (P_Id) ALTER TABLE Persons ADD CONSTRAINT un_PersonID UNIQUE (P_Id, LastName) To drop a UNIQUE constraint ALTER TABLE Persons DROP INDEX un_PersonID MySQL ALTER TABLE Persons DROP CONSTRAINT un_PersonID SQL Server/Oracle/MS Access

PRIMARY KEY Each table should have one and only one primary key Primary key should be unique and does not contain NULL values 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) ); CREATE TABLE Persons ( P_Id INT NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ); SQL Server/Oracle/MS Access MySQL CREATE TABLE Persons ( P_Id INT NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) CONSTRAINT pk_PersonID PRIMARY KEY (P_Id, LastName) ); MySQL/SQL Server/Oracle/MS Access

PRIMARY KEY When Persons table has already been created, use Alter to add new constraints. ALTER TABLE Persons ADD PRIMARY KEY (P_Id) ALTER TABLE Persons ADD CONSTRAINT un_PersonID PRIMARY KEY (P_Id, LastName) To drop a constraint ALTER TABLE Persons DROP PRIMARY KEY MySQL ALTER TABLE Persons DROP CONSTRAINT pk_PersonID SQL Server/Oracle/MS Access

FOREIGN KEY CREATE TABLE Orders ( O_Id INT NOT NULL, A foreign key in one table points to a primary key in another table The foreign key constraint prevents invalid data from being inserted into the foreign key column because it has to be one of the values contained in the table it points to. CREATE TABLE Orders ( O_Id INT NOT NULL, OrderNo INT NOT NULL, P_Id INT, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons (P_Id) ); CREATE TABLE Orders ( O_Id INT NOT NULL PRIMARY KEY, OrderNo INT NOT NULL, P_Id INT FOREIGN KEY REFERENCES Persons(P_Id) ); SQL Server/Oracle/MS Access MySQL CREATE TABLE Orders ( O_Id INT NOT NULL, OrderNo INT NOT NULL, P_Id INT, PRIMARY KEY (O_Id), CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons (P_Id)); MySQL/SQL Server/Oracle/MS Access

FOREIGN KEY When Orders table has already been created, use Alter to add new constraints. ALTER TABLE Orders ADD CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons (P_Id) To drop a constraint MySQL ALTER TABLE Orders DROP FOREIGN KEY fk_PerOrders ALTER TABLE Orders DROP CONSTRAINT fk_PerOrders SQL Server/Oracle/MS Access

CHECK Constraint Used to limit the value range of a column CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (P_Id>0) ) CREATE TABLE Persons ( P_Id int NOT NULL CHECK (P_Id>0), LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) SQL Server/Oracle/MS Access MySQL CREATE TABLE Persons (P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes‘)); MySQL/SQL Server/Oracle/MS Access

CHECK Constraint When a table has already been created, use Alter to add new constraints. ALTER TABLE Persons ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City=‘Sandnes’) ALTER TABLE Persons ADD CHECK (P_Id>0) To drop a constraint SQL Server/Oracle/MS Access ALTER TABLE Persons DROP CONSTRAINT chk_Person

DEFAULT Constraint Used to insert a default value into a column CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' ) CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, OrderDate date DEFAULT GETDATE() ) MySQL/SQL Server/Oracle/MS Access MySQL/SQL Server/Oracle/MS Access

DEFAULT Constrain When a table has already been created, use Alter to add new constraints. ALTER TABLE Persons ALTER COLUMN City SET DEFAULT ‘Sandnes’ ALTER TABLE Persons ALTER City SET DEFAULT ‘Sandnes’ MySQL SQL Server/Oracle/MS Access To drop a constraint MySQL ALTER TABLE Persons ALTER City DROP DEFAULT ALTER TABLE Persons ALTER COLUMN City DROP DEFAULT SQL Server/Oracle/MS Access

CREATE INDEX statement An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes. Indexes can speed up searches/queries Updating a table with indexes takes more time than updating a table without indexes. Because indexes also need to update. So please create indexes on columns (and tables) that will be frequently searched.

CREATE INDEX statement CREATE INDEX index_name ON table_name (column_name) Duplicate values are allowed CREATE UNIQUE INDEX index_name ON table_name (column_name) Duplicate values are not allowed CREATE INDEX PIndex ON Persons (lastname) CREATE INDEX PIndex ON Persons (LastName, FirstName)

SQL DROP INDEX, DROP TABLE, DROP DATABASE MS Access DROP INDEX index_name ON table_name DROP INDEX table_name.index_name MS SQL Server DROP INDEX index_name DB2/Oracle ALTER TABLE table_name DROP INDEX index_name MySQL DROP TABLE table_name DROP DATABASE database_name TRUNCATE TABLE table_name Delete data inside the table, not the table itself

SQL ALTER TABLE statement Alter table statement is used to add, delete, or modify columns in an existing table ALTER TABLE table_name ADD column_name datatype To add a column in a table ALTER TABLE table_name DROP COLUMN column_name To delete a column in a table (some database systems don’t allow deleting a column) ALTER TABLE table_name ALTER COLUMN column_name datatype To change the data type of a column in a table

SQL ALTER TABLE statement ALTER TABLE Persons ADD DateOfBirth date ALTER TABLE Persons DROP COLUMN DateOfBirth

SQL AUTO INCREMENT Auto-increment allows a unique number to be generated when a new record is inserted into a table MySQL CREATE TABLE Persons ( P_Id int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) ) ALTER TABLE Persons AUTO_INCREMENT=100 To start the auto_increment with another value INSERT INTO Persons (FirstName, LastName) VALUES (‘Lars’, ‘Monsen’)

SQL AUTO INCREMENT Auto-increment allows a unique number to be generated when a new record is inserted into a table SQL Server CREATE TABLE Persons ( P_Id int PRIMARY KEY IDENTITY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) ALTER TABLE Persons AUTO_INCREMENT=100 To start the auto_increment with another value INSERT INTO Persons (FirstName, LastName) VALUES (‘Lars’, ‘Monsen’) To start at 10 and increment by 5 IDENTITY(10,5)

SQL AUTO INCREMENT Auto-increment allows a unique number to be generated when a new record is inserted into a table Access CREATE TABLE Persons ( P_Id PRIMARY KEY AUTOINCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) INSERT INTO Persons (FirstName, LastName) VALUES (‘Lars’, ‘Monsen’) To start at 10 and increment by 5 AUTOINCREMENT(10,5)

SQL AUTO INCREMENT Auto-increment allows a unique number to be generated when a new record is inserted into a table Oracle CREATE SEQUENCE seq_person MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10 INSERT INTO Persons (P_Id,FirstName,LastName) VALUES (seq_person.nextval,'Lars','Monsen') To start at 10 and increment by 5 AUTOINCREMENT(10,5)

SQL Views A view is a virtual table based on the result-set of an SQL statement A view just looks like a real table with fields and records from one or more real tables in the database CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition CREATE VIEW Sandnes AS SELECT LastName, FirstName FROM Persons WHERE city=“Sandnes” CREATE VIEW SandnesLastName AS SELECT LastName FROM Sandnes SELECT * FROM SandnesLastName SELECT * FROM Sandnes

SQL Views A view can be updated or dropped CREATE OR REPLACE OR ALTER VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition ALTER VIEW Sandnes AS SELECT LastName, FirstName, Address FROM Persons WHERE city=“Sandnes” DROP VIEW view_name SELECT * FROM Sandnes DROP VIEW Sandnes

SQL Quiz http://www.w3schools.com/sql/sql_quiz.asp