Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007.

Similar presentations


Presentation on theme: "ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007."— Presentation transcript:

1 ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007

2 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

3 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

4 UCF 4ISM 4212 Lab Create Tables and Drop Tables

5 UCF 5ISM 4212 Lab Two Ways of Creating Tables Graphical User Interface’s (GUI) “New Table” form Execute SQL in a Query Pane

6 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”

7 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

8 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

9 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

10 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

11 UCF 11ISM 4212 Lab Drop a Table (SQL) # Make sure you are using the correct database DROP TABLE YourTableName

12 UCF 12ISM 4212 Lab Setting table properties

13 UCF 13ISM 4212 Lab table properties In Object Explorer right-click on the table name Select “Properties”

14 UCF 14ISM 4212 Lab More about creating DB tables

15 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

16 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.

17 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.

18 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.

19 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.

20 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.

21 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.

22 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)

23 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

24 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.

25 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.

26 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.

27 UCF 27ISM 4212 Lab Evidence of the Default values D. Select the field then look below

28 UCF 28ISM 4212 Lab Diagram

29 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

30 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

31 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…”

32 UCF 32ISM 4212 Lab Help with Multi-page diagrams With the diagram open… Right click on the background and select “View Page Breaks”

33 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”

34 UCF 34ISM 4212 Lab Create a Relationship Between Two Tables

35 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)

36 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)

37 UCF 37ISM 4212 Lab Inspect a One-to-Many Relationship (con’t) Lists the details of the relationship

38 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)

39 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)

40 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) )

41 UCF 41ISM 4212 Lab Bonus

42 UCF 42ISM 4212 Lab Ctrl+K, Ctrl+C to Comment SQL Ctrl+K, Ctrl+U to Uncomment SQL

43 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.

44 UCF 44ISM 4212 Lab Next Week

45 UCF 45ISM 4212 Lab Next Topic 1. Introduction to SQL SELECT Statements including the following important SQL keywords: WHERE ORDER BY AS

46 UCF 46ISM 4212 Lab To Do List 1. Practice 2. Read Ch. 3 (pp. 41-52): Statements 3. Read Ch. 4 (pp. 79-89): JOINs


Download ppt "ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007."

Similar presentations


Ads by Google