Database Fundamentals #30: Create Foreign Keys With T-SQL

Database Fundamentals
You can create foreign keys using TSQL roughly the same way as you created primary keys. You can either use the ALTER TABLE statement to add the foreign key, or, if you already have the parent table created along with it’s primary key, you can use the CREATE TABLE statement to include foreign key constraints. The restrictions for creating foreign keys are still the same when using TSQL. Adding a Foreign Key Using the ALTER TABLE statement is very straight forward as before. This script will create a foreign key relationship between the Personnel.Person table and the Personnel.PersonAddress table: ALTER TABLE Personnel.PersonAddress ADD CONSTRAINT PersonAddress_FK_Person FOREIGN KEY (PersonID) REFERENCES Personnel.Person (PersonID); The ALTER TABLE and ADD CONSTRAINT statements are the same as what you saw before. They respectively refer to…
Read More

Database Fundamentals #21: Using the JOIN Operator, OUTER JOIN

Database Fundamentals, SQL Server
The OUTER JOIN returns one complete set of data and then the matching values from the other set. The syntax is basically the same as INNER JOIN but you have to include whether or not you’re dealing with a RIGHT or a LEFT JOIN. The OUTER word, just like the INNER key word, is not required. OUTER JOIN Imagine a situation where you have a list of people. Some of those people have financial transactions, but some do not. If you want a query that lists all people in the system, including those with financial transactions, the query might look like this: SELECT p.LastName, ft.TransactionAmount, ft.TransactionDate, ft.TransactionTime FROM Personnel.Person AS p LEFT JOIN Finance.FinancialTransaction AS ft ON p.PersonID = ft.PersonID; Except for the addition of the LEFT key word, this…
Read More

Database Fundamentals #20: Using the JOIN Operator, Inner Join

Database Fundamentals
It is entirely possible to try to JOIN two tables on almost any field, as long as the two data types can, in some way, be made to reconcile to each other, you can try to join the tables. But, most database designs assume a much more directly relationship and provide a column or columns in one table that match the identifying column or columns in the other table. INNER JOIN The INNER JOIN will return the parts of both data sets that match. Frequently, what you'll see when joining two tables is the same column name in each table. With that, you have to be sure to identify the owner of each column. I've introduced what is called an alias to make it so I don't have to type…
Read More

Database Fundamentals #17: Learning T-SQL

Database Fundamentals
While SQL Server Management Studio (SSMS) provides a robust graphical user interface (GUI), the commands you're going to use the most to control databases and the data within them in SQL Server are going to be done through T-SQL. Therefore, you really need to spend time learning how to write, read, and edit T-SQL. Previous posts in the Fundamentals series have showed how to INSERT, UPDATE and DELETE data using T-SQL. Next, we're going to learn SELECT. However, I want to show you a crutch you can use as you get started learning how to write T-SQL, the Query Designer window. The instruction on this topic is only meant to provide a mechanism to focus on the more important topic, learning T-SQL. However, this may be an easier path for…
Read More

Database Fundamentals #13: Data Entry Through T-SQL

Database Fundamentals
T-SQL provides lots of functions that help to make data entry through T-SQL much more powerful. Over time you won’t be typing everything into T-SQL directly as we’ll do here. You’ll be able to use stored procedures and parameters to automate the use of scripts. These will also be generated or used by applications. To  start using T-SQL, you need to open a query window. You can do this by right clicking on a database and selecting the “New Query” command from the context menu. This will open a new query window in the main window on your screen. This is basically just a big, open text box into which you can type commands. The INSERT Statement To add rows using T-SQL, the principal statement is the INSERT statement. The…
Read More

Database Fundamentals #11: Why Learn T-SQL

Database Fundamentals
If you've been following along with the previous 10 Database Fundamentals blog posts, you have a SQL Server installed and a database with a table in it. You may have more if you've been practicing. Now would be the time to start adding data to the database, but first, I want to talk about the importance of T-SQL Why T-SQL? The way SQL Server accepts information is very different than most programs you’re used to using. Most programs focus on the graphical user interface as a mechanism for enabling data entry. While there is a GUI within SQL Server that you can use for data entry, and I will do a blog post on it, the primary means of manipulating data within SQL is the Transact Structured Query Language, or…
Read More

Database Fundamentals #10: Modifying Tables

Database Fundamentals
Invariably, either while building a new database, or while maintaining old ones, you will find that the business needs change or that you’ve made a mistake. Either way, you will need to modify the tables that you’ve created. Change is inevitable and luckily SQL Server takes that into account, providing you with mechanisms to modify structures after you’ve created them. Important Warning One of the fundamental concepts of databases is that the data is stored. You can’t simply throw data away when you need to make a change to a table. There are two levels of changes you can make to a table. The first, is a change that won’t affect data in the table. These are simple changes and we’ll cover them in this blog post. The second level…
Read More

Database Fundamentals #9: Schemas As Containers

Database Fundamentals
Schemas are a very useful tool for managing the objects in your database. From security through process, through placement, schemas provide you with another tool to control how your database behaves. Schemas The tables created so far in this blog series have all been attached to a schema, dbo. Depending on how you login to the server and the security settings for your user, this is usually the default schema. A schema is simply a container in which you place objects. Once placed there, a schema is a method for managing the objects it contains. Schemas give you a simple way to control placement of the objects on filegroups. Schemas are a very easy way to manage security. The use of schemas becomes extremely important as your database becomes more…
Read More

Database Fundamentals #8: All About Data Types

Database Fundamentals
SQL Server provides all different kinds of data types in order to give you more flexibility and control for how you store your data. You define these data types as you define the columns in your tables. If you wanted to store information about the date and time that a purchase was made online you’re going to store the values in a column, or columns, that define dates and times in order to ensure accuracy in your data. Choosing a Data Type You could make the data type into one that stores just about anything you give it, such as one of the character types like char or varchar. Do this and you’re going to run into issues when you start running queries against that data. For example, your business…
Read More

Database Fundamentals #6: Create a Table with the SSMS GUI

Database Fundamentals
The whole idea behind databases is to store information, data, in order to look at it later. The place where that data is going to be kept is referred to as a table. A table consists of columns and each column can be defined to store a variety of specific kinds of data. You can make small tables or large ones. The columns on the tables can be made so that they have to have data in them or they can be empty. The choices are yours to make, but you need to know how to set tables up appropriately . In addition to tables, we're going to start learning about columns. Columns can be very generic in nature, supporting all sorts of different kinds of data, or they can be…
Read More