Beginning SQL
Beginning SQL Beginning SQL
Chapter 1 38 Finally, you need to assign each film a category — for example, horror, action, romance, and so on. The following table outlines the Films table’s contents: Field Name Data Type Notes FilmId integer Primary key FilmName varchar(100) YearReleased integer PlotSummary varchar(2000) Change to memo data type if using MS Access or to the text data type if using MySQL AvailableOnDVD char(1) Rating integer CategoryId integer Foreign key Before going further, there are two points to note regarding the field definitions. First, the PlotSummary field is a varchar data type, except when used in MS Access, which only supports varchar with a size of up to 255 characters. Because you need to store up to 2000 characters, you must use Access’s memo field, which allows you to store up to 65,536. You can’t specify length; it’s set at a maximum of 65,536 characters, which is equivalent to a varchar(65536) data type. The second point to note pertains to the CategoryId field. It is a foreign key field, which means that its value is the primary key field in another table, and it provides a way of relating the two tables. The table containing the primary key to which the Films table links is the FilmCategory table, which you will create shortly. First, though, the SQL to create the Films table is as follows: CREATE TABLE Films ( FilmId integer, FilmName varchar(100), YearReleased integer, PlotSummary varchar(2000), AvailableOnDVD char(1), Rating integer, CategoryId integer ); After creating the Films table, you can create the FilmCategory table, which contains the following data: Field Name Data Type Notes CategoryId integer Primary key Category varchar(100)
- 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
Chapter 1<br />
38<br />
Finally, you need to assign each film a category — for example, horror, action, romance, and so on.<br />
The following table outlines the Films table’s contents:<br />
Field Name Data Type Notes<br />
FilmId integer Primary key<br />
FilmName varchar(100)<br />
YearReleased integer<br />
PlotSummary varchar(2000) Change to memo data type if using MS Access or to<br />
the text data type if using My<strong>SQL</strong><br />
AvailableOnDVD char(1)<br />
Rating integer<br />
CategoryId integer Foreign key<br />
Before going further, there are two points to note regarding the field definitions. First, the PlotSummary<br />
field is a varchar data type, except when used in MS Access, which only supports varchar with a size<br />
of up to 255 characters. Because you need to store up to 2000 characters, you must use Access’s memo<br />
field, which allows you to store up to 65,536. You can’t specify length; it’s set at a maximum of 65,536<br />
characters, which is equivalent to a varchar(65536) data type.<br />
The second point to note pertains to the CategoryId field. It is a foreign key field, which means that its<br />
value is the primary key field in another table, and it provides a way of relating the two tables. The table<br />
containing the primary key to which the Films table links is the FilmCategory table, which you will create<br />
shortly. First, though, the <strong>SQL</strong> to create the Films table is as follows:<br />
CREATE TABLE Films<br />
(<br />
FilmId integer,<br />
FilmName varchar(100),<br />
YearReleased integer,<br />
PlotSummary varchar(2000),<br />
AvailableOnDVD char(1),<br />
Rating integer,<br />
CategoryId integer<br />
);<br />
After creating the Films table, you can create the FilmCategory table, which contains the following data:<br />
Field Name Data Type Notes<br />
CategoryId integer Primary key<br />
Category varchar(100)