2. Page | 1
Creating valid databases can be hard if you do not know what they consist of, how they work, or what they
are for. This portfolio goes in depth to just that. There are many examples and images to show the proper
way to set up a database and all its components. This will help you to get basic knowledge of the do’s and
don’ts of SQL Server and XML.
3. Page | 2
Table of Contents
DATABASE STRUCTURE AND ORGANIZATION ..........................................................................................................................5
ENTITY RELATIONSHIP DIAGRAMIN 3RD
NORMAL FORM.....................................................................................................................5
Figure 1 Relational Diagram Chen's Database Notation....................................................................................................... 5
Relationships ...........................................................................................................................................................................6
Figure 2 Relationship Placement.......................................................................................................................................... 6
Foreign Keys.............................................................................................................................................................................6
Figure 3 Foreign Keys............................................................................................................................................................ 7
Primary Keys ............................................................................................................................................................................7
Figure 4 Primary Key............................................................................................................................................................. 7
Data Types ...............................................................................................................................................................................7
Figure 5 Data Types............................................................................................................................................................... 8
STRUCTURED QUERY LANGUAGE ................................................................................................................................................8
DATA DEFINITION LANGUAGE............................................................................................................................................................8
Create a database ..................................................................................................................................................................8
Figure 6 SQL Server Management Studiolocal connect....................................................................................................... 9
Figure 7 Create Database...................................................................................................................................................... 9
Create all of the tables...........................................................................................................................................................9
Figure 8 Create Instructor Table......................................................................................................................................... 10
Figure 9 Create Student Table ............................................................................................................................................ 10
Figure 10 Create Semester Table........................................................................................................................................ 11
Figure 11 Create Course Table............................................................................................................................................ 11
Figure 12 Create Social Security Table................................................................................................................................ 12
Figure 13 Create Enrollment Table..................................................................................................................................... 12
Apply all primary keys..........................................................................................................................................................12
Figure 14 Instructor Primary Key........................................................................................................................................ 13
Figure 15 Course Primary Key............................................................................................................................................. 13
Figure 16 Enrollment Primary Key...................................................................................................................................... 14
Figure 17 Semester Primary Key......................................................................................................................................... 14
Figure 18 Social Security Primary Key................................................................................................................................. 15
Figure 19 Student Primary Key ........................................................................................................................................... 15
Apply all foreign keys ...........................................................................................................................................................15
Figure 20 Foreign Keyfor Student on Social Security Table............................................................................................... 16
Figure 21 Instructor Foreign Keyon Course Table.............................................................................................................. 16
Figure 22 Course Foreign Key on Enrollment Table............................................................................................................ 17
Figure 23 Semester Foreign Key on Enrollment Table........................................................................................................ 17
Figure 24 Student Foreign Keyon Enrollment Table.......................................................................................................... 17
Database Diagram in Standard view ................................................................................................................................17
Figure 25 List of Tables to Add to Database Diagram......................................................................................................... 18
Figure 26 How to Change to Standard Table View............................................................................................................. 18
Figure 27 Database Diagram............................................................................................................................................... 19
DATA MANIPULATION LANGUAGE...................................................................................................................................................19
Insert data into each table ..................................................................................................................................................19
Figure 28 Populated Instructor Table ................................................................................................................................. 19
4. Page | 3
Figure 29 Populated Student Table..................................................................................................................................... 20
Figure 30 Populated Semester Table.................................................................................................................................. 20
Figure 31 Populated Course Table...................................................................................................................................... 20
Figure 32 Populated Social Security Table.......................................................................................................................... 21
Figure 33 Adding Self to Student Table............................................................................................................................... 21
Figure 34 Adding Self to Social Security Table.................................................................................................................... 21
Figure 35 Populated Enrollment Table ............................................................................................................................... 22
Read all data for each table................................................................................................................................................22
Figure 36 Select Statement for Course Table ..................................................................................................................... 22
Figure 37 Select Statement for Enrollment table............................................................................................................... 23
Figure 38 Select Statement for Instructor Table................................................................................................................. 23
Figure 39 Select Statement for SemesterTable ................................................................................................................. 24
Figure 40 Select Statement for Social Security Table......................................................................................................... 24
Figure 41 Select Statement for Student Table.................................................................................................................... 25
Update data and read tablet result set ............................................................................................................................25
Figure 42 Instructor Update................................................................................................................................................ 25
Figure 43 Student Name Update ........................................................................................................................................ 26
Delete data ............................................................................................................................................................................26
Figure 44 Delete Social SecurityRecord ............................................................................................................................. 26
Figure 45 Delete Student Record........................................................................................................................................ 27
JOINS............................................................................................................................................................................................27
Figure 46 Inner Join Student and Social Security Tables .................................................................................................... 27
Figure 47 Full Outer Join Student, Semester, and Course Tables....................................................................................... 28
Figure 48 Left Join Student, Social Security, Course, and Enrollment Tables..................................................................... 28
Figure 49 Right Join Instructor and Course Tables ............................................................................................................. 29
XML ..................................................................................................................................................................................................29
CREATE XML..................................................................................................................................................................................29
Prolog......................................................................................................................................................................................29
Figure 50 XML Prolog.......................................................................................................................................................... 29
Root Element.........................................................................................................................................................................29
Figure 51 XML Root Element.............................................................................................................................................. 29
Elements .................................................................................................................................................................................30
Figure 52 XML Elements...................................................................................................................................................... 30
Attributes ...............................................................................................................................................................................30
Figure 53 XML Attribute...................................................................................................................................................... 30
Descriptive Comments .........................................................................................................................................................30
Figure 54 XML Descriptive Comments................................................................................................................................ 30
Add Data ................................................................................................................................................................................31
Figure 55 XML Sample Data................................................................................................................................................ 31
XPATH............................................................................................................................................................................................31
XML Queries...........................................................................................................................................................................31
Select the students first and last names...........................................................................................................................31
Figure 56 XML Query 1........................................................................................................................................................ 31
Instructor first name where the email address is mlbrun@ntc.edu ............................................................................32
Figure 57 XML Query 2........................................................................................................................................................ 32
RESULTS OF QUERY.........................................................................................................................................................................32
5. Page | 4
Select the students first and last names...........................................................................................................................32
Figure 58 Query 1 Results................................................................................................................................................... 32
Figure 59 XML Query 2 Results........................................................................................................................................... 33
6. Page | 5
Database Structure and Organization
Entity Relationship Diagram in 3rd
NormalForm
An entity relationship Diagramis a group of data that relates to other information within the database. In
order to createit you have to make sure your data is in 3rd Normal Form. For instance, I have a list of data
that I need to put in 3rd Normal Form. To do that first, I need to make sure it’s in 1st Normal Form. 1st Normal
Form has to follow the very basic rules:
Make sure data has its own subject
No duplicating Data
Each group has own table
Set primary key
Now that you have the rules you can look over your data and apply them. Once you do that, you can work on
2nd Normal Form. Just as 1st Normal Form, 2nd Normal Form also follows some rules:
Must be in 1st Normal Form
Create tables for data that repeats
Create relationships between tables
Once it is in 2nd Normal Form you can finally put it in 3rd Normal Form. The rules for 3rd Normal Form are:
Must be in 2nd Normal Form
Removing any data that can be used elsewhere
These arethe steps needed to put data in to 3rd Normal form.
FIGURE 1RELATIONAL DIAGRAM CHEN'S DATABASE NOTATION
7. Page | 6
Relationships
To create a successful relationship, you must know and understand the role of both parties and the
boundaries and rules of the relationship. To show relationships for a database you use Visio’s Chan’s
DatabaseNotation (See Figure 1). There arethree kinds of relationships, one to one, one to many, and many
to many. One to one relationships are those that directly relate one record in a table to exactly one in
another table, meaning it cannot be used in another row. For example, one dog belongs to one person or
family, a car is made by one company. For my illustration, I will be creating a database for a small charter
school, my one to one would be one student to one social security number. The one to many relationship
means that each of the rows in a relating table can relate to one or many rows in another table. This allows
frequently used data to be saves once and referenced several times. As one State has many people living in it
a state can be considered a one to many relationship, as well as a city name can be the same in many states.
In my case, an instructor can teach multiple courses and one course can be offered multiple times in a
semester. The most common relationship is a many to many. One or many rows in one table associate with
one or many rows in another. It represents the real-life relationship of the items and their tables. Many
stores have many products, many departments, in a store, has “x” amount of employees aresome instances.
My many to many relationships aremany students enroll in multiple courses and many courses areoffered in
multiple semesters. Now let’s createour relationships. I have six tables which have many relationships. When
relating tables ask yourself, does every blank have a blank? For example, does every student have a social
security number or does every social security number have a student? The correct answer is every social
security number has a student, because there are some cases, i.e. exchange students, that may not have a
social security number. So a student would relate to the social security number and to show this you need
primary keys and foreign keys (See Figure 2).
FIGURE 2RELATIONSHIP PLACEMENT
Foreign Keys
Foreign keys are a way of ensuring data reliability and the appearance of the relationship between tables.
They createa dependency and must match, or be set to NULL, on both tables. Before you can set a foreign
key you must first set your primary keys. Tables containing foreign keys areknown as child tables and in turn
the referenced table is the parent table. Foreign keys have very important roles in a relational database. One
of those is to show the real-world relationships are reflected. Another is to reconstruct the broken down
8. Page | 7
tables. Not every table will have a foreign key but every table should relateto another table in some way.
Some tables may have multiple foreign keys; this is the only way some tables can relateto one another. In
Visio to show foreign keys I drew a square then typed FK in side and shaded the square and put it next to the
foreign key. For my tables some relatedirectly to each other and others are related only by one table. My
Enrollment table relates to three other tables (See Figure 3).
FIGURE 3FOREIGN KEYS
Primary Keys
Every table in a relationship database needs to have a primary key. A primary key is a unique identifier for
that row of data. Each row will be identified by its own unique primary key value. Primary keys cannot be
NULL because they distinguish the row. A table can only have one primary key. The primary key value cannot
be changed. When you insert new data you must also insert a new primary key value. The primary key is
usually the first column in a database. In Visio’s Crow’s Foot Notation, it is the first entry under the table
name (See Figure 4).
FIGURE 4PRIMARY KEY
Data Types
A data type is a classification to identify data. There are many data type to choose from, these types define
how your data is entered and what kind of data can be entered. Common types include:
Integers
Booleans
Characters
Alphanumeric strings
An integer type represents whole numbers, this is usually used for primary and foreign keys. Booleans are
true or false types, meaning only two values are possible. Alphanumeric string types are used for a sequence
of characters. Characters could be letters, numbers, blank space, and punctuation marks. There is also
9. Page | 8
different type for dates and times. These types can be very general of very specific down to the nearest 100th
nanosecond. Some data types use a precision, where you choose the amount of data that it can hold. This
varies in size for all the data. In the diagram the data type is on the right side and the precision is in
parentheses (See Figure 5).
FIGURE 5DATA TYPES
Structured Query Language
Data Definition Language
Data definition language define the different structures within a database. They areused in statements to
create, modify, and remove a database. Data types and fields are expressed by a declarative syntax used by
data definition language. Structured query language uses imperative verbs whose effect is to alter the
schema of the database by adding, changing, or deleting tables or other information. These statements can
be easily mixed with other Structured query language so it’s not an independent language.
Create a database
Now that our data is in 3rd Normal Form we can use it to createa database. Open SQL Server Management
Studio and connect to your local server (See Figure 6).
10. Page | 9
FIGURE 6SQL SERVER MANAGEMENT STUDIO LOCAL CONNECT
Once connected you will open a new query window, by clicking on New Query. When it opens you will select
were you want to store your database by using the word USE. When typing Structured query language, you
need to make sure your statements are all capitalized. Now use the master and then press enter and use the
statement CREATE DATABASE and type your database name (See Figure 7). Using a semicolon closes that
statement. Now click Execute or f5 key to createyour database. On the bottom where the tab says messages
is where it will tell you if it was completed successfully or if there were errors in completing your statement.
FIGURE 7CREATE DATABASE
Create all of the tables
Now that you have created your database you can create your tables. This works almost like creating a
database with a few changes. When creating your tables, you need to create them in a particular order. First
you need to figure out what tables relate to each other and how. The first tables you create shouldn’t have
any foreign keys. The next ones should relateonly to the first few tables, and so on (See Figures 8-13). Now
your USE statement, like when you created your database, will now be the name of the database you just
created. Next you will use the statement CREATE TABLE followed by dbo.TableName; dbo is the default
11. Page | 10
schema. Then in parentheses you add your column names, data types, and whether they areNULL or NOT
NULL. Press Execute or f5 and check your messages to ensure your table was created (See Figure 8).
FIGURE 8CREATE INSTRUCTOR TABLE
FIGURE 9CREATE STUDENT TABLE
13. Page | 12
FIGURE 12CREATE SOCIAL SECURITY TABLE
FIGURE 13CREATE ENROLLMENT TABLE
Apply all primary keys
Now that you have created your tables in your database you can add your primary keys. This uses a ATLER
TABLE, ADD CONSTRAINT, AND PRIMARY KEY statements (See Figures 14-19). There is no particular order to
set primary keys. First your USE DatabaseName, then ALTER TABLE dbo.TableName, ADD CONSTRAINT
PK_ConstraintName, and lastly what column your primary key is in parentheses. This is the same syntax
throughout the tables only the constraint name and the column name will change.
16. Page | 15
FIGURE 18SOCIAL SECURITY PRIMARY KEY
FIGURE 19STUDENT PRIMARY KEY
Apply all foreign keys
Foreign keys are essentially the same as the primary key only you have to change it to the constraint name
and FOREIGNKEY and add REFERENCES to it (See Figure 20-24). The constraint name becomes
FK_constraintname and this name has to be unique from all others. My Student table gets references on the
Enrollment table and the SocialSecurity table, my constraint name for each table has to differ from the other
so one might be FK_StudentIDEnrollment and the other might be FK_StudentID (See Figures 20 and 24).
17. Page | 16
Foreign keys also have to in the same order that you created the tables in. For my example, my SocialSecurity
and Course tables must be first then the Enrollment table.
FIGURE 20FOREIGN KEY FOR STUDENT ON SOCIAL SECURITY TABLE
FIGURE 21INSTRUCTOR FOREIGN KEY ON COURSE TABLE
18. Page | 17
FIGURE 22COURSE FOREIGN KEY ON ENROLLMENT TABLE
FIGURE 23SEMESTER FOREIGN KEY ON ENROLLMENT TABLE
FIGURE 24STUDENT FOREIGN KEY ON ENROLLMENT TABLE
Database Diagram in Standard view
Now you can createa database diagram, by right-clicking on the database diagram folder and selecting New
DatabaseDiagram. Next you will have a list of the tables that you can add, by holding shift while selecting you
19. Page | 18
can select all the tables at once or add each table individually (See Figure 25). Once all tables are added you
click close and you right-click each table or select them all, by holding shift, and click Table view, then
Standard (See figure 26). Now you can move them around to your content and should end up looking similar
to Figure 27.
FIGURE 25LIST OF TABLES TO ADD TO DATABASE DIAGRAM
FIGURE 26HOW TO CHANGE TO STANDARD TABLE VIEW
20. Page | 19
FIGURE 27DATABASE DIAGRAM
Data Manipulation Language
Data Manipulation languageis syntax used to select, insert, update, and delete data. In SQL it consists of
change statements, which alters stored data but not the schema or database objects.
Insert data into each table
The INSERT statement is used to insert a new record or multiple records into stated table. To do this you start
with your database then INSERT INTO dbo.TableName (ColumnOne, ColumnTwo,…) then VALUES (data for
first row), (data for second row), and continuing with all your data entries. When completed click Execute or
press f5 and if everything was entered correctly the message will say how many rows were affected (See
Figures 28-32). Entering data has to be in correct order in the table. Also any tables with foreign keys won’t
be able to be populated until the referencing table is complete.
FIGURE 28POPULATED INSTRUCTOR TABLE
22. Page | 21
FIGURE 32POPULATED SOCIAL SECURITY TABLE
You can also add data to existing tables by using an INSERT statement. I am adding myself to my database
and going to enroll in a class and enroll one other student in a class as well (See Figures 33-35).
FIGURE 33ADDING SELF TO STUDENT TABLE
FIGURE 34ADDING SELF TO SOCIAL SECURITY TABLE
23. Page | 22
FIGURE 35POPULATED ENROLLMENT TABLE
Read all data for each table
There are a few different ways to read all the data in a table. One is the SELESCT * statement, another is a
SELECT statement with each column name after it. The results of the SELECT statement will show in the
messages box (See Figures 36-41). One more way to read your data is by right-clicking the table and selecting
Select Top 1000 Rows. Although if your table has more than a thousand rows you won’t be able to see all the
data. Data reading can be done in any order that you choose. You can also choose what order your data will
result in with some additional syntax.
FIGURE 36SELECT STATEMENT FOR COURSE TABLE
24. Page | 23
FIGURE 37SELECT STATEMENT FOR ENROLLMENT TABLE
FIGURE 38SELECT STATEMENT FOR INSTRUCTOR TABLE
25. Page | 24
FIGURE 39SELECT STATEMENT FOR SEMESTER TABLE
FIGURE 40SELECT STATEMENT FOR SOCIAL SECURITY TABLE
26. Page | 25
FIGURE 41SELECT STATEMENT FOR STUDENT TABLE
Update data and read tablet result set
Instead of deleting data to update data SQL lets you use an UPDATE statement. This allows you to easily
update outdated information, like when someone moves or gets a new phone number. Some parts of the
syntax may look similar to the SELECT statement there aresome differences. When using an UPDATE
statement, you need to also use a WHERE clause. A WHERE clause is stating where the update will be,
without it you will update the whole table instead of just one record. Start with your USE Database then
UPDATE dbo.TableName then SET What you are going to update = what the update is and then WHERE the
update is to take place. You can also add a SELECT statement to your UPDATE statement so that when it
updates it will also read the table. Here aretwo examples of UPDATE and SELECT statements:
FIGURE 42INSTRUCTOR UPDATE
27. Page | 26
FIGURE 43STUDENT NAME UPDATE
Delete data
A DELETE statement is used to delete a whole record of data. Let’ssay a product is no longer available and
will never be available again, instead of leaving this record in a database taking up space you can use a
DELETE statement to remove it. This helps maintain the size of your database and also give you another place
to add new records. This is another statement that requires a WHERE clause, as if you are not careful you
could delete your entire table. A DELETE statement cannot be undone the only way to recover the data is to
completely reenter everything. First is your USE DatabaseNamenext is DELETE FROM dbo.TableName and
lastly WHERE it is to take place. As you can see I have deleted a student and their social security number from
my database (See Figures 44 and 45). Make sure you delete foreign keys before you delete the primary keys
or you will get an error code.
FIGURE 44DELETE SOCIAL SECURITY RECORD
28. Page | 27
FIGURE 45DELETE STUDENT RECORD
JOINS
Joins let you unite two or more tables or rows from tables together. If a table does not have the information
that another one has that value will show up as NULL. There are four types of joins:
INNER JOIN
FULL JOIN
LEFT JOIN
RIGHT JOIN
The INNER JOIN is the most common, as it returns all rows when there is a match in both tables (See Figure
46). This means that it will only show data that matches.
FIGURE 46INNER JOIN STUDENT AND SOCIAL SECURITY TABLES
FULL JOINS return all the data in both tables and is sure to have NULL data for unmatched records (See Figure
47). Both right and left table may have NULL data with this type of join.
29. Page | 28
FIGURE 47FULL OUTER JOIN STUDENT, SEMESTER, AND COURSE TABLES
A LEFT JOIN returns all rows from the left table, and matched rows from the right (See Figure 48). Again if
there is no match in the right table the value will be NULL. The order in which you join the tables is very
important for this join as well as the RIGHT JOIN, make sure you are joining your tables in the right order.
FIGURE 48LEFT JOIN STUDENT, SOCIAL SECURITY, COURSE, AND ENROLLMENT TABLES
A RIGHT JOIN is basically the opposite of a LEFT JOIN (See Figure 49). Although just like the LEFT JOIN you
have to be very careful of the order you are joining the tables.
30. Page | 29
FIGURE 49RIGHT JOIN INSTRUCTOR AND COURSE TABLES
XML
XML stands for Extensible Markup Language, it was designed to store and transport data and be both human
and machine readable. It uses tags that are made up by the author of the XML document. XML stores data in
plain text so that it provides a software and hardware independent way of storing, transporting, and sharing
data. As well as making upgrading easier without losing data.
Create XML
I am using Note Pad ++ to createmy XML document although there areother programs that you can use as
well. To start you open Note Pad ++ and Save the document as an XML document. Then you can start typing
your document.
Prolog
The prolog comes before the root element and it includes information that applies to the whole document.
XML does not require a prolog but if you do have one it must come first (See Figure 50).
FIGURE 50XML PROLOG
Root Element
All XML documents must have a root element. This is the parent element for the entire document. It comes
after the prolog, if you have one (See Figure 51).
FIGURE 51XML ROOT ELEMENT
31. Page | 30
Elements
All XML elements must have a closing tag. This can be a self-closing tag or a separate closing tag. Tags arealso
case sensitive so both start and end tags must have the same casing. Elements must be inside the start and
end tags. Here is an example of and XML Element with start and end tags:
FIGURE 52XML ELEMENTS
Attributes
XML attributes must be quoted. They are name-value sets connected with elements and a main part in an
XML document. They entail a name and a value separated by an equal sign (See Figure 53). Empty quotes are
the very least that is needed in an XML attribute (See Figure 52).
FIGURE 53XML ATTRIBUTE
Descriptive Comments
Comments are used to give a remark. They have a distinct syntax that has an exclamation point and four total
dashes. It also appears green on the document. In my XML document I have used them to label the parts of
the document (See Figure 54).
FIGURE 54XML DESCRIPTIVE COMMENTS
32. Page | 31
Add Data
Once you have your syntax set up you can input your data (See Figure 55). Make sure your attributes are
inside the quotes and your elements are between the start and end tags.
FIGURE 55XML SAMPLE DATA
Xpath
Xpath is a language used to find information in an XML document. It uses path expressions to navigate an
XML document. Without knowledge of Xpath you will not be able to createXSLT documents.
XML Queries
Select the students first and last names.
FIGURE 56XML QUERY 1
33. Page | 32
As you see in Figure 56, there are many parts to the query. First is the root element, next is the element, then
the attribute, and lastly what you are wanting. Because you want not only the first names but also the last
names you must separate the query with a pipe then the root element and so on with the path that you
need.
Instructor first name where the email address is mlbrun@ntc.edu
FIGURE 57XML QUERY 2
In the query above, figure 57, it has a where clause in it to find a selected element. So the path has to show
that by using brackets. This tells Xpath that you are looking for that specific element.
Results of Query
Once you have your query in the box, you can click test path and it will show you the results of that query or
it will show no results. If it shows no results, there may be an error in your query.
Select the students first and last names.
FIGURE 58QUERY 1RESULTS
In figure 58, it shows you the first and last names of the students. So, xpath used the query to navigate
through the document to find exactly what was stated.
34. Page | 33
Instructor first name where the email address is mlbrun@ntc.edu
FIGURE 59XML QUERY 2 RESULTS
As you can see in figure 59, only one element is listed as in the query only one was requested. Xpath
navigated through the query to find the exact element that was needed.
Here arethe basics to SQL Server and XML databases. Learning how to use SQL Server and XML in the IT field
will give you a greater understanding of how information technology runs. Being able to apply these skills in
IT can help to give you an edge over the competition.