1. Home
  2. Explore
  3. Computer Science
  4. SQL Database Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

SQL Database Course

Speaker 1Speaker 2

SQL Database Course

6:23
0:00-6:23
  1. Maria

    Alright, Corey, let's dive into SQL. But first, what even is a database? It's really just any collection of related information. Think of a phonebook, a shopping list, or even your five best friends in your head. That's all a database.

  2. Corey

    So even something as simple as a to-do list is a database? That's broader than I thought. But why do we need special software then? Can't we just use a notepad?

  3. Maria

    You could, but computers are way better at handling huge amounts of data, keeping it secure, and making it easy to back up. That's where Database Management Systems come in. They're software that help create, maintain, and secure databases. For relational databases, we use something called a Relational DBMS.

  4. Corey

    Right, and relational databases organize data into tables with rows and columns, like an Excel spreadsheet. That's the most popular type, and we use SQL—Structured Query Language—to interact with them. So what can SQL actually do?

  5. Maria

    SQL is a hybrid language. It's a data query language for asking questions, a data definition language for defining tables, a data control language for managing access, and a data manipulation language for inserting, updating, and deleting data. Basically, it handles everything you need to do with a relational database.

  6. Corey

    Got it. So for this course, we're going to install MySQL, which is a popular relational DBMS, and then we'll learn SQL by actually using it. I've seen the installation steps for Windows and Mac. What do we need to do first?

  7. Maria

    First, install MySQL Community Server. On Windows, you use the installer and select custom, then add MySQL Server and MySQL Shell. On Mac, download the DMG archive, install it, then set up the server in System Preferences. You'll also need to update the temporary password. Then we'll create a database called 'giraffe' using the MySQL command line.

  8. Corey

    And once that's done, we install PopSQL as a nicer interface to write and run queries. It connects to our MySQL server. So now we can start creating tables. How do we do that?

  9. Maria

    We use CREATE TABLE. For example, a student table: CREATE TABLE student ( student_id INT PRIMARY KEY, name VARCHAR(20), major VARCHAR(20) );. You have to specify the column names and data types. INT for integers, VARCHAR for strings, DECIMAL for decimals, DATE for dates, etc.

  10. Corey

    Right, and we can also add constraints like NOT NULL, UNIQUE, and DEFAULT. For instance, if we want the name to never be null or the major to be unique, we add that after the data type. And AUTO_INCREMENT is great for primary keys so we don't have to assign them manually.

  11. Maria

    Exactly. Once the table is created, we insert data with INSERT INTO student VALUES (1, 'Jack', 'Biology');. We can also specify columns to insert only parts of the row. And if we try to insert a duplicate primary key, it'll give an error.

  12. Corey

    Then we can update and delete rows using UPDATE and DELETE. The WHERE clause is crucial there. For example, UPDATE student SET major = 'Bio' WHERE major = 'Biology';. And we can use AND and OR conditions to be more specific.

  13. Maria

    And for querying, we use SELECT. SELECT * FROM student gets all rows. We can select specific columns like SELECT name, major FROM student. We can order results with ORDER BY, limit with LIMIT, and filter with WHERE using comparison operators like =, <, >, and the IN keyword for multiple values.

  14. Corey

    That covers the basics. Now for the company database, which is more complex. It has tables like employee, branch, client, branch_supplier, and works_with. They're all linked by foreign keys. Creating those tables involves defining primary keys and foreign keys with ON DELETE SET NULL or CASCADE.

  15. Maria

    Right. Inserting data into that schema required careful ordering because of circular references between employee and branch. We had to insert employees with NULL branch_id first, then create the branch, then update the employee. That's a common challenge in complex schemas.

  16. Corey

    Then we moved to more advanced queries. Functions like COUNT, AVG, SUM work great. For example, SELECT COUNT(emp_id) FROM employee; gives the number of employees. And we can use GROUP BY to group results, like finding the total sales per employee.

  17. Maria

    Wildcards with LIKE are powerful. '%LLC' finds any client ending in LLC. Underscore matches exactly one character, so '____-10%' finds any date in October. UNION combines results from multiple SELECT statements, but they must have the same number of columns and similar data types.

  18. Corey

    JOINs let us combine rows from multiple tables. INNER JOIN only includes matching rows. LEFT JOIN includes all rows from the left table, RIGHT JOIN all from the right. For instance, SELECT employee.first_name, branch.branch_name FROM employee JOIN branch ON employee.emp_id = branch.mgr_id; gives the managers and their branches.

  19. Maria

    Nested queries use one SELECT inside another. Like finding employees who sold over $30,000: SELECT first_name, last_name FROM employee WHERE emp_id IN (SELECT emp_id FROM works_with WHERE total_sales > 30000);. That's a great way to filter based on related data.

  20. Corey

    And when we delete rows, foreign keys can cause issues. ON DELETE SET NULL sets the foreign key to null if the referenced row is deleted. ON DELETE CASCADE deletes the entire row. The choice depends on whether the foreign key is part of the primary key or not.

  21. Maria

    Triggers automate actions. For example, a trigger can insert a log entry every time a new employee is added. We write them with DELIMITER $$ ... END $$ DELIMITER ;. We can use NEW to access the new row's columns. Triggers can be BEFORE or AFTER INSERT, UPDATE, or DELETE.

  22. Corey

    Finally, ER diagrams help design databases. They map entities, attributes, relationships, and cardinalities. We took a company requirements document and built an ER diagram with entities like employee, branch, client. Then we converted that diagram into actual tables using mapping rules.

  23. Maria

    Right. For regular entities, we create tables with their attributes. Weak entities get a composite key that includes the owner's key. For 1:1 relationships, we add a foreign key on the side with total participation. For 1:N, the foreign key goes on the N side. For M:N, we create a new table with a composite key.

  24. Corey

    That's a lot of material! But it covers everything from the very basics to database design. With practice, it'll all make sense. If we combine the fundamentals with the company database examples and the ER diagram process, you'll have a solid foundation in SQL.

  25. Maria

    Absolutely. The key is to start with simple queries and gradually add complexity. And remember, all the code and extra resources are on GiraffeAcademy.com. So let's get out there and start querying!

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent