Lecture 07 - Basic SQL

1,332 views

Published on

Basic SQL

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,332
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
102
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide























































  • Lecture 07 - Basic SQL

    1. 1. Trinity College Basic SQL Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 372
    2. 2. SQL Language • Standard for Relational DBs • Enabled success • Easy to migrate • From one DBMS to another • SQL is the “same” • In practice... • Differences between relational DBMSs do exist! • However, using those features that are part of the standard makes migration easier. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 2
    3. 3. SQL Language • Declarative Language Trinity College, Hartford CT • Department of Computer Science • CPSC 372 3
    4. 4. SQL Language • Declarative Language A user only specifies what the result is to be... Trinity College, Hartford CT • Department of Computer Science • CPSC 372 4
    5. 5. SQL Language • Declarative Language A user only specifies what the result is to be... The database figures out how to retrieve the result! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 5
    6. 6. SQL Language • Declarative Language A user only specifies what the result is to be... The database figures out how to retrieve the result! This allows for greater flexibility in the language and more opportunity for an SQL compiler to optimize queries to achieve increased performance! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 6
    7. 7. SQL Language • SQL: Structured Query Language • Originally called SEQUEL • Structured English Query Language • Designed/Implemented at IBM Research for an experimental DBMS called System R. • SQL is now a standard • American National Standards Institute (ANSI) • International Standards Organization (ISO) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 7
    8. 8. SQL Language • Standards • SQL-86 (SQL1) • SQL-92 (SQL2) • SQL:1999 (SQL3) • SQL:2003 and SQL:2006 (XML support) • SQL:2008 added object database features • Later Standards • Core specification • Plus Extensions (Optional) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 8
    9. 9. SQL Language • Comprehensive DB Language • Data Definition Statements • Updates Statements (Insert, Update, Delete) • Query Statements • Both DDL and DML • Data Definition Language • Data Manipulation Language • SQL is Important and Large Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
    10. 10. SQL Language • Comprehensive DB Language • Data Definition Statements • Updates Statements (Insert, Update, Delete) • Query Statements We’ll start with the • Both DDL and DML basic DDL and DML • Data Definition Language statements • Data Manipulation Language • SQL is Important and Large Trinity College, Hartford CT • Department of Computer Science • CPSC 372 10
    11. 11. Create Schema • A Schema Elements • Tables, Constraints,Views, Domains, and more! • Identifies By: • A schema name • An authorization identifier CREATE SCHEMA COMPANY AUTHORIZATION ‘trichards’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
    12. 12. Catalogs • Catalog: • A named collection of schemas in an SQL environment. • An SQL environment is an installation of an SQL- compliant RDBMS on a computer system. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 12
    13. 13. Create Table • Create Table Command • Specifies a new relation • With a given name • And a set of attributes • Attributes are given names, types, and constraints Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
    14. 14. Create Table CREATE TABLE COMPANY.EMPLOYEE ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Trinity College, Hartford CT • Department of Computer Science • CPSC 372 14
    15. 15. Create Table CREATE TABLE COMPANY.EMPLOYEE ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Not Required Trinity College, Hartford CT • Department of Computer Science • CPSC 372 15
    16. 16. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); As long as you create the table within the schema environment. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 16
    17. 17. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Base command for creating a table Trinity College, Hartford CT • Department of Computer Science • CPSC 372 17
    18. 18. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Non-null Constraints Trinity College, Hartford CT • Department of Computer Science • CPSC 372 18
    19. 19. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Primary Key Constraint entity integrity constraint (non-null primary key) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 19
    20. 20. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Foreign Key Constraint referential integrity constraint (non-null foreign key) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 20
    21. 21. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 21
    22. 22. Attribute Domains • Data Types Integer (Int) SmallInteger • Numeric Float • Character string Real • Bit string Double Decimal(i,j) (j is precision) • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
    23. 23. Attribute Domains • Data Types Char(n) • Numeric Varchar(n) • Character string Clob • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 23
    24. 24. Attribute Domains • Data Types Char(n) Fixed-length • Numeric Varchar(n) Padded if string is shorter than n • Character string Clob • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 24
    25. 25. Attribute Domains • Data Types Char(n) Variable-length • Numeric Varchar(n) No Padding • Character string Clob • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 25
    26. 26. Attribute Domains • Data Types Char(n) Character • Numeric Varchar(n) Large • Character string Clob Object Large text values • Bit string such as documents • Boolean Maximum length • Time specified as K, M, or G. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 26
    27. 27. Attribute Domains • Data Types Char(n) • Numeric Varchar(n) • Character string Clob • Bit string Format • Boolean ‘Smith’ • Time Concatenation ‘Bob ’ || ‘Smith’ ‘Bob Smith’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
    28. 28. Attribute Domains • Data Types • Numeric • Character string Bit(n) • Bit string Bit Varying(n) • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
    29. 29. Attribute Domains • Data Types • Numeric • Character string Bit(n) Fixed Length • Bit string Bit Varying(n) • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 29
    30. 30. Attribute Domains • Data Types • Numeric • Character string Bit(n) Variable • Bit string Bit Varying(n) Length • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 30
    31. 31. Attribute Domains • Data Types • Numeric • Character string Bit(n) Binary • Bit string Bit Varying(n) Large Object • Boolean Blob Images, Objects • Time Max Size as K, M, G: Blob(30G) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 31
    32. 32. Attribute Domains • Data Types • Numeric • Character string Bit(n) Format • Bit string Bit Varying(n) ‘B0010110’ • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
    33. 33. Attribute Domains • Data Types • Numeric • Character string Format • Bit string TRUE FALSE • Boolean Boolean NULL (Unknow) • Time three-valued logic Trinity College, Hartford CT • Department of Computer Science • CPSC 372 33
    34. 34. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Date Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 34
    35. 35. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Date Format Time YYYY-MM-DD DATE‘2010-09-30’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 35
    36. 36. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Date Format Time HH:MM:SS TIME’13:30:27’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36
    37. 37. Attribute Domains • Data Types • Numeric Format • Character string DATE TIME • Bit string TIMESTAMP’2010-09-30 13:30:27’ • Boolean • Time Date Time Timestamp Extension Type Trinity College, Hartford CT • Department of Computer Science • CPSC 372 37
    38. 38. Attribute Domains • Data Types • Numeric Create your own domain! • Character string CREATE DOMAIN SSN_TYPE AS CHAR(9); • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 38
    39. 39. Attribute Constraints • Default • Specifies a default value for an attribute Dno INT DEFAULT 75 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 39
    40. 40. Attribute Constraints • Default • Specifies a default value for an attribute Dno INT DEFAULT 75 • Check • Impose a restriction on values Dno INT NOT NULL CHECK (Dno > 0 AND Dno < 100) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 40
    41. 41. Attribute Constraints • Default • Specifies a default value for an attribute Dno INT DEFAULT 75 • Check • Impose a restriction on values Dno INT NOT NULL CHECK (Dno > 0 AND Dno < 100) • Key Constraint PRIMARY KEY (Dno) Dno INT PRIMARY KEY Trinity College, Hartford CT • Department of Computer Science • CPSC 372 41
    42. 42. Attribute Constraints • Unique • Indicates uniqueness, but not a primary key Dname Varchar(15) UNIQUE Trinity College, Hartford CT • Department of Computer Science • CPSC 372 42
    43. 43. Attribute Constraints • Unique • Indicates uniqueness, but not a primary key Dname Varchar(15) UNIQUE Other constraints can be specified, but often they depend on the RDBMS. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 43
    44. 44. Retrieval Queries SELECT-FROM-WHERE Structure SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 44
    45. 45. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 45
    46. 46. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT Bdate, Address FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 46
    47. 47. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT Bdate, Address FROM EMPLOYEE WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 47
    48. 48. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT Bdate, Address FROM EMPLOYEE WHERE Fname=‘John’ AND Minit=’B’ AND Lname=‘Smith’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 48
    49. 49. Query Example Retrieve the name and address of all employees who work for the ‘Research’ department. SELECT <attribute list> FROM <table list> WHERE <condition>; What is different about this query? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 49
    50. 50. Query Example Retrieve the name and address of all employees who work for the ‘Research’ department. SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname=‘Research’ AND Dnumber=Dno; What is different about this query? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 50
    51. 51. Query Example Retrieve the name and address of all employees who work for the ‘Research’ department. SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname=‘Research’ AND Dnumber=Dno; What is different about this query? It uses a join condition Trinity College, Hartford CT • Department of Computer Science • CPSC 372 51
    52. 52. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 52
    53. 53. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 53
    54. 54. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT Pnumber, Dnum, Lname, Address, Bdate FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation=‘Stafford’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 54
    55. 55. More SQL Next Time Questions? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 55

    ×