How can a new table be created in SQL Server?
To create a new table in SQL Server, you can use the CREATE TABLE statement. Here is the basic syntax for creating a table:
The syntax to create a new table with multiple columns and their respective data types and constraints in a database is:
“`sql
CREATE TABLE table_name
(
column1 datatype constraint,
column2 datatype constraint,
…
columnN datatype constraint
);
“`
In this case, table_name refers to the name of the data table to be created, column1, column2, … columnN are the column names in the data table, datatype represents the data type of the column, and constraint refers to the constraints on the columns (such as primary key, unique constraint, etc.).
Here is an example, where we create a data table named “employees” with columns for “id”, “name”, and “age”.
Create a table for employees with columns for ID, name (required and up to 50 characters), and age (must be 18 years or older).
In the example above, the id column is the primary key, the name column is of type varchar(50) and cannot be empty, and the age column is of integer type and must be greater than or equal to 18 with a check constraint.