Beginning SQL
Beginning SQL Beginning SQL
The FilmCategory table is very small and simple. The SQL you need to create it is likewise very simple: CREATE TABLE Category ( CategoryId integer, Category varchar(100) ); The final table you need to create is called FavCategory, and it stores each member’s favorite film categories: Field Name Data Type Notes CategoryId integer Foreign key MemberId integer Foreign key As you can see, it’s a very simple table! Both CategoryId and MemberId are foreign keys, the first from the Category table and the second from the MemberDetails table. Combined, they make up a unique primary key. The SQL to create the FavCategory table is as follows: CREATE TABLE FavCategory ( CategoryId integer, MemberId integer ); Creating the FavCategory table completes the basic database structure. As you can see, creating a database is actually fairly easy! Chapter 4 covers more complex examples and options, but what you learned in this chapter lays a good foundation of database creation. Summary This chapter walked you through the fundamentals of SQL and database design and provided you with instruction on how to write the SQL code necessary to create a database’s structure. You now have enough knowledge to start designing your own databases. In this chapter, you discovered the following: Introduction to SQL ❑ Databases are an efficient way to store large amounts of raw data. They don’t process the data; that’s left to the application that uses the data. ❑ Databases make sharing data easier than do other means, such as text files, spreadsheets, or other documents. They also allow secure data sharing and allow you to define the levels of user access. You can limit what you let others do to your database’s data. ❑ Relational databases contain tables and fields and provide ways of relating data in different tables and ways of ensuring that any data entered is valid and doesn’t corrupt the database. 39
- Page 66: The SQL language is actually fairly
- Page 70: you wish, and the database system d
- Page 74: A keyword is a word that has some m
- Page 78: Introduction to SQL A perfectly val
- Page 82: Data Type Description Storage Used
- Page 86: The char and varchar data types use
- Page 90: Most RDBMSs handle the most common
- Page 94: To add a new column, use the basic
- Page 98: Now that you know what you want to
- Page 102: ❑ Meeting date 2 ❑ Location 2
- Page 106: Generally speaking, picking a data
- Page 110: example, the MemberId field links t
- Page 114: Depending on which RDBMS you’re u
- Page 120: Chapter 1 ❑ Databases are part of
- Page 124: Chapter 2 42 CategoryId Category 1
- Page 128: Chapter 2 44 Member First Last Date
- Page 132: Chapter 2 46 Now Steve’s record l
- Page 136: Chapter 2 This would work fine with
- Page 140: Chapter 2 If you want to delete all
- Page 146: 3 Extracting Information So far you
- Page 150: Returning Only Distinct Rows If you
- Page 154: The good news is that everything yo
- Page 158: MemberDetails (MemberId, FirstName,
- Page 162: MemberDetails table, the columns Fi
- Page 166: State DateOfJoining New State 2005-
The FilmCategory table is very small and simple. The <strong>SQL</strong> you need to create it is likewise very simple:<br />
CREATE TABLE Category<br />
(<br />
CategoryId integer,<br />
Category varchar(100)<br />
);<br />
The final table you need to create is called FavCategory, and it stores each member’s favorite film<br />
categories:<br />
Field Name Data Type Notes<br />
CategoryId integer Foreign key<br />
MemberId integer Foreign key<br />
As you can see, it’s a very simple table! Both CategoryId and MemberId are foreign keys, the first from<br />
the Category table and the second from the MemberDetails table. Combined, they make up a unique primary<br />
key. The <strong>SQL</strong> to create the FavCategory table is as follows:<br />
CREATE TABLE FavCategory<br />
(<br />
CategoryId integer,<br />
MemberId integer<br />
);<br />
Creating the FavCategory table completes the basic database structure. As you can see, creating a<br />
database is actually fairly easy! Chapter 4 covers more complex examples and options, but what you<br />
learned in this chapter lays a good foundation of database creation.<br />
Summary<br />
This chapter walked you through the fundamentals of <strong>SQL</strong> and database design and provided you with<br />
instruction on how to write the <strong>SQL</strong> code necessary to create a database’s structure. You now have<br />
enough knowledge to start designing your own databases.<br />
In this chapter, you discovered the following:<br />
Introduction to <strong>SQL</strong><br />
❑ Databases are an efficient way to store large amounts of raw data. They don’t process the data;<br />
that’s left to the application that uses the data.<br />
❑ Databases make sharing data easier than do other means, such as text files, spreadsheets, or<br />
other documents. They also allow secure data sharing and allow you to define the levels of user<br />
access. You can limit what you let others do to your database’s data.<br />
❑ Relational databases contain tables and fields and provide ways of relating data in different<br />
tables and ways of ensuring that any data entered is valid and doesn’t corrupt the database.<br />
39