Home > Articles > Data > SQL Server

Using the Data Definition Language (DDL) to Create Databases and Objects

With the new features of Query Analyzer, getting the basic outline of the common database objects that you need to create is very easy. With a couple of mouse clicks and some fill-in-the-gaps programming, you can have a database created in only a couple of moments.

The important part of this process though is what to put in the gaps. Understanding how a CREATE DATABASE statement is built is just as important (if not more) than just entering the code.

In this section we will go through the CREATE DATABASE statement rather than use the templates that SQL Server 2000 provides.

First though we need to clarify a couple of points about SQL. As I said earlier, SQL is a standard language for manipulating data and databases. It is based on mathematical set theory. But two distinct parts exist to SQL—the Data Definition Language (DDL) and the Data Manipulation Language (DML).

DDL is used to create database objects and define how the data will look in the database. For example, through the DDL we can not only create databases, tables, views and so on, but we can also delete them (as in the previous example). It is easy to spot the DDL because the SQL statement usually begins with CREATE, ALTER, or DROP.

DML is used to manipulate the data within a database. For example, through the DML we cannot only insert new rows of data but we can also delete them (as in the previous example). It is easy to spot the DML because the SQL statement usually begins with SELECT, INSERT, UPDATE, or DELETE.

With this in mind, if you look at the Transact-SQL statement that we entered to delete our database, you will see that it is a mixture of DDL and DML. The DROP statement in line 4 is DDL and the SELECT statement in line 3 is DML.

We now have an idea of what DDL and DML are. Let us create our SQLSpyNet database (once again) with a Transact-SQL DDL statement and really get this show on the road!

The CREATE DATABASE statement is a complex beast and has many parameters that you can specify. Because of the limitations of this book (namely size!), we are only going to cover the options that are the most relevant to us. The code in Listing 3.2 will re-create our SQLSpyNet database for us.

Listing 3.2—Creating Our SQLSpyNet Database Through Transact-SQL Code

1:USE master
3a:ON PRIMARY (NAME = 'SQLSpyNet_Data',
3d:SIZE = 5MB,
3e:FILEGROWTH = 10%)
4: LOG ON (NAME = 'SQLSpyNet_Log',
4c:SIZE = 5MB,
4d:FILEGROWTH = 10%)

You will need to replace the x:\ with the path on which you would like to create the data file and transaction log.

Tip - To get the file path that your previous database used, copy the file paths that you saved earlier. If you didn't save them, you will need to enter them where the x:\ is.

After you have entered the file paths you can run the code. SQL Server 2000 will return at least two messages similar to the following:

1: The CREATE DATABASE process is allocating 5.00 MB on disk 'SQLSpyNet_Data'.
2: The CREATE DATABASE process is allocating 5.00 MB on disk 'SQLSpyNet_Log'.

Let's examine the code that you have entered.

  • Line 1 specifies to Query Analyzer that it switch to the master database.

  • Line 2 forces the execution of the code in line 1. Use the GO command whenever you would like code to be fired immediately instead of waiting until the whole block is executed. This is a unique feature of all versions of SQL Server. Some other RDBMSs use a semicolon (;) to execute a block of code immediately. Through different options we can change this, but we don't really need to unless using code from another RDBMS.

  • Line 3 is the actual DDL that creates the database. The only parameter required is the database name. We could create a database by only specifying a name. Unfortunately though, this does not allow us enough flexibility.

  • Line 3a specifies the filegroup (PRIMARY, the default) that will be used and the data filename. The bracket denotes the beginning of the data file parameters. The NAME = allows you to set the name that SQL Server 2000 will use to refer to the data file for this database.

  • Lines 3b and 3c specify the location of the data file.

  • Lines 3d and 3e specify the initial size of the data file and the size by which it grows. If you leave off the % sign, the file growth size will be in MBs (the default). The end bracket denotes the end of the data file parameters.

  • Line 4 specifies the transaction log parameters. The bracket denotes the beginning of the transaction log parameters. The NAME = allows you to set the name that SQL Server 2000 will use to refer to the transaction log for this database.

  • Lines 4a, 4b, 4c and 4d—These parameters are the same as they are for the data file (see previous).

  • Line 5 executes the previous Transact-SQL block from the last GO statement; that is, everything from CREATE DATABASE down.

If you now refresh your Databases folder in Enterprise Manager or Object Browser in Query Analyzer, you will see the SQLSpyNet database. Now I am sure a smart person like yourself found that easy, didn't you?

Just perform a quick check on your newly created database (by looking at the database properties, remember? If not look at "The Properties Tab," earlier in this chapter.), and make sure the options for the database fit our needs that we specified earlier. If you find any discrepancies, make the necessary changes.

As I mentioned earlier, we can specify many other parameters when creating a database through Transact-SQL code. We can specify that the database is for data loading only, the collation to be used, and the filegroups that the database will use. As you can see, developing a database through Transact-SQL code is very flexible but definitely a little harder than using the UI through Enterprise Manager.

We will now briefly look at tables and discuss some more database concepts.

Building Tables for Our Spies

Tables are the key to all databases. They enable us to store related information for retrieval at a later stage. Without tables we would not be able to store and retrieve the information that is so critical to our databases, so we wouldn't have a database!

A table will allow us to store a spy's name, address, date of birth, and so on, but we can also store the activities that our spies are involved in.

Tables can be related to each other, unrelated, or only used on a temporary basis. They are very flexible, and a well-designed table will help to ensure that information is stored accurately and concisely.

