Sql server® 2012 t-sql fundamentals


   Microsoft SQL Server 2012 T-SQL Fundamentals Creating Tables



Download 10,93 Mb.
Pdf ko'rish
bet61/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   57   58   59   60   61   62   63   64   ...   443
Bog'liq
BookSQL

20  

Microsoft SQL Server 2012 T-SQL Fundamentals



Creating Tables

The following code creates a table named Employees in the dbo schema in the TSQL2012 database.

USE TSQL2012; 

 

IF OBJECT_ID('dbo.Employees', 'U') IS NOT NULL 



  DROP TABLE dbo.Employees; 

 

CREATE TABLE dbo.Employees 



  empid     INT         NOT NULL

  firstname VARCHAR(30) NOT NULL, 

  lastname  VARCHAR(30) NOT NULL, 

  hiredate  DATE        NOT NULL, 

  mgrid     INT         NULL, 

  ssn       VARCHAR(20) NOT NULL, 

  salary    MONEY       NOT NULL 

);

The USE statement sets the current database context to that of TSQL2012. It is important to incor-



porate the USE statement in scripts that create objects to ensure that SQL Server creates the objects 

in the specified database. In an on-premises SQL Server implementation, the USE statement can actu-

ally change the database context from one to another. In SQL Database, you cannot switch between 

different databases, but the USE statement will not fail as long as you are already connected to the 

target database. So even in SQL Database, I recommend having the USE statement to ensure that you 

are connected to the right database when creating your objects.

The IF statement invokes the OBJECT_ID function to check whether the Employees table already 

exists in the current database. The OBJECT_ID function accepts an object name and type as inputs. 

The type ‘U’ represents a user table. This function returns the internal object ID if an object with the 

specified input name and type exists, and NULL otherwise. If the function returns a NULL, you know 

that the object doesn’t exist. In our case, the code drops the table if it already exists, and then creates 

a new one. Of course, you could have chosen a different treatment, such as simply not creating the 

object if it already exists.

The CREATE TABLE statement is in charge of defining what I referred to earlier as the header of the 

relation. Here you specify the name of the table and, in parentheses, the definition of its attributes 

(columns).

Notice the use of the two-part name dbo.Employees for the table name, as recommended earlier. 

If you omit the schema name, SQL Server will assume the default schema associated with the data-

base user running the code. 

For each attribute, you specify the attribute name, data type, and whether the value can be NULL 

(this is called nullability). 

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   57   58   59   60   61   62   63   64   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish