- SQL Server Reference Guide
- Introduction
- SQL Server Reference Guide Overview
- Table of Contents
- Microsoft SQL Server Defined
- SQL Server Editions
- SQL Server Access
- Informit Articles and Sample Chapters
- Online Resources
- Microsoft SQL Server Features
- SQL Server Books Online
- Clustering Services
- Data Transformation Services (DTS) Overview
- Replication Services
- Database Mirroring
- Natural Language Processing (NLP)
- Analysis Services
- Microsot SQL Server Reporting Services
- XML Overview
- Notification Services for the DBA
- Full-Text Search
- SQL Server 2005 - Service Broker
- Using SQL Server as a Web Service
- SQL Server Encryption Options Overview
- SQL Server 2008 Overview
- SQL Server 2008 R2 Overview
- SQL Azure
- The Utility Control Point and Data Application Component, Part 1
- The Utility Control Point and Data Application Component, Part 2
- Microsoft SQL Server Administration
- The DBA Survival Guide: The 10 Minute SQL Server Overview
- Preparing (or Tuning) a Windows System for SQL Server, Part 1
- Preparing (or Tuning) a Windows System for SQL Server, Part 2
- Installing SQL Server
- Upgrading SQL Server
- SQL Server 2000 Management Tools
- SQL Server 2005 Management Tools
- SQL Server 2008 Management Tools
- SQL Azure Tools
- Automating Tasks with SQL Server Agent
- Run Operating System Commands in SQL Agent using PowerShell
- Automating Tasks Without SQL Server Agent
- Storage – SQL Server I/O
- Service Packs, Hotfixes and Cumulative Upgrades
- Tracking SQL Server Information with Error and Event Logs
- Change Management
- SQL Server Metadata, Part One
- SQL Server Meta-Data, Part Two
- Monitoring - SQL Server 2005 Dynamic Views and Functions
- Monitoring - Performance Monitor
- Unattended Performance Monitoring for SQL Server
- Monitoring - User-Defined Performance Counters
- Monitoring: SQL Server Activity Monitor
- SQL Server Instances
- DBCC Commands
- SQL Server and Mail
- Database Maintenance Checklist
- The Maintenance Wizard: SQL Server 2000 and Earlier
- The Maintenance Wizard: SQL Server 2005 (SP2) and Later
- The Web Assistant Wizard
- Creating Web Pages from SQL Server
- SQL Server Security
- Securing the SQL Server Platform, Part 1
- Securing the SQL Server Platform, Part 2
- SQL Server Security: Users and other Principals
- SQL Server Security – Roles
- SQL Server Security: Objects (Securables)
- Security: Using the Command Line
- SQL Server Security - Encrypting Connections
- SQL Server Security: Encrypting Data
- SQL Server Security Audit
- High Availability - SQL Server Clustering
- SQL Server Configuration, Part 1
- SQL Server Configuration, Part 2
- Database Configuration Options
- 32- vs 64-bit Computing for SQL Server
- SQL Server and Memory
- Performance Tuning: Introduction to Indexes
- Statistical Indexes
- Backup and Recovery
- Backup and Recovery Examples, Part One
- Backup and Recovery Examples, Part Two: Transferring Databases to Another System (Even Without Backups)
- SQL Profiler - Reverse Engineering An Application
- SQL Trace
- SQL Server Alerts
- Files and Filegroups
- Partitioning
- Full-Text Indexes
- Read-Only Data
- SQL Server Locks
- Monitoring Locking and Deadlocking
- Controlling Locks in SQL Server
- SQL Server Policy-Based Management, Part One
- SQL Server Policy-Based Management, Part Two
- SQL Server Policy-Based Management, Part Three
- Microsoft SQL Server Programming
- An Outline for Development
- Database
- Database Services
- Database Objects: Databases
- Database Objects: Tables
- Database Objects: Table Relationships
- Database Objects: Keys
- Database Objects: Constraints
- Database Objects: Data Types
- Database Objects: Views
- Database Objects: Stored Procedures
- Database Objects: Indexes
- Database Objects: User Defined Functions
- Database Objects: Triggers
- Database Design: Requirements, Entities, and Attributes
- Business Process Model Notation (BPMN) and the Data Professional
- Business Questions for Database Design, Part One
- Business Questions for Database Design, Part Two
- Database Design: Finalizing Requirements and Defining Relationships
- Database Design: Creating an Entity Relationship Diagram
- Database Design: The Logical ERD
- Database Design: Adjusting The Model
- Database Design: Normalizing the Model
- Creating The Physical Model
- Database Design: Changing Attributes to Columns
- Database Design: Creating The Physical Database
- Database Design Example: Curriculum Vitae
- NULLs
- The SQL Server Sample Databases
- The SQL Server Sample Databases: pubs
- The SQL Server Sample Databases: NorthWind
- The SQL Server Sample Databases: AdventureWorks
- The SQL Server Sample Databases: Adventureworks Derivatives
- UniversalDB: The Demo and Testing Database, Part 1
- UniversalDB: The Demo and Testing Database, Part 2
- UniversalDB: The Demo and Testing Database, Part 3
- UniversalDB: The Demo and Testing Database, Part 4
- Getting Started with Transact-SQL
- Transact-SQL: Data Definition Language (DDL) Basics
- Transact-SQL: Limiting Results
- Transact-SQL: More Operators
- Transact-SQL: Ordering and Aggregating Data
- Transact-SQL: Subqueries
- Transact-SQL: Joins
- Transact-SQL: Complex Joins - Building a View with Multiple JOINs
- Transact-SQL: Inserts, Updates, and Deletes
- An Introduction to the CLR in SQL Server 2005
- Design Elements Part 1: Programming Flow Overview, Code Format and Commenting your Code
- Design Elements Part 2: Controlling SQL's Scope
- Design Elements Part 3: Error Handling
- Design Elements Part 4: Variables
- Design Elements Part 5: Where Does The Code Live?
- Design Elements Part 6: Math Operators and Functions
- Design Elements Part 7: Statistical Functions
- Design Elements Part 8: Summarization Statistical Algorithms
- Design Elements Part 9:Representing Data with Statistical Algorithms
- Design Elements Part 10: Interpreting the Data—Regression
- Design Elements Part 11: String Manipulation
- Design Elements Part 12: Loops
- Design Elements Part 13: Recursion
- Design Elements Part 14: Arrays
- Design Elements Part 15: Event-Driven Programming Vs. Scheduled Processes
- Design Elements Part 16: Event-Driven Programming
- Design Elements Part 17: Program Flow
- Forming Queries Part 1: Design
- Forming Queries Part 2: Query Basics
- Forming Queries Part 3: Query Optimization
- Forming Queries Part 4: SET Options
- Forming Queries Part 5: Table Optimization Hints
- Using SQL Server Templates
- Transact-SQL Unit Testing
- Index Tuning Wizard
- Unicode and SQL Server
- SQL Server Development Tools
- The SQL Server Transact-SQL Debugger
- The Transact-SQL Debugger, Part 2
- Basic Troubleshooting for Transact-SQL Code
- An Introduction to Spatial Data in SQL Server 2008
- Performance Tuning
- Performance Tuning SQL Server: Tools and Processes
- Performance Tuning SQL Server: Tools Overview
- Creating a Performance Tuning Audit - Defining Components
- Creating a Performance Tuning Audit - Evaluation Part One
- Creating a Performance Tuning Audit - Evaluation Part Two
- Creating a Performance Tuning Audit - Interpretation
- Creating a Performance Tuning Audit - Developing an Action Plan
- Understanding SQL Server Query Plans
- Performance Tuning: Implementing Indexes
- Performance Monitoring Tools: Windows 2008 (and Higher) Server Utilities, Part 1
- Performance Monitoring Tools: Windows 2008 (and Higher) Server Utilities, Part 2
- Performance Monitoring Tools: Windows System Monitor
- Performance Monitoring Tools: Logging with System Monitor
- Performance Monitoring Tools: User Defined Counters
- General Transact-SQL (T-SQL) Performance Tuning, Part 1
- General Transact-SQL (T-SQL) Performance Tuning, Part 2
- General Transact-SQL (T-SQL) Performance Tuning, Part 3
- Performance Monitoring Tools: An Introduction to SQL Profiler
- Performance Tuning: Introduction to Indexes
- Performance Monitoring Tools: SQL Server 2000 Index Tuning Wizard
- Performance Monitoring Tools: SQL Server 2005 Database Tuning Advisor
- Performance Monitoring Tools: SQL Server Management Studio Reports
- Performance Monitoring Tools: SQL Server 2008 Activity Monitor
- The SQL Server 2008 Management Data Warehouse and Data Collector
- Performance Monitoring Tools: Evaluating Wait States with PowerShell and Excel
- Practical Applications
- Choosing the Back End
- The DBA's Toolbox, Part 1
- The DBA's Toolbox, Part 2
- Scripting Solutions for SQL Server
- Building a SQL Server Lab
- Using Graphics Files with SQL Server
- Enterprise Resource Planning
- Customer Relationship Management (CRM)
- Building a Reporting Data Server
- Building a Database Documenter, Part 1
- Building a Database Documenter, Part 2
- Data Management Objects
- Data Management Objects: The Server Object
- Data Management Objects: Server Object Methods
- Data Management Objects: Collections and the Database Object
- Data Management Objects: Database Information
- Data Management Objects: Database Control
- Data Management Objects: Database Maintenance
- Data Management Objects: Logging the Process
- Data Management Objects: Running SQL Statements
- Data Management Objects: Multiple Row Returns
- Data Management Objects: Other Database Objects
- Data Management Objects: Security
- Data Management Objects: Scripting
- Powershell and SQL Server - Overview
- PowerShell and SQL Server - Objects and Providers
- Powershell and SQL Server - A Script Framework
- Powershell and SQL Server - Logging the Process
- Powershell and SQL Server - Reading a Control File
- Powershell and SQL Server - SQL Server Access
- Powershell and SQL Server - Web Pages from a SQL Query
- Powershell and SQL Server - Scrubbing the Event Logs
- SQL Server 2008 PowerShell Provider
- SQL Server I/O: Importing and Exporting Data
- SQL Server I/O: XML in Database Terms
- SQL Server I/O: Creating XML Output
- SQL Server I/O: Reading XML Documents
- SQL Server I/O: Using XML Control Mechanisms
- SQL Server I/O: Creating Hierarchies
- SQL Server I/O: Using HTTP with SQL Server XML
- SQL Server I/O: Using HTTP with SQL Server XML Templates
- SQL Server I/O: Remote Queries
- SQL Server I/O: Working with Text Files
- Using Microsoft SQL Server on Handheld Devices
- Front-Ends 101: Microsoft Access
- Comparing Two SQL Server Databases
- English Query - Part 1
- English Query - Part 2
- English Query - Part 3
- English Query - Part 4
- English Query - Part 5
- RSS Feeds from SQL Server
- Using SQL Server Agent to Monitor Backups
- Reporting Services - Creating a Maintenance Report
- SQL Server Chargeback Strategies, Part 1
- SQL Server Chargeback Strategies, Part 2
- SQL Server Replication Example
- Creating a Master Agent and Alert Server
- The SQL Server Central Management System: Definition
- The SQL Server Central Management System: Base Tables
- The SQL Server Central Management System: Execution of Server Information (Part 1)
- The SQL Server Central Management System: Execution of Server Information (Part 2)
- The SQL Server Central Management System: Collecting Performance Metrics
- The SQL Server Central Management System: Centralizing Agent Jobs, Events and Scripts
- The SQL Server Central Management System: Reporting the Data and Project Summary
- Time Tracking for SQL Server Operations
- Migrating Departmental Data Stores to SQL Server
- Migrating Departmental Data Stores to SQL Server: Model the System
- Migrating Departmental Data Stores to SQL Server: Model the System, Continued
- Migrating Departmental Data Stores to SQL Server: Decide on the Destination
- Migrating Departmental Data Stores to SQL Server: Design the ETL
- Migrating Departmental Data Stores to SQL Server: Design the ETL, Continued
- Migrating Departmental Data Stores to SQL Server: Attach the Front End, Test, and Monitor
- Tracking SQL Server Timed Events, Part 1
- Tracking SQL Server Timed Events, Part 2
- Patterns and Practices for the Data Professional
- Managing Vendor Databases
- Consolidation Options
- Connecting to a SQL Azure Database from Microsoft Access
- SharePoint 2007 and SQL Server, Part One
- SharePoint 2007 and SQL Server, Part Two
- SharePoint 2007 and SQL Server, Part Three
- Querying Multiple Data Sources from a Single Location (Distributed Queries)
- Importing and Exporting Data for SQL Azure
- Working on Distributed Teams
- Professional Development
- Becoming a DBA
- Certification
- DBA Levels
- Becoming a Data Professional
- SQL Server Professional Development Plan, Part 1
- SQL Server Professional Development Plan, Part 2
- SQL Server Professional Development Plan, Part 3
- Evaluating Technical Options
- System Sizing
- Creating a Disaster Recovery Plan
- Anatomy of a Disaster (Response Plan)
- Database Troubleshooting
- Conducting an Effective Code Review
- Developing an Exit Strategy
- Data Retention Strategy
- Keeping Your DBA/Developer Job in Troubled Times
- The SQL Server Runbook
- Creating and Maintaining a SQL Server Configuration History, Part 1
- Creating and Maintaining a SQL Server Configuration History, Part 2
- Creating an Application Profile, Part 1
- Creating an Application Profile, Part 2
- How to Attend a Technical Conference
- Tips for Maximizing Your IT Budget This Year
- The Importance of Blue-Sky Planning
- Application Architecture Assessments
- Transact-SQL Code Reviews, Part One
- Transact-SQL Code Reviews, Part Two
- Cloud Computing (Distributed Computing) Paradigms
- NoSQL for the SQL Server Professional, Part One
- NoSQL for the SQL Server Professional, Part Two
- Object-Role Modeling (ORM) for the Database Professional
- Business Intelligence
- BI Explained
- Developing a Data Dictionary
- BI Security
- Gathering BI Requirements
- Source System Extracts and Transforms
- ETL Mechanisms
- Business Intelligence Landscapes
- Business Intelligence Layouts and the Build or Buy Decision
- A Single Version of the Truth
- The Operational Data Store (ODS)
- Data Marts – Combining and Transforming Data
- Designing Data Elements
- The Enterprise Data Warehouse — Aggregations and the Star Schema
- On-Line Analytical Processing (OLAP)
- Data Mining
- Key Performance Indicators
- BI Presentation - Client Tools
- BI Presentation - Portals
- Implementing ETL - Introduction to SQL Server 2005 Integration Services
- Building a Business Intelligence Solution, Part 1
- Building a Business Intelligence Solution, Part 2
- Building a Business Intelligence Solution, Part 3
- Tips and Troubleshooting
- SQL Server and Microsoft Excel Integration
- Tips for the SQL Server Tools: SQL Server 2000
- Tips for the SQL Server Tools – SQL Server 2005
- Transaction Log Troubles
- SQL Server Connection Problems
- Orphaned Database Users
- Additional Resources
- Tools and Downloads
- Utilities (Free)
- Tool Review (Free): DBDesignerFork
- Aqua Data Studio
- Microsoft SQL Server Best Practices Analyzer
- Utilities (Cost)
- Quest Software's TOAD for SQL Server
- Quest Software's Spotlight on SQL Server
- SQL Server on Microsoft's Virtual PC
- Red Gate SQL Bundle
- Microsoft's Visio for Database Folks
- Quest Capacity Manager
- SQL Server Help
- Visual Studio Team Edition for Database Professionals
- Microsoft Assessment and Planning Solution Accelerator
- Aggregating Server Data from the MAPS Tool
We're continuing our study of programming SQL Server databases with the next object after databases: tables. Tables are the absolute heart of Relational Database Management Systems, and a solid understanding here will help you immensely as we go along.
I'll go from the general to the specific in this article but there's a lot to this topic. Don't worry; just keep this introduction handy, since we'll see table structure all throughout the development cycle. I've also included some really good links on this topic, plus several books in the Safari online service from InformIT. Ready? Here we go!
Tables Described
The concept of tables is really quite simple. Think of a series of rows and columns, like a spreadsheet. The columns represent the grouping of the data, and the rows represent the unique sets of data. The "cell," or the single intersection of a row and column, contains a single datum.
The relational database concept (which shares many concepts with set theory from algebra) has a few specific words for all this. Not that many people use them any more, but you might as well be familiar with them:
Table |
Entity |
Column |
Attribute |
Row |
Tuple, or Record |
Cell |
Item, value, intersection |
I'll use the more common names on the left throughout this tutorial.
So what might a table look like? Well, from the pubs sample database in SQL Server, here is a small set that I've selected:
au_id |
au_lname |
au_fname |
phone |
address |
city |
state |
zip |
contract |
172-32-1176 |
White |
Johnson |
408 496-7223 |
10932 Bigge Rd. |
Menlo Park |
CA |
94025 |
1 |
213-46-8915 |
Green |
Marjorie |
415 986-7020 |
309 63rd St. #411 |
Oakland |
CA |
94618 |
1 |
238-95-7766 |
Carson |
Cheryl |
415 548-7723 |
589 Darwin Ln. |
Berkeley |
CA |
94705 |
1 |
267-41-2394 |
O'Leary |
Michael |
408 286-2428 |
22 Cleveland Av. #14 |
San Jose |
CA |
95128 |
1 |
274-80-9391 |
Straight |
Dean |
415 834-2919 |
5420 College Av. |
Oakland |
CA |
94609 |
1 |
This sample is from the "authors" table. You can see that each row makes a record, and obviously contains information about individual authors. Here's another sample set:
title_id |
title |
type |
pub_id |
price |
advance |
royalty |
ytd_sales |
notes |
pubdate |
PS3333 |
Prolonged Data Deprivation: Four Case Studies |
psychology |
0736 |
19.99 |
2000 |
10 |
4072 |
What happens when the data runs dry? Searching evaluations of information-shortage effects. |
6/12/1991 |
PS7777 |
Emotional Security: A New Algorithm |
psychology |
0736 |
7.99 |
4000 |
10 |
3336 |
Protecting yourself and your loved ones from undue emotional stress in the modern world. Use of computer and nutritional aids emphasized. |
6/12/1991 |
TC3218 |
Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean |
trad_cook |
0877 |
20.95 |
7000 |
10 |
375 |
Profusely illustrated in color, this makes a wonderful gift book for a cuisine-oriented friend. |
10/21/1991 |
TC4203 |
Fifty Years in Buckingham Palace Kitchens |
trad_cook |
0877 |
11.95 |
4000 |
14 |
15096 |
More anecdotes from the Queen's favorite cook describing life among English royalty. Recipes, techniques, tender vignettes. |
6/12/1991 |
TC7777 |
Sushi, Anyone? |
trad_cook |
0877 |
14.99 |
8000 |
10 |
4095 |
Detailed instructions on how to make authentic Japanese sushi in your spare time. |
6/12/1991 |
These are titles of books written, and this table has rows and columns representing information about books.
Now that we've covered the individual tables, we move to the reason that they make up the heart of the Relational Database Management System.
Relating Tables
Individual tables normally store the most absolute atomic level of data. By "atomic," I mean that only the data that belongs to the entity is stored in it. Information about the author should only be about the author not his or her books. We'll come back to that in a bit, during the database design phase. (As a matter of fact, you'll come back to this over and over in your career, and you'll have some fairly heated arguments with your colleagues about what goes in a particular table!) This process is called table normalization, and it has several levels. For now, know that the entire basis of Relational Database Systems is based on this concept of keeping the table data atomic.
Back to the subject at hand. The individual tables are a nice, tight structure in which to store data, but they don't seem to have anything in common. As a matter of fact, from the data shown, you can't figure out which book was written by which author.
To solve this problem, we can add a column in one of the tables that points to the other, or relates them. That's done quite often in SQL, and is perfectly acceptable as long as once piece of data only relates to another one time (called a one to one relationship).
For instance, we could add a column in the second table that has the author's last name in it, showing who wrote that book. That may work, but two authors might have the same last name. Plus, storing the name (or almost any data) in two different tables can often lead to trouble, since you have to remember that if the information changes in one table, you have to change it in all the others.
Another problem arises in this situation if more than one author writes a single book. This is called a one-to-many relationship. I've worked on several projects like that both as an author and as a SQL-guy! Would you add one column for the other author, two (in the two-author case), three, more?
You can solve this problem with a table that has a column for the author link, and another column for the title of the books. The link for the author would repeat in several records, but the book titles would be different.
You can see that the problem would also carry through the other way round. That is, if we tried to add a column for the books in the author's table, how many should we add? Again, this problem can be solved with a link to the book titles written, with the author appearing only once.
But often both situations occur at the same time. More than one author can write more than one book. This situation is called a many-to-many relationship. How do we resolve that dilemma? With one more table.
Here's what that table looks like in the pubs database:
au_id |
Title_id |
au_ord |
royaltyper |
172-32-1176 |
PS3333 |
1 |
100 |
213-46-8915 |
BU1032 |
2 |
40 |
213-46-8915 |
BU2075 |
1 |
100 |
238-95-7766 |
PC1035 |
1 |
100 |
267-41-2394 |
BU1111 |
2 |
40 |
You can probably guess how this helps. By combining the three tables, we pull the au_id from the first table, the title_id from the second and connect those using the third table. I've highlighted the values in question and placed the third table in the middle:
Authors
au_id |
au_lname |
au_fname |
phone |
address |
city |
state |
zip |
contract |
172-32-1176 |
White |
Johnson |
408 496-7223 |
10932 Bigge Rd. |
Menlo Park |
CA |
94025 |
1 |
TitleAuthors
au_id |
Title_id |
au_ord |
royaltyper |
172-32-1176 |
PS3333 |
1 |
100 |
Titles
title_id |
Title |
type |
pub_id |
price |
advance |
royalty |
ytd_sales |
notes |
pubdate |
PS3333 |
Prolonged Data Deprivation: Four Case Studies |
psychology |
0736 |
19.99 |
2000 |
10 |
4072 |
What happens when the data runs dry? Searching evaluations of information-shortage effects. |
6/12/1991 |
The middle table shown above provides the "link" between the other two tables. Actually, the middle table can be used to join many tables. This joining of tables is analogous to the concept of the union in algebra set theory.
To be sure, there's more to know about these relationships, but I've hit you with enough new info for now. We'll revisit this topic in future articles on development, called Logical Database Design.
For now, let's look at how SQL Server physically implements tables, and then how to create them.
How SQL Server 2000 Implements Tables
SQL Server 2000 pretty much complies with the ANSI-92 SQL Standards. Those standards are the base-level requirements for creating, destroying and managing tables. The ANSI standard sets the minimums, and then each vendor then extends that minimum to sell their product.
Here are some quick facts about SQL Server 2000 tables:
Tables have owners
Tables can be stored on various SQL Filenames
You can store 8060 bytes per row of data in a table
You can have 1024 columns per table
You can have 2,147,483,647 objects in a database, and the number of tables is part of this maximum
You can store 1,048,516 TB of data in a SQL Server database, so the total storage of all tables must be kept under this number
Creating Tables
There are several ways to create tables, from using graphical tools, to typing Transact-SQL (T-SQL) commands to programming. I'll show you how to use the graphical tools and then the T-SQL method. We'll discuss programming in another article.
The first method I teach for creating tables is with a great tool, the Table Designer. You can see in the graphic below that I've opened Enterprise Manager and drilled down to the Test database object.
I then right-clicked the Tables object and selected New Table from the menu that appeared. I continue the process on this panel:
You can see here that I'm setting the name of the columns in my table and giving them a data type (I'll cover data types in greater depth in another article) and length.
The part I wanted to point out here is the Allow Nulls selection. I'll explain Nulls more completely when we begin coding, but a Null condition means basically "I don't know yet." It doesn't mean zero, it doesn't mean nothing, it means "I don't know yet." As a matter of fact, there is still a bit of argument going around on whether you should even be allowed to have a Null value in a database.
While I won't take sides on this issue (some people need to get out more!) I will say that you should carefully consider whether you should allow a value to be Null. Here's a basic rule of thumb: If you're going to compare this value to something else, then you should think long and hard about allowing a Null value. Think of it like this:
I don't know how much money I have in my pocket. You walk up and say, "How much money do you have?" I say, "I don't know." You then ask, "Do you have more than Carl? He's got ten dollars." I reply "I don't know; I might." You ask, "Do you have exactly what Christine has? She has five dollars." I reply "I don't know. I might have that much, or none. I just don't know."
You see, you can never be sure what I have, so you can't trust any answer I give. For that reason, you shouldn't compare values to a Null value. In listing the number of people who have five dollars in their pocket, you really can't count me in or out of that set.
Even though I've said all this, I'll make quite a few of my columns to allow a Null that really shouldn't be, so we can see the problems that it creates later.
OK, I've beat that concept to death! Let's move on.
The next panel has all the columns filled out, and you can see that I've also created a value to hold the key for this table, which is the unique value assigned to each row. It's easy to do just highlight the column and click the key icon. I've set the Identity attribute to be true on this Integer column, which means that SQL Server will generate a new number for me each time I enter a new record.
I haven't covered all of the options here, since we'll use this tool again when we create our programming databases in future articles. I just wanted to expose the panels to you here.
Clicking the icon that looks like a scroll brings up the following panel:
Here I'm setting the table name, owner, and so forth. The next tab on this panel is the Relationships tab, shown here:
On this tab I can create relationships with other tables, just as we discussed earlier. This allows me to make sure that a "child" value from another table checks to make sure that a "parent" value exists in this one. I can also make this the "child" table, or have this table be a child to one table and a parent to another.
The next tab is shown here:
I explained indexes in another set of articles, and here's how you can create them graphically. We'll see more of this tab later. The next tab sets the check constraints:
A check constraint is another way you can make sure users and programs only put data in a field when it meets certain conditions. These constraints, along with others, are called Declarative Referential Integrity. That means that no matter how the code is prepared, you prevent anyone or thing from entering data you don't want in the database. If you forgo this type of constraint, it will be up to the developer to make sure the data is clean. That's called Programmatic Referential Integrity, and is less safe, for obvious reasons.
OK, that's the high level graphical method for creating a table. You can also create tables with T-SQL Commands. The syntax is quite extensive, and is described completely in Books Online, but here's the code to create the same table I just made:
CREATE TABLE UserNames ( UserCode int IDENTITY (1, 1) NOT NULL, FirstName varchar (50) NULL, LastName varchar (50) NOT NULL, Address varchar (50) NULL, City varchar (50) NULL, State char (2) NOT NULL, PostalCode varchar (15) NULL ) ON [PRIMARY]
The relevant parts here are the Null and Not Null qualifiers, which I'm sure you can decipher. Also note the IDENTITY qualifier, which creates that auto-incrementing (1 to start, then increment by 1 each time) value.
What you don't see here is the Primary Key constraint I set with the key icon earlier. I'll demonstrate adding that constraint in the next article.
There's an optional qualifier here: the ON [PRIMARY] keywords. This important command sets where the table lives. If you just have one file for all objects (as 90% of most databases do) then you don't need it. If you do make other files for the database (which I've described elsewhere on this site), remember that they aren't used unless you put this statement on your indexes or table creation statements. You want to place tables that are used most often on these other files to speed access to them.
Whew!
Well, once again, we've scratched the surface of one of the database objects. We've got several more to cover, and as mentioned, we'll be revisiting the table object quite often in our programming. See you soon!