Three main types of tables are used within SQL Server 2000:

  • User-defined tables—Developers who want to store user-defined information (such as a spy's details) within a table create these. They are easily identifiable in SQL Server 2000 Enterprise Manager because their type is defined as User. Within the Object Browser in Query Analyzer, they are listed in a separate folder named User, under the main Tables folder.

  • System tables—These are used by the internal workings of SQL Server 2000 to manage the database objects. They are characteristically identifiable by the "sys" naming convention; for example, sysobjects. This table contains a list of database objects within a database.

  • Temporary tables—As the name suggests, these tables are created only on a temporary basis. They are characterized by the hash (#) symbol in front of their names. Different types of temporary tables have different levels of scope.

This gives you a brief overview of the tables that are available in SQL Server 2000. We will work with them more as we develop the SQL Spy Net application further.

But why have tables? Tables are modeled on real-world objects (people, spies, bad guys) and events (activities). We have already seen this with the ERD we partially designed earlier, in Chapter 1.

But tables that are not related provide us with few benefits. We might as well have a data file on a single PC! Defining a relationship between our tables is the key to having an effective and informative data store.

We can implement relationships between tables that model the relationships in a real-world scenario. For example, a spy can battle a bad guy to prevent world domination. We can then model and implement this relationship in our database to represent what happens when a spy battles a bad guy.

Note - Of course, all this spy business might not seem too practical when you're ready to work with a real-world client, unless you're working for military intelligence! But, everything we discuss here applies to business relationships such as salespeople to customers, customer address information, and dates and items in sales data, for example. But then, you'll have the rest of your career to create good ol' boring sales records, so enjoy the fun while you can. I'll let you decide who the "bad guys" are in your world.

Relationships also help us enforce integrity of our data. After we define a relationship, through primary and foreign keys, we can specify that in the table that has the foreign key (the child), it can only contain a row of data if a value exists in the parent table (we will prove this after we have built our tables).

This prevents us from entering a spy in our Spy table without them first existing in the Person table.

Revisiting the Analysis of the SQL Spy Net Application

So far in our application development we have decided to implement the Spy table and the BadGuy table as subtypes of a person table. We have also implemented the Activity table as an associative table between the Spy and BadGuy tables.

Well what about the rest of the application? Because as I stated earlier, this book is about SQL Server 2000 not analyzing requirements, I will give you the rest of the ERD without boring you too much about the details. However, I will endeavor to explain the ERD, as shown in Figure 3.15, so you have an overview of the application.

Figure 3.15
Final ERD of the SQLSpyNet database.

As you can see, the ERD for the whole application is relatively simple. It contains only a few tables. We have defined the spies and the bad guys to be subtypes of the person entity.

As discussed earlier (in Chapter 1), this allows us the greatest flexibility for adding to our application. If we need an employee entity, creating an employee subtype is a simple process of creating an employee subtype.

The important factor to note is how the bad guys and the spies relate to each other. A spy is only given an assignment if a bad guy is wrecking havoc on the world, so this is their relationship.

In a lifetime, a spy can be involved in foiling one or more bad guy's plans, and a bad guy can be involved in one or more dastardly deeds. So we have a many-to-many relationship.

Because this cannot be implemented in a relational database, we need an associative entity. So we introduce the Activity entity. This keeps track of the bad guys that a spy has fought, as well as the plans that they have foiled.

The ActivityType entity allows us to enter a list of evil plans that the bad guys hatch. These plans could include (and most probably will) taking over the world! They could wreck havoc and generally cause a disturbance among the citizens.

The Address entity is only used to track the addresses of a person. Because it is possible for a person to have more than one address, we have broken it out into another table, thus reducing repeating rows in our person table and enabling our data model (ERD) to conform to 1NF.

1NF is First Normal Form, which is the first level of normalization (see the Excursion "1NF, 2NF, 3NF, 4!"). Basically, a table must not contain a list of values in any single column (that is, a comma-delimited list of values), and there must be no repeating groups.

1NF, 2NF, 3NF, 4!

Normalization is derived from the study of relational theory and was developed by E.F. Codd, the father of relational theory. It is the process of arranging tables and data within our database to reduce dependencies and inefficient structures as much as possible.

Normalization is linear in nature (each rule is applied after another rule is complete) and is used to define the best database structure that you can possibly achieve.

Six normal forms, or stages, are defined in the process of normalization. As mentioned, the rules of normalization are linear; therefore, the second rule depends on the first rule, the third rule depends on the second, and so on.

3NF is the level that most developers take their database applications to when normalizing their databases. Why? Most developers find that that third normal is sufficient to meet the requirements of the application and ensure data consistency.

Note - By removing repeating rows from our Person table, we have ensured that our table now conforms to First Normal Form (1NF).

As I referred to earlier, defining a data model is not the same for everybody, and having said this, this model will not suit some people. They might look at the model and decide that it has limitations, which it does. So I will now describe to you the limitations that are in place in the model.

  • A spy can work on only one assignment at a time. This means that we cannot schedule a spy to be fighting two or more bad guys (probably a good thing!). Likewise, a bad guy cannot be executing more than one plan at one time.

  • A person can have many addresses, but an address can belong to only one person. This means that communal sharing of housing is not recognized. To implement this we need to introduce either another entity (the correct way) or allowing repeating rows of information in the address table (naughty, naughty, naughty).

  • We can—and this is relatively serious, if we do not develop our business rules correctly—end up with the scenario of a person belonging to the spies group as well as the bad-guys group! To prevent this we implement domain constraints. These will prevent the incorrect values from being entered into either table.

Domain constraints enables us to limit the values that a column in a table will accept. We can specify a pool of allowable values for a given column. For example, if a value falls between the range of 1–10, then it can be entered, otherwise it will be rejected. As you may can imagine, this is a very powerful tool of database development.

That about wraps it up for the ERD for our application except, of course, for the actual column names within our tables, but we will create these as we develop the tables.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020