18
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.

Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Embed Size (px)

Citation preview

Page 1: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Introduction To SQL

Lynnwood Brown

President System Managers LLC

Copyright System Managers LLC 2003 all rights reserved.

Page 2: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Structured Query Language

• The Structured Query Language is used to communicate with all relational databases.

• Oracle, DB2, Informix, SQL Server, MS Access are some of the more popular databases.

Oracle - 30% market share

DB2 - 30% market share

SQL Server - 15% market share

Copyright System Managers LLC 2003 all rights reserved.

Page 3: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• A database consist of various types of objects.

• One type of database object is called a table.

• Tables store end user information in records.

Copyright System Managers LLC 2003 all rights reserved.

Page 4: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• End users must be able to insert new data into a table.

• End users must be able to update the information in a table.

• End users must be able to remove information from a table.

• End users must be able to look up information that is stored in a table.

Copyright System Managers LLC 2003 all rights reserved.

Page 5: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• To get information from a table the table is queried.

• To get information from a table the SQL command “SELECT” is used to query the table.

Copyright System Managers LLC 2003 all rights reserved.

Page 6: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• To find the name, locations and department numbers from the table called “DEPT” a “SELECT” statement is issued.

• Select deptno,dname,loc from dept;

Copyright System Managers LLC 2003 all rights reserved.

Page 7: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• SQL supports the use of functions to aggregate data.

• Partial list of SQL functions:MIN - Find the minimum value in a column

MAX - Find the maximum value in a column

AVG - Calculate the average value for a column

SQRT - Calculate the square root for the column data

STDDEV - Calculate the standard deviation for a column.

TO_CHAR - Convert data to character format

Copyright System Managers LLC 2003 all rights reserved.

Page 8: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting information

• SQL supports a conditional search using the “WHERE” clause.

• Select * from dept where deptno = 10;

• Select * from dept where loc = ‘DALLAS’;

Copyright System Managers LLC 2003 all rights reserved.

Page 9: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• Data may be stored in various tables.

• Data form multiple tables can be queried using a table join.

• Tables are joined on like columns.

Select a.ename,b.loc from emp a, dept b where a.deptno=b.deptno;

Select a.ename, b.loc from emp a, dept b where a.deptno > b.deptno;

Copyright System Managers LLC 2003 all rights reserved.

Page 10: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Getting Information

• We can use one query as a input for another query. This is called a subquery.

Select * from emp where deptno = select deptno from dept where loc = ‘DALLAS’;

Select * from emp where deptno in (select deptno from dept);

Select * from emp where deptno in (select deptno from dept where loc in (‘DALLAS’,’BOSTON’);

Copyright System Managers LLC 2003 all rights reserved.

Page 11: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Creating A Table

• The SQL command “CREATE TABLE” is used create a table.

CREATE TABLE EMP (EMPNO NUMBER(4) NOT

NULL,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7,2),COMM NUMBER(7,2),DEPTNO NUMBER(2));

Copyright System Managers LLC 2003 all rights reserved.

Page 12: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Modifying A Table

• Columns can be added to an existing table using the SQL command “ALTER TABLE”.

Alter table emp add loc varchar2(20);

• Columns can be enlarged using the “ALTER TABLE” command.

Alter table emp modify loc varchar2(40);

• Columns can be removed from a tableAlter table emp drop loc;

Copyright System Managers LLC 2003 all rights reserved.

Page 13: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Adding New Information

• The SQL command “INSERT” is used to add new records to a table.

INSERT INTO EMP VALUES

(7369,'SMITH','CLERK',7902,'17-DEC-80',800,NULL,20);

Copyright System Managers LLC 2003 all rights reserved.

Page 14: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Updating Information

• The SQL command “UPDATE” is used to modify data that exist in a table record.

update emp set deptno = 5;

update emp set deptno = 5 where ename = ‘SMITH’;

Copyright System Managers LLC 2003 all rights reserved.

Page 15: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Removing Information

• The SQL command “DELETE” is used to remove a record(s) from a table.

Delete from emp;

Truncate table emp;

Delete from emp where ename = ‘SMITH’;

Copyright System Managers LLC 2003 all rights reserved.

Page 16: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Removing A Table

• The SQL command “DROP TABLE” is used to remove a table from the database.

Drop table emp;

• Tables can also be renamed:

rename emp to emp_old;

Copyright System Managers LLC 2003 all rights reserved.

Page 17: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Summary

• SQL is the language used to communicate with a relational database

• SQL provides the commands “SELECT”, “INSERT”, “UPDATE” and “DELETE” to query the database, put new information into the database, update existing data that is in the database and remove data from the database respectively.

Copyright System Managers LLC 2003 all rights reserved.

Page 18: Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved

Summary (cont.)

• SQL provides various mathematical functions (MIN, MAX, STDDEV etc…).

• The command “ALTER TABLE” is used to modify the structure of a table.

• SQL*PLUS is Oracle’s version of ANSI SQL.

Copyright System Managers LLC 2003 all rights reserved.