place to store data Specifically, a database is collection of structured information that can be efficiently accessed and modified A database has four types of operations 1. Retrieve data from the database 2. Insert data into the database 3. Modify data in the database 4. Delete data from the database
to store data, we have to set up the database with these steps: 1. Install the database software 2. Create the database file 3. Define the structure of the data in the database
Microsoft SQL Server Oracle IBM’s DB2 Microsoft Access IBM’s Informix Free databases for amateur developers PostgreSQL MySQL SQLite Microsoft SQL Server 2008 Express Edition
putting the data into tables Each table has a unique name Tables are arranged into columns and rows Each column is used to store one attribute of the data Columns are also referred to as fields Each row is used to store a complete data item Rows are also referred to as records
store information about customers Name of database: CUSTOMERS Customer attributes: name of customer, phone number, zip code, and date of first purchase Customer attributes are columns in database: Name Phone Zip FirstPurchase
records for the 5 customers Name Phone Zip FirstPurchase Misae Nohara 808-333-4444 96816 January 10, 2005 Satsuki Kusakabe 808-222-5555 96819 April 22, 2001 Hattori Hanzo 808-111-2222 96812 June 7, 2002 Kosaku Shima 808-555-7777 96817 July 1, 2007 Chihiro Ogino 808-888-9999 96819 October 3, 2003
each column of data has a specific data type Name column has data type nvarchar(50) Phone column has data type nvarchar(12) Zip column has data type nvarchar(5) FirstPurchase column has data type date To see a complete list of SQL Server Data Types and Ranges for the columns (fields), see the Data Types and Update 2008 links on the class web page
Column data type Description Visual Basic parallel nvarchar(n) A string of up to n characters in length String int A whole number Integer bit 0 (zero) or 1 (one) Boolean datetime Year, month, day and time DateTime money Monetary value Decimal float Floating-point number Decimal
types nvarchar, nchar, varchar, and char? var is short for “variable length” nvarchar(20) will take up only the amount of space that is needed, up to 20 characters nchar(20) or char(20) will always take up 20 characters, whether the word is shorter or not n is short of “Unicode” Unicode has a much greater range of characters than ASCII characters
to encode all the languages in the world with up to 32 bits See the Unicode link on the class webpage to see a really cool chart of all the languages in the world! Older character standard gives each character a numeric code called an ASCII code (American Standard Code for Information Interchange) with 8 bits See the ASCII link on the class website for an example
and column (field, attribute) is missing, the Null value is used Null is a marker for “nothing” or “unknown” value For example, if customers Hattori Hanzo and Chihiro Ogino do not have phones, Null values are entered Name Phone Zip FirstPurchase Misae Nohara 808-333-4444 96816 January 10, 2005 Satsuki Kusakabe 808-222-5555 96819 April 22, 2001 Hattori Hanzo Null 96812 June 7, 2002 Kosaku Shima 808-555-7777 96817 July 1, 2007 Chihiro Ogino Null 96819 October 3, 2003
in a table is the primary key columns The primary key uniquely identifies each record (row) in a database Usually a column of type int with unique values Typically has the name TableNameID Can be auto-incremented (automatic counter), so that the value is always unique and automatically set when a new record is added to the table
with SQL server 1. Create a new website with Visual Web Developer 2. In the Solution Explorer window, right-click on the name of the website 3. Click Add New Item 4. Click SQL Server Database 5. Change the name to MyDatabase.mdf 6. You should see the Database Explorer window, if not, click View > Other Windows > Database Explorer 7. When it asks to create the App_Data folder, click Yes
about your book collection Here are the book attributes, which will be the columns (fields) of the database table: 1. Title of type nvarchar(50), no Nulls 2. Author of type nvarchar(50), no Nulls 3. Year of type int, no Nulls 4. Price of type money, no Nulls 5. LastRead of type datetime, Nulls allowed 6. Pages of type int, no Nulls
table 1. In the Database Explorer window, under MyDatabase.mdf, right click on Tables 2. Click Add New Table 3. Fill in the Column Name, Data Type, and Allow Nulls checkbox for all the attributes
key column: 1. Right click on the 1st row (1st column in database) 2. Click Insert Column 3. Column name is BookID 4. Data type is int 5. Allow no Nulls 6. Click the 1st row (1st column in database) 7. Click the primary key icon (a yellow key just above the table)
key column (continued): 8. Click the row again 9. Go to the Column Properties window just below the table 10. Expand the Identify Specification property by clicking on the plus sign (+) 11. For the (Is Identity) property, change the value to Yes 12. Click Save to save the file 13. Change the table name to BOOKS
to the table 1. Go to the Database Explorer window 2. Expand the Tables folder, by clicking on the plus sign (+) 3. Right click on the Books table 4. Click Show Table Data 5. Click on the cell in the 1st row, BookID column 6. Use the Tab key to more to the other fields (columns) 7. Type in the data for the 5 books listed in the slides 8. Add your own 10 books (so 15 total records) 9. Use real data (do not makeup book titles, etc.)
correct data type If the column does not accept Null values, then you have to put in a value If the column does accept Null values, then you can leave it blank or fill it in The red exclamation point just means that the cell’s value has changed Once you tab to the next record, then the previous record is written to the database, and the red exclamation points disappear
column titles as the table below, plus 10 more records of your own BookID Title Author Year Price LastRead Pages 1 Alice's Adventures in Wonderland Lewis Carroll 1865 3.95 NULL 240 2 The Grapes of Wrath John Steinbeck 1939 13.29 10/31/1983 672 3 Uncle Tom's Cabin Harriet Beecher Stowe 1852 39.99 NULL 384 4 Animal Farm George Orwell 1945 9.99 4/1/1986 176 5 Gulliver's Travels Jonathan Swift 1726 8.00 NULL 336