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

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

Microsoft SQL Server Online TrainingData 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!


Online SQL Server Training

MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.

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 }}