Lecture 3 Finishing SQL www.Agile1Tech.com
Quick Recap Constraints Select Operators Not Null, Unique What is a Database schema A Collection of Database Objects associated with ONE database Username. EX – Facebook Personal_Information, Wall_Posts, Friends, Friends_Wall_Posts What are some Datatypes? Go through the Datatypes table to review. EX - String, Number, etc. Select Operators Equal, Not Equal, Greater than, Less than, etc. etc. BETWEEN, LIKE, IN Select Distinct, Wildcards Constraints Not Null, Unique PRIMARY KEY / FOREIGN KEY www.agile1tech.com Zahid Hussin
QUIZ!!! (like I promised) Can a Table have more than one PRIMARY KEY? Can a Table have more than one FOREIGN KEY? What is the difference between COUNT() and COUNT(*) Write a Query to find the FIRST_NAME which starts with the letter “T”. Table name for this Question is “persons” True/False --- A Primary key CAN contain Null Values. True/False --- A Primary key CANNOT contain Unique Values Here is the tough one! ---- Write the line of code which creates the foreign key constraint for a column. www.agile1tech.com Zahid Hussin
HOMEWORK REVIEW ! First we need to create the table which is used in the homework.
HW REVIEW CONT. 1. What Query will confirm 10 records are in the database? 2. Write a query that tells you how many drivers are single drivers? 3. Write a Query that tells you how many drivers are not single drivers? 4. In the front end, you wrote 10 records. For each record a new customer ID is generated. What should your max customer ID be?
HW part 1.5 What is the difference between Count() and Count(*) Which aggregate function gives you the biggest value of a column? If you want to order the table from newest entry to oldest entry, what would be the code for this? 1. Count (Column_name) will count the NON-NULL data in column_name and return that number. 2. Count (*) will count EVERY ROW in the ENTIRE TABLE and return that value.
HW PART 1.5 CONT. 4. Write the code which will help you search for a record that starts or ends with a certain character. 5. Write an SQL query which will help you find a city with ONLY 5 Characters. 6. Write an SQL query to find the first name of employees that start with the letter 'A'.
HW PART 2
HW PART 2 CONT. 1. Write a query to display first name and last name for all employees whose salary is in the range $10,000 through $15,000 I HAD AN ISSUE HERE, there were too many rows and I only wanted to see this answer within the first 10 rows that showed above.
HW PART 2 CONT. 2. Write a query to display the [first name, last name and department ID] of all employees in departments 30 or 100 in descending order by department ID.
HW PART 2 CONT. 3. Write a query to display the first name, last name and hire date for all employees who were hired in 1987. 4. Write a query to display the last names of employees whose names have exactly 6 characters.
HW PART 2 CONT. 5. Write a query to display the first name, last name and salary for all employees whose salary is in the range $10,000 through $15,000 and are in department 30 or 100.
!!!JOINS!!! Imagine that Each table is represented by a circle. THIS IS THE MOST IMPORTANT SQL COMMAND YOU WILL EVER EVER EVER EVER EVER EVER EVER EVER LEARN!!!!!!!!!! An instruction to a database to combine data from MORE THAN ONE TABLE. And then to return some values WHICH FULFILL A SPECIFIC CONDITION. Imagine that Each table is represented by a circle. Then what can we do with these circles? www.agile1tech.com Zahid Hussin
Lets start ! To explain this, I will use 2 basic tables which are easy to understand. Table 1 = people Table 2 = food What does the foreign key signify? What does it do?
First understand Foreign Key Lets put some data into our two tables Why doesn’t this work?
What about NO Foreign key? And now will it work?
YES
JOIN INNER JOIN General Query Example… What if I only want to see the name of the person and the food they like? Introduction to Software Testing (Ch 1) © Ammann & Offutt
INNER JOIN CONT. I want to see which food people like. What about more complex functions?
RIGHT JOIN SAME AS RIGHT OUTER JOIN General Query Someone explain this to me!!!
RIGHT JOIN CONT.
LEFT JOIN SAME AS LEFT OUTER JOIN General Query This should be easier to explain.
LEFT JOIN CONT.