03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creat<strong>in</strong>g Your Own Tables x 441<br />

This SQL statement simply deletes the row with an ID of 13. If the row exists, it gets deleted. If the<br />

row does not exist, no error is raised, but the dialog box <strong>in</strong> SSMS shows you that zero rows have been<br />

affected. The parentheses are not required <strong>in</strong> this example, but they help <strong>in</strong> determ<strong>in</strong><strong>in</strong>g precedence<br />

when you have multiple conditions <strong>in</strong> your WHERE clause.<br />

Up to this po<strong>in</strong>t, you have seen how to work with exist<strong>in</strong>g tables <strong>in</strong> a database. However, it’s also<br />

important to underst<strong>and</strong> how to create new tables with relationships yourself. This is discussed <strong>in</strong><br />

the next section.<br />

CREATING YOUR OWN TABLES<br />

Creat<strong>in</strong>g tables <strong>in</strong> a SQL Server database is easy us<strong>in</strong>g the database tools that are part of SSMS. You<br />

see how you can create your own tables <strong>in</strong> the database after the next section, which briefly <strong>in</strong>troduces<br />

you to the data types at your disposal <strong>in</strong> SQL Server.<br />

Data Types <strong>in</strong> SQL Server<br />

Just as with programm<strong>in</strong>g languages like Visual Basic .<strong>NET</strong> <strong>and</strong> C#, a SQL Server database uses<br />

different data types to store its data. SQL Server 2012 supports more than 30 different data types,<br />

most of which look similar to the types used <strong>in</strong> .<strong>NET</strong>. The follow<strong>in</strong>g table lists the most common<br />

SQL Server data types together with a description <strong>and</strong> their .<strong>NET</strong> counterparts.<br />

SQL 2012 DATA TYPE DESCRIPTION .<strong>NET</strong> DATA TYPE<br />

bit Stores boolean values <strong>in</strong> a 0 / 1 format (1 = True, 0 =<br />

False).<br />

System.Boolean<br />

char / nchar<br />

Conta<strong>in</strong>s fixed-length text. When you store text<br />

shorter than the def<strong>in</strong>ed length, the text is padded<br />

with spaces. The nchar stores the data <strong>in</strong> Unicode<br />

format, which enables you to store data for many<br />

foreign languages (at the cost of need<strong>in</strong>g twice as<br />

much space <strong>in</strong> the database).<br />

System.Str<strong>in</strong>g<br />

datetime Stores a date <strong>and</strong> a time <strong>in</strong> the range 1753/1/1<br />

through 9999/12/31.<br />

System.DateTime<br />

datetime2<br />

Similar to the datetime type, but with a greater precision<br />

<strong>and</strong> range (from 0001/1/1 through 9999/12/31)<br />

System.DateTime<br />

date Stores a date without the time element. System.DateTime<br />

time Stores a time without the date element. System.TimeSpan<br />

decimal Enables you to store large, fractional numbers. System.Decimal<br />

float Enables you to store large, fractional numbers. System.Double<br />

cont<strong>in</strong>ues

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!