When working with SQL Server, there are multiple ways to create a table. One of the most common methods is using the graphical user interface (GUI) provided by SQL Server Management Studio. However, if you prefer to work with scripts or need to automate the table creation process, you can use Transact-SQL (T-SQL) statements.
Let’s take a look at an example of how to create a table using T-SQL:
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];
The above script will create a table named “Person” in the “dbo” schema. The table has four columns: “PersonID”, “FirstName”, “LastName”, and “DateOfBirth”. The “PersonID” column is defined as an identity column, which means it will automatically generate a unique value for each new row. The “FirstName” and “LastName” columns are of type varchar(50) and cannot store NULL values. The “DateOfBirth” column is of type date and can store NULL values.
If you ever need to delete the table, you can use the following T-SQL command:
DROP TABLE dbo.Person;
As you can see, creating a table using T-SQL is straightforward and follows a logical syntax. Each column is defined with its name, data type, and any additional constraints. You can also specify the file group on which the table will be stored.
While creating tables is relatively simple, manipulating data within tables can be more complex. This topic will be covered in future blog posts in this series.
Stay tuned for the next post, where we will explore the various data types available within SQL Server.
Thank you for reading!