Data Models & SQL
Data Models & SQL
- Determining what the database should look like and how it should function before creating it
- To start, think about the most essential aspect of the application. Once you build that you can determine what
other attributes need their own tables and what are attributes of the essential aspect table
- Primary Key – A column (or a set of columns) in a table that uniquely identifies each row in that table and is
referenced in other tables
- Foreign Key – A column (or a set of columns) in one table that is used to refer to the primary key in another
table
- Logical Key - A column or a set of columns that uniquely identify a record based on the business logic or
domain requirements. Used to lookup data in database from outside world
- When creating tables in database, work from outside in and create subsidiary tables first
- If you don’t add an ON clause, it combines all the data together in every possible combination
- Reconstruct replication to show users but don’t store replication in actual database
Textbook:
- Data Modeling – The act of deciding how to break up your application data into multiple tables and
establishing relationships between those tables
- Data Model – The design document that shows the tables and their relationships
- Database Normalization – Never put the same string data in a column more than once, better to create a
numeric key for the data and reference it from another table
- Use a primary key to link one table to another, key is a number assigned into table using id column
- In the Artist Table, 42 is a primary key but in subsequent tables that reference it is known as a foreign key
because it references data in a different table
- When we want to retrieve data we use the JOIN keyword to properly extract it during a SELECT statement
- Crow’s Foot Diagrams – Each table is shown as a box with the name of the table and its columns. Then the
relationship between tables is represented by drawing a connecting line and adding notation at the end to
indicate the nature of the relationship
- When creating large databases, it is best to let the Primary Key ID be automatically generated. This is
accomplished by declaring the id column as PRIMARY KEY and leaving out a numerical id value
- Use the SELECT last_insert_rowid() function to lookup the value of the last primary key inserted into table
Logical Keys:
- Logical Keys are used to create an index that improves the query speed of the database and facilitates faster
information lookup
- They consist of attributes that naturally occur in the data and carry business meaning (Social Security number
or ISBN for books)
Enforcing Constraints:
- Can also use an index to enforce rules within a database
- The most common one is a uniqueness constraint that requires all values in a column to be unique
- Can use the IGNORE keyword to force database to ignore the constraint