Database Fundamentals #7: Create a Table Using T-SQL

DZone 's Guide to

Database Fundamentals #7: Create a Table Using T-SQL

Knowing how to create tables in SQL Server without a GUI is a bit of knowledge worth having. Read on to find out how you can perform this task.

· Database Zone ·
Free Resource

The syntax for creating a table logically follows many of the same steps that you did when using the GUI, but it will all be done with the statements. This script will exactly replicate everything that you did with the GUI:

CREATE TABLE dbo.Person (
PersonID int IDENTITY(1,1) NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
DateOfBirth date NULL

Breaking the script into separate lines, it’s easy to see how the T-SQL commands perform the actions defined in the GUI (it also makes it easier to read). The CREATE TABLE statement in this context is self-explanatory. After that, you’re defining the schema and the table name. Within the parenthesis, you define each of the columns. First is the name of the column followed by the data type and, for most columns, the ability of the column to store NULL values. The table will be stored on the PRIMARY file group.

The first column, PersonID, is defined as an identity value through the IDENTITY keyword. It also includes the increment and seed values within the parenthesis, just like from the GUI.

If you wanted to delete this table through T-SQL, the command is:

DROP TABLE dbo.Person;

That’s really all there is to it. The T-SQL language is very easy to use and is pretty straightforward when it comes to data definition language (DDL). It gets very complex when we start to talk about data manipulation language (DML). That’ll be saved for more blog posts later in the series.

The next post will drill down on the various data types available within SQL Server.

database ,t-sql ,tutorial ,create table ,sql server ,gui

Published at DZone with permission of Grant Fritchey , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}