ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007
UCF 2ISM 4212 Lab Lab Overview Last Week: Introduction to the SQL Server DBMS Creating Databases Moving Databases This Week: Creating DB Tables Next Week: Introduction to SQL SELECT Statements
UCF 3ISM 4212 Lab Today’s Agenda CREATE tables and DROP tables (GUI and SQL) Fields (the columns), including setting field data types More about creating tables NULL / NOT NULL IDENTITY keyword PRIMARY KEY DEFAULT values Setting table properties Create a diagram Multi-page diagrams Using the diagram as an editing tool for DB objects Relationships between tables
UCF 4ISM 4212 Lab Create Tables and Drop Tables
UCF 5ISM 4212 Lab Two Ways of Creating Tables Graphical User Interface’s (GUI) “New Table” form Execute SQL in a Query Pane
UCF 6ISM 4212 Lab The GUI’s “New Table” form Using SQL Server Manager Studio, open the your database in the Object Explorer Expand the database to see the “Tables” folder Right click on “Tables” and select “New Table…” In the pane that opens, enter a “Column Name”, hit tab, use the drop down to specify a “Data Type”, and check “Allow Nulls” depending on your design Complete cells with column metadata Repeat until all of your columns are specified When done, “X” out the pane, respond “Yes” Finally, in the “Choose Name” pop-up, type in the name of your table, and click “OK”
UCF 7ISM 4212 Lab Having Created It, Display Your Table Using SQL Server Manager Studio In the Object Explorer, open the your database Expand the database to see the “Tables” folder Expand the “Tables” folder to see the name of your table Right click on the name of your table and select “Modify” Note: select “Open Table” to see the table’s data
UCF 8ISM 4212 Lab basic CREATE TABLE syntax CREATE TABLE Products ( ProductID INT, ProductName CHAR(20), Price MONEY ) Commas between column specifications Parenthesis around Column specifications Notice how each line has a field name and that field’s data type
UCF 9ISM 4212 Lab Another way to create a table with SQL Open a Query Pane Open the “Template Explorer” Under “Table”, select “Create Table” Replace the as needed an example of the syntax is on the next slide And, of course, execute
UCF 10ISM 4212 Lab Drop a Table (GUI) Using SQL Server Manager Studio, open the your database in the Object Explorer Expand the database to see the “Tables” folder Expand the “Tables” folder to see the name of your table Right click on the name of your table and select “Delete” Click on “Yes” to confirm
UCF 11ISM 4212 Lab Drop a Table (SQL) # Make sure you are using the correct database DROP TABLE YourTableName
UCF 12ISM 4212 Lab Setting table properties
UCF 13ISM 4212 Lab table properties In Object Explorer right-click on the table name Select “Properties”
UCF 14ISM 4212 Lab More about creating DB tables
UCF 15ISM 4212 Lab Look at an existing table In “Northwind” Open the “Shippers” table Using Rgt-Click “Modify” Note the following: A. Allow Nulls B. Identify Specification C. Primary Key icon D. Default values (not shown) Now we covers these four topics… C A B
UCF 16ISM 4212 Lab CREATE TABLE with NULL or NOT NULL CREATE TABLE Products2 ( ProductID INTNOT NULL, Name CHAR(20) NOT NULL, Price MONEYNOT NULL, Description CHAR(100)NULL ) After executed, you can view the table and check what you created for the Description field: A.
UCF 17ISM 4212 Lab What is the impact of the NOT NULL? In a few weeks, in the lab when we INSERT data into a table, we’ll see that if a field is marked as NOT NULL then data must be inserted into that field or an error message will result. That is, there is a constraint on that field in the table Any row of data in that table, must have data in any NOT NULL field. A.
UCF 18ISM 4212 Lab CREATE TABLE with an IDENTITY What does the IDENTITY do? Automatically increments on insert Only one per table By itself, not guaranteed unique You can turn it on and off Now, see the syntax… B.
UCF 19ISM 4212 Lab CREATE TABLE with an IDENTITY CREATE TABLE Products3 ( ProductID INTIDENTITYNOT NULL, Name CHAR(20) NOT NULL, Price MONEYNOT NULL, Description CHAR(100)NULL ) After executed, you can view the table and check the result at the bottom under “Column Properties”: B.
UCF 20ISM 4212 Lab What is the impact of the IDENTITY? In a few weeks, in the lab when we INSERT data into a table, we’ll see that if a field has an IDENTITY then the “seed” automatically increments and is inserted into that field of the row when the row is inserted into the table. This is perfect for some Primary Keys B.
UCF 21ISM 4212 Lab One little trick to know about the IDENTITY You can specify how the identity is going to behave: IDENTITY (, ) CREATE TABLE Products4 ( ProductID INTIDENTITY(10,5)NOT NULL, Name CHAR(20) NOT NULL, Price MONEYNOT NULL, Description CHAR(100)NULL ) B.
UCF 22ISM 4212 Lab One little trick to know about the IDENTITY When viewing the table, in the “Column Properties” section, expand the “Identity Specification” The details of the Identity B. (con’t)
UCF 23ISM 4212 Lab CREATE TABLE with a PRIMARY KEY CREATE TABLE Categories ( CategoryIDint NOT NULL, CategoryNamenvarchar(15)NOT NULL, DescriptionntextNULL, CONSTRAINT PK_Categories PRIMARY KEY (CategoryID) ) C. the name of this constraint the type of constraint the field that is the PK
UCF 24ISM 4212 Lab CREATE TABLE with a PRIMARY KEY After executed, you can view the table and see a check for the PK icon: C.
UCF 25ISM 4212 Lab What is the impact of a PRIMARY KEY ? For starters, it is a constraint “Enforces uniqueness for the purpose of identifying a row.” - Turley, page 331 C.
UCF 26ISM 4212 Lab Default values When a new row is inserted into this table, if the “State” field is not specified in the insert statement, then the system automatically add the default field of ‘FL’. CREATE TABLE Locations ( LocationID INT IDENTITY NOT NULL, LocationName CHAR(20) NOT NULL, State CHAR(2) NOT NULL DEFAULT 'FL' ) D.
UCF 27ISM 4212 Lab Evidence of the Default values D. Select the field then look below
UCF 28ISM 4212 Lab Diagram
UCF 29ISM 4212 Lab Create a diagram Using SQL Server Manager Studio, Open the your database in the Object Explorer Expand the database to see the “Database Diagrams” folder Right click on “Database Diagrams and select “New Database Diagram” Add Tables To save, right click on the tab of the pane and enter a name in the “Choose Name” pop-up
UCF 30ISM 4212 Lab Miscellaneous Diagram Operations On menu, select “Database Diagram” and then “Arrange Tables” To zoom, right click on the background and select “Zoom” To change the appearance of a table, right click on a table and select “Table View” and then a new selection
UCF 31ISM 4212 Lab From a diagram, you can alter your tables In the diagram, right click on the table Choose a selection, for example, “Insert Column” When you are all done, make sure you save the diagram by right-clicking on the tab and selecting “Save…”
UCF 32ISM 4212 Lab Help with Multi-page diagrams With the diagram open… Right click on the background and select “View Page Breaks”
UCF 33ISM 4212 Lab Save Your Diagram Right-click on the tab, and select save Give your diagram a name The diagram is then available in the Object Explorer under “Database Diagrams”
UCF 34ISM 4212 Lab Create a Relationship Between Two Tables
UCF 35ISM 4212 Lab Inspect a One-to-Many Relationship First, lets look at an example In Northwind… Focus on just the Categories table and the Products table Notice that the Categories table has a CategoryID and that the Products table has a CategoryID There exists a relationship between these two tables Specifically, Products.CategoryID is a Foreign Key that references the Categories.CategoryID which is a Primary Key (con’t on next slide)
UCF 36ISM 4212 Lab Inspect a One-to-Many Relationship (con’t from previous slide) To find evidence of this relationship… Open Northwind’s “Products” table Right-click on the background and select “Relationships” In the “Foreign Key Relationships” pop-up window, find the “Selected Relationships” area that lists the existing relationships Highlight the “FK_Products_Categories” and then expand the part of the window where it says “Tables and Columns Specification” … this will show the tables and attributes. (con’t on next slide) (con’t)
UCF 37ISM 4212 Lab Inspect a One-to-Many Relationship (con’t) Lists the details of the relationship
UCF 38ISM 4212 Lab Creating a One-to-Many Relationship (GUI) First, create two tables that can be used in the example CREATE TABLE MyCategories ( CategoryIDintIDENTITY(1,1)NOT NULL, CategoryNamenvarchar(15)NOT NULL, DescriptionntextNULL, CONSTRAINT PK_MyCategories PRIMARY KEY (CategoryID) ) CREATE TABLE MyProducts ( ProductID INT, ProductName CHAR(20), Price MONEY, CategoryID INT NOT NULL ) (con’t)
UCF 39ISM 4212 Lab Creating a One-to-Many Relationship (GUI) Now create the relationship, where the MyProducts.CategoryID is the FK and the MyCategories.CategoryID is the PK From the Object Explorer, right click and “modify” the MyProducts table Right click on background and select “Relationships” Click “Add” Under “Identity” where it says (name), enter the name of the relationships (e.g. FK_MyProducts_MyCategories) Under “Tables and Columns Specification” click the button that has the ellipsis (“…”) Select a table and field for the Primary Key and the Foreign Key “OK” to save and “Close” to exit “Foreign Key Relationship” pop-up (con’t)
UCF 40ISM 4212 Lab Creating a One-to-Many Relationship (SQL) First, create two tables that can be used in the example CREATE TABLE MyCategories2 ( CategoryIDintIDENTITY(1,1)NOT NULL, CategoryNamenvarchar(15)NOT NULL, DescriptionntextNULL, CONSTRAINT PK_MyCategories2 PRIMARY KEY (CategoryID) ) CREATE TABLE MyProducts2 ( ProductID INT, ProductName CHAR(20), Price MONEY, CategoryID INT NOT NULL FOREIGN KEY REFERENCES MyCategories2 (CategoryID) )
UCF 41ISM 4212 Lab Bonus
UCF 42ISM 4212 Lab Ctrl+K, Ctrl+C to Comment SQL Ctrl+K, Ctrl+U to Uncomment SQL
UCF 43ISM 4212 Lab Another way to see your table’s metadata EXEC sp_help YourTableName Note: the “sp_help” is a stored procedure that comes with the installation. We’ll learn more about stored procedures in a few weeks.
UCF 44ISM 4212 Lab Next Week
UCF 45ISM 4212 Lab Next Topic 1. Introduction to SQL SELECT Statements including the following important SQL keywords: WHERE ORDER BY AS
UCF 46ISM 4212 Lab To Do List 1. Practice 2. Read Ch. 3 (pp ): Statements 3. Read Ch. 4 (pp ): JOINs