Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

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
) ON [PRIMARY];

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.

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

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

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}