Introduction to SQL
Rongji Yao, Utku Tuluk
NYU Shanghai Library
Fall 2021
Content
………Overview of Database
………Software for Demo, SQL-Teaching Website
………Entity, Attribute, Data
………Table, Relationship Database
………Syntax, Example and Exercise
………Python Django
Overview
Information
Table
Database
Data
Record
Extract
Store
Query with SQL
Answer with Data
User
Design(with SQL)
Overview
SQL = Structured Query Language
When you ask a question to search engine
Use natural language
When you ask a question to Database?
Use Structured Query Language
Overview-Software for database Demo
MySQL Workbench for demonstrations and screenshots
MySQL Workbench is a unified visual tool for database architects, developers, and DBAs.
Overview-SQL-Teaching Website for this workshop
Perfect study website for SQL beginner!
Syntax
Example
Demo Data
Exercise
1.1 Database-Entity, Attribute and Data
How we describe the world?
1.2 Database-Reality to Table
Each line data is a record of Entity 加slides介绍Mysql Workbench
1.3 Database-To define a table
1.3 Database-To define a table
1.3 Database-To define a table
Insert this line into table
Report with error because the department_id and school_id are null
1.3 Database-To define a table
Two person have same Name, Birthday, Gender, Race, Nationality values, we could use PK to identify them.
1.3 Database-To define a table
No school_id = 4 in School table!
1.4 Database-Relationship Between Tables (entity)
1.5 Database-Tables to Database
1.5 Database-Tables to Database
yz1121 double major in CS
What DB program does?
1. Reconstruct “student_belongto_Dept” (may take more time if searching and deleting are needed)
2. Reconstruct “Department_belongto”(may take more time if searching and deleting are needed)
Hard to find whether a student is in a department...
String operation takes too much time and computing resource!
1.5 Database-Tables to Database
More structured
Easier to insert, update, delete, collect
2 Introduction to SQL Syntax
For each statement/clause/function:
How we learn SQL today?
1. Learn syntax
2. Example
3. Do exercise
Using Demo Database
2 Introduction to SQL Syntax
Demo Database Exercise
Figure out Relationships between :
Customers&OrderID
Employees&OrderID
Suppliers&OrderID
2 Introduction to SQL Syntax
Demo Database Exercise
Figure out Relationship between :
Suppliers&Products
2 Introduction to SQL Syntax
Demo Database Exercise
Figure out Relationship between :
Categories&Products
2 Introduction to SQL Syntax
Demo Database Exercise
Figure out Relationship between :
OrderID&OrderDetail
OrderDetail&Products
2 Introduction to SQL Syntax
Demo Database Exercise
2 Introduction to SQL Syntax
3 Real-life applications
3.1 Django
What is Django?
“Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. ... It’s free and open source.” - djangoproject.com
3.2 Django MTV
Model, Template, View
Image from https://data-flair.training/blogs/django-mtv-architecture/
Django Code Example
Term List
•Entity: Noun to describe
•Attributes: Characteristics of an entity
•Data: Values of attributes
•Table Name: Entity in table
•Column: Attributes in table
•Row: data in table
•Column Name: Name of column
•Column Datatype: Datatype of column
•Column Attribute: Attribute of column
•Relationship: Relationship between tables, including: one to one, one to N and N to M
• MySQL Workbench: A visual tool for MySQL database