Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL – Structured Query Language CIS 324 – Chapter 5.

Similar presentations


Presentation on theme: "SQL – Structured Query Language CIS 324 – Chapter 5."— Presentation transcript:

1 SQL – Structured Query Language CIS 324 – Chapter 5

2 CREATE TABLE CREATE TABLEnewtablename ( Three-part column description, Etc.,); Three-part column description: column name, data type, and (optional) Primary Key, Not Null, Null Three-part column description: column name, data type, and (optional) Primary Key, Not Null, Null End with semi-colon; End with semi-colon;

3 DATA TYPES BinaryBinary BinaryBinary CharCharacter 0 -8,000 bytes CharCharacter 0 -8,000 bytes Datetime8 byte datetime (1753 - 9999) Datetime8 byte datetime (1753 - 9999) ImageVariable-length binary data ImageVariable-length binary data Integer4 byte integer Integer4 byte integer Money8 byte money Money8 byte money NumericDecimal NumericDecimal Smalldatetime4 byte datetime (1900 – 2079) Smalldatetime4 byte datetime (1900 – 2079) Smallint2 byte integer (+-32768) Smallint2 byte integer (+-32768) Smallmoney4 byte money Smallmoney4 byte money Text1 – 2 billion + characters Text1 – 2 billion + characters Tinyint1 byte integer 0-255 Tinyint1 byte integer 0-255 VarcharVariable length character 0 – 8000 bytes VarcharVariable length character 0 – 8000 bytes

4 CREATE TABLE PROJECT ( ProjectIDIntegerPrimaryKey, NameChar(25)Not Null, DepartmentVarChar (100)Null,(Null is allowed) MaxHoursNumeric (5,2));(5 numbers, 2 decimal) CREATE TABLE EMPLOYEE ( EmployeeNumberIntegerNot Null, NameChar (25)Not Null, PhoneChar(8), DepartmentVarChar (100)); ALTER TABLE EMPLOYEE ADD CONSTRAINT EmployeePK PRIMARY KEY (EmployeeNumber); (Add a primary key, EmployeePK is an arbitrary name for the constraint – must be unique) ALTER TABLE statement is necessary for a composite primary key

5 CREATE TABLE ASSIGNMENT ( ProjectIDIntegerNot Null, EmployeeNumIntegerNot Null, HoursWorkedNumberic (5,2)); ALTER TABLE ASSIGNMENT ADD CONSTRAINT AssignmentPK PRIMARY KEY (ProjectID, EmployeeNum);

6 DEFINING FOREIGN KEYS ALTER TABLE ASSIGNMENT ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNum) REFERENCES EMPLOYEE; ALTER TABLE ASSIGNMENT ALTER TABLE ASSIGNMENT ADD CONSTRAINT ProjectFK FOREIGN KEY (ProjectID) REFERENCES PROJECT ON DELETE CASCADE; On Delete Cascade  Allow Cascading Delete

7 QUERY THE TABLES SELECT Statement: The result of a SQL SELECT statement is always a relation. They start with one or more relations, manipulate them, and produce a relation (could be as little as one column, one row – a single cell – but it is still a relation) SELECTcolumn name, column name  the order of column names Determines the order of the results FROMtable name; SELECTName, MaxHours, Department FROMProject; (what would be results) SELECTName, Department, MaxHours FROMProject; (different order)

8 SELECT Department FROMPROJECT; Results:Finance Accounting Marketing Finance The requirements for checking and eliminating duplicate rows within a DBMS is difficult and time consuming – often not done. In reality duplicate rows can exist within a SQL relation. SELECTDISTINCT Department  Avoid duplicate row FROMPROJECT; Results:Finance Accounting Marketing

9 SELECTING ROWS SELECTProjectID, Name, Department, MaxHours FROMPROJECT WHEREDepartment = ‘Finance’; SELECT*  ALL COLUMNS FROMPROJECT WHEREDepartment = ‘Finance’; (What are results)

10 SELECT / FROM / WHERE – is the pattern for SQL select statements WHERE – character data types in ‘ ‘, values ARE case-sensitive Multiple where conditions joined by AND SELECT* FROM PROJECT WHEREDepartment = ‘Finance’ AND MaxHours > 100; (What are results)

11 SELECTING ROWS & COLUMNS SELECTING ROWS AND COLUMNS – combine above SELECTName, Department FROMEMPLOYEE WHEREDepartment = ‘Accounting’;

12 RANGES, WILDCARDS, NULLS IN keyword  WHERE Department IN (‘Accounting’, ‘Finance’, ‘Marketing’); NOT IN keyword  WHERE Department NOT IN (‘Accounting’, ‘Finance’, ‘Marketing’); BETWEEN keyword  WHERE EmployeeNumber BETWEEN 200 AND 500; LIKE keyword  SELECT* FROMProject WHEREName LIKE ‘Q_Portfolio Analysis’; LIKE  Selects Partial Values _  Selects a Single Wildcard Character %  Multiple Wildcard Characters (Phone Like ‘285-%’;) (ACCESS uses ? not _ and * not %) NULL keyword  Searches for null values WHEREPhone is NULL

13 SORTING RESULTS SORTING RESULTS: ORDER BY (default in ascending order) ORDER BY ASC(ascending order) ORDER BY DESC(descending order) SELECTName, Department FROMEMPLOYEE ORDER BYDepartment; Can have multiple sort order – separate by comma Select Name, Department FROMEMPLOYEE ORDER BYDepartment DESC, Name ASC;

14 BUILT IN FUNCTIONS BUILT-IN Functions: Operate on the results of a SELECT statement COUNT  counts the number of rows in the resulting relation SUM  sum of NUMERIC columns AVG  average of NUMERIC columns MAX  max value of NUMERIC columns MIN  min value of NUMERIC columns SELECTCOUNT (*) FROMPROJECT; Result  4 SELECTCOUNT (DISTINCT Department) FROMPROJECT; Result  3

15 SELECTMIN (MaxHours), MAX (MaxHours), SUM (MaxHours) FROMPROJECT WHEREProjectID < 1500; Result  75.00145.00358.00 Cannot combine regular column names and built-in functions in the same select statement (except when using a grouping statement) Not Allowed: SELECTMaxHours, SUM(MaxHours) FROMPROJECT WHEREProjectID < 1500; Allowed: SELECTDepartment, Count(*) FROMEMPLOYEE GROUP BYDepartment; This counts the number of employees in each department Accounting 2 Marketing2 Finance1 Info Systems 2

16 GROUP BY / HAVING GROUP BY - This sorts the table by the named column and then applies the built in function to groups of rows having the same value as the named column. HAVING – Restricting conditions on the grouping SELECTDepartment, Count(*) FROMEMPLOYEE GROUP BYDepartment HAVING COUNT(*) > 1; Only groups departments with more than one member: Accounting2 Marketing2 Info Systems2 When using WHERE clauses with GROUP clauses there may be ambiguity as to which is done first, do you check the where condition before grouping or group before the where??? Standards indicate you apply the WHERE before the GROUPING

17 SELECTDepartment, Count(*) FROMEMPLOYEE WHEREEmployeeNumber < 600  Evaluated First GROUP BYDepartment  Evaluated Second HAVINGCOUNT (*) > 1; Result  Accounting2


Download ppt "SQL – Structured Query Language CIS 324 – Chapter 5."

Similar presentations


Ads by Google