Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

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)

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)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!