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 TSQL 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 TSQL 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 pretty straight forward 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.
The post Database Fundamentals #7: Create a Table Using T-SQL appeared first on Grant Fritchey.