ism 4212 lab creating db tables 02 copyright lars paul linden 2007
TRANSCRIPT
ISM 4212 Lab
Creating DB Tables
02
copyright Lars Paul Linden 2007
UCF 2 ISM 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 3 ISM 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 4 ISM 4212 Lab
Create Tablesand
Drop Tables
UCF 5 ISM 4212 Lab
Two Ways of Creating Tables
Graphical User Interface’s (GUI) “New Table” form Execute SQL in a Query Pane
UCF 6 ISM 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 7 ISM 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 8 ISM 4212 Lab
basic CREATE TABLE syntax
CREATE TABLE Products(
ProductID INT, ProductName CHAR(20), Price MONEY
)
Commas betweencolumn specifications
Parenthesis aroundColumn specifications
Notice how each linehas a field nameand that field’s data type
UCF 9 ISM 4212 Lab
Another wayto create a table with SQL
Open a Query Pane Open the “Template Explorer” Under “Table”, select “Create Table” Replace the <xxxxxxx> as needed
an example of the syntax is on the next slide And, of course, execute
UCF 10 ISM 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 11 ISM 4212 Lab
Drop a Table (SQL)
# Make sure you are using the correct database
DROP TABLE YourTableName
UCF 12 ISM 4212 Lab
Setting table properties
UCF 13 ISM 4212 Lab
table properties
In Object Explorer right-click on the table name Select “Properties”
UCF 14 ISM 4212 Lab
More about creating DB tables
UCF 15 ISM 4212 Lab
Look at an existing table
In “Northwind” Open the “Shippers” table
Using Rgt-Click “Modify” Note the following:
A. Allow NullsB. Identify SpecificationC. Primary Key iconD. Default values (not shown)
Now we covers these four topics…
CA
B
UCF 16 ISM 4212 Lab
CREATE TABLEwith NULL or NOT NULLCREATE TABLE Products2(
ProductID INT NOT NULL, Name CHAR(20) NOT NULL, Price MONEY NOT NULL,Description CHAR(100) NULL
)
After executed, you can viewthe table and check what you createdfor the Description field:
A.
UCF 17 ISM 4212 Lab
What is the impactof 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 18 ISM 4212 Lab
CREATE TABLEwith 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 19 ISM 4212 Lab
CREATE TABLEwith an IDENTITY
CREATE TABLE Products3(ProductID INT IDENTITY NOT NULL, Name CHAR(20) NOT NULL, Price MONEY NOT NULL,Description CHAR(100) NULL
)After executed, you can viewthe table and checkthe result at the bottomunder “Column Properties”:
B.
UCF 20 ISM 4212 Lab
What is the impactof 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 21 ISM 4212 Lab
One little trick to knowabout the IDENTITY
You can specify how the identity is going to behave: IDENTITY (<seed>, <increment>)
CREATE TABLE Products4
(
ProductID INT IDENTITY(10,5) NOT NULL,
Name CHAR(20) NOT NULL,
Price MONEY NOT NULL,
Description CHAR(100) NULL
)
B.
UCF 22 ISM 4212 Lab
One little trick to knowabout the IDENTITY
When viewing the table,in the “Column Properties” section,expand the “Identity Specification”
The detailsof the Identity
B.(con’t)
UCF 23 ISM 4212 Lab
CREATE TABLEwith a PRIMARY KEY
CREATE TABLE Categories(
CategoryID int NOT NULL,CategoryName nvarchar(15) NOT NULL,Description ntext NULL,CONSTRAINT PK_Categories PRIMARY KEY (CategoryID)
)
C.
the nameof thisconstraint
the typeof constraint
the fieldthat is the PK
UCF 24 ISM 4212 Lab
CREATE TABLEwith a PRIMARY KEY
After executed, you can viewthe table and see a checkfor the PK icon:
C.
UCF 25 ISM 4212 Lab
What is the impactof a PRIMARY KEY?
For starters, it is a constraint “Enforces uniqueness for the purpose of identifying a
row.”-Turley, page 331
C.
UCF 26 ISM 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 27 ISM 4212 Lab
Evidence of the Default valuesD.
Select the field
then look below
UCF 28 ISM 4212 Lab
Diagram
UCF 29 ISM 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 30 ISM 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 31 ISM 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 32 ISM 4212 Lab
Help with Multi-page diagrams
With the diagram open… Right click on the background and select “View Page Breaks”
UCF 33 ISM 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 34 ISM 4212 Lab
Create a RelationshipBetween Two Tables
UCF 35 ISM 4212 Lab
Inspecta 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 36 ISM 4212 Lab
Inspecta 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 37 ISM 4212 Lab
Inspecta One-to-Many Relationship
(con’t)
Lists the detailsof the relationship
UCF 38 ISM 4212 Lab
Creating a One-to-Many Relationship (GUI)
First, create two tables that can be used in the exampleCREATE TABLE MyCategories(
CategoryID int IDENTITY(1,1) NOT NULL,CategoryName nvarchar(15) NOT NULL,Description ntext NULL,CONSTRAINT PK_MyCategories PRIMARY KEY (CategoryID)
)CREATE TABLE MyProducts(
ProductID INT, ProductName CHAR(20), Price MONEY,CategoryID INT NOT NULL
) (con’t)
UCF 39 ISM 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 40 ISM 4212 Lab
Creating a One-to-Many Relationship (SQL)
First, create two tables that can be used in the exampleCREATE TABLE MyCategories2(
CategoryID int IDENTITY(1,1) NOT NULL,CategoryName nvarchar(15) NOT NULL,Description ntext NULL,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 41 ISM 4212 Lab
Bonus
UCF 42 ISM 4212 Lab
Ctrl+K, Ctrl+C to Comment SQLCtrl+K, Ctrl+U to Uncomment SQL
UCF 43 ISM 4212 Lab
Another wayto 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 44 ISM 4212 Lab
Next Week
UCF 45 ISM 4212 Lab
Next Topic
1. Introduction to SQL SELECT Statements
including the following important SQL keywords:
WHERE ORDER BY AS
UCF 46 ISM 4212 Lab
To Do List
1. Practice
2. Read Ch. 3 (pp. 41-52): Statements
3. Read Ch. 4 (pp. 79-89): JOINs