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

Table Syntax in SQL Server 2017

DZone's Guide to

Table Syntax in SQL Server 2017

Look at the syntax for different MS SQL commands for SQL Server 2017, and get an example of how exactly each command works.

· Database Zone
Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

Data or information for a database is stored in tables. Tables are uniquely identified by their names and are comprised of columns and rows. A table has a specified number of columns but can have any number of rows. Columns contain the column name and data type for the column. Rows contain the records or data for the columns.

Let's look at the syntax for different MS SQL commands, as well as an example of how exactly each command works.

Create Table

Syntax:

CREATE TABLE <<Table_Name>>
(
  column1 datatype,
  column2 datatype,
  column3 datatype
); 

Example: To create a table Customers:   

CREATE TABLE Customers(
  CustomerName nvarchar(250),
  Countryy nvarchar(50),
  City nvarchar(50)
); 


Alter Table

Syntax to add column:

ALTER TABLE <<Table_Name>> ADD <<Column_Name>> <<datatype>>;

Example:

ALTER TABLE Customers ADD Age Char(1); 

Syntax to alter/modify column data type:

ALTER TABLE <<Table_Name>> ALTER <<Column_Name>> <<datatype>>; 

Example:

ALTER TABLE Customers ALTER COLUMN Age int; 

Syntax to delete/drop column:

ALTER TABLE <<Table_Name>> DROP COLUMN <<Column_Name>>; 

Example:

ALTER TABLE Customers DROP COLUMN Age; 

Syntax to rename column name:

EXEC sp_rename'<<Table_Name>>.<<Column_Name>>', <<NewColumn_Name>>, 'COLUMN'; 

Example:

EXEC sp_rename'Customers.Countryy', Country, 'COLUMN'; 

Syntax to rename table name:

EXEC sp_rename <<Table_Name>, <<New_Table_Name>>; 

Example:

EXEC sp_rename Customers, CustomersInfo;


Drop Table

Syntax:

DROP TABLE <<Table_Name>>;  

Example:

DROP TABLE CustomersInfo; 


Lastly, here's a video to help you make sense of it all!

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

Topics:
sql server 2017 ,tables ,database ,tutorial ,ms sql

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}