Top Banner
SQL server Section 2&3
22

SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Jan 18, 2018

Download

Documents

Allan Cook

Defining tables:  Ensure that SQL Server Management Studio is running.  Create Database [ApressFinancial].  Expand the Object Explorer so that you can see the ApressFinancial database.  Expand the ApressFinancial database so that you can see the Tables node, as shown in Figure.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

SQL server Section 2&3

Page 2: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Presentation Topics• What are Data Types• Character Data Types• Number Data Types• Date and Time Data Types• CAST and CONVERT functions• TRY_PARSE and TRY_CONVERT functions.• Other Data Types

Page 3: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Defining tables:

Ensure that SQL Server Management Studio is running. Create Database [ApressFinancial]. Expand the Object Explorer so that you can see the ApressFinancial database. Expand the ApressFinancial database so that you can see the Tables node, as shown

in Figure.

Defining tables

Page 4: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Right-click the Tables node and select New Table. This will take you into the Table Designer. Figure shows how the Table Designer looks when you first enter it.

From this screen, you need to enter the details for each column within the table. Enter the first column, CustomerId, in the Column Name column. When naming columns, try to avoid using spaces. Either keep the column names without spaces, like I have done with CustomerId, or use an underscore (_) instead of a space.

At the moment, notice that Column Properties in the middle of Figure is empty. This will fill up when you start entering a data type after entering the column name. The Column Properties section is just as crucial as the top half of the screen where you enter the column name and data type.

The drop-down combo box that lists the data types is one of the first areas provided by SQL Server to help us with table creation.

Page 5: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

SQL Server Data Types• SQL Server associates columns, expressions,

variables, and parameters with data types• Data types determine what kind of data can be held:

Integers, characters, dates, money, decimals, etc.

Page 6: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Character Data Types

Data Type Range StorageCHAR(n), NCHAR(n)

1-8000 characters n bytes2*n bytes

VARCHAR(n), NVARCHAR(n)

1-8000 characters n+2 bytes(2*n) +2 bytes

VARCHAR(MAX),NVARCHAR(MAX)

1-2^31-1 characters

Actual length + 2

Page 7: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Character Data Types

char

nchar

varchar

nvarchar

1 byte per char

Fixed Variable

Regular

Unicode 2 bytes per char

Page 8: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Character Data Types

Page 9: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Integer Data Types

Data type Range Storage (bytes)tinyint 0 to 255 1

smallint -32,768 to 32,768 2int 2^31 (-2,147,483,648) to

2^31-1 (2,147,483,647)4

Bigint -2^63 - 2^63-1 (+/- 9 quintillion)

8

bit 1, 0 or NULL 1

Page 10: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Other Numeric Data TypesData type Range Storage (bytes)

decimal/numeric 10^38 +1 through 10^38 – 1 when maximum precision is used

5-17

money -922,337,203,685,477.5808 to 922,337,203,685,477.5807

8

smallmoney - 214,748.3648 to 214,748.3647 4

float /real Approximate data type for floating point numeric data

4-8

Page 11: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Date and Time Data TypesData Type Storage

(bytes)Date Range Accuracy

DATETIME 8 January 1, 1753 to December 31, 9999

3-1/3 milliseconds

SMALLDATETIME

4 January 1, 1900 to June 6, 2079

1 minute

DATETIME2 6 to 8 January 1, 0001 to December 31, 9999

100 nanoseconds

DATE 3 January 1, 0001 to December 31, 9999

1 day

TIME 3 to 5 100 nanoseconds

DATETIMEOFFSET

8 to 10 January 1, 0001 to December 31, 9999

100 nanoseconds

Page 12: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Date and Time Data Types• Older versions of SQL Server supported only

DATETIME and SMALLDATETIME• DATE, TIME, DATETIME2, and DATETIMEOFFSET

introduced in SQL Server 2008• SQL Server doesn't offer an option for entering

a date or time value explicitly– Dates and times are entered as character literals

and converted explicitly or implicitly• For example, CHAR converted to DATETIME due to

precedence

Page 13: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

When building table definitions, there can be columns defined as NULL and columns that have NOT NULLs, or, if using the Table Designer, you can check or uncheck the Allow Nulls option. These two different statements define whether data must be entered into the column or not. A NULL value means that there is absolutely nothing entered in that column—no data at all. A column with a NULL value is a special data state, with special meaning. This really means that the type of data within the column is unknown.

Null Values

Page 14: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

By defining a column using the IDENTITY option, you are informing SQL Server that The column will have a value generated by SQL Server. There will be a start point (seed). An increment value is given, informing SQL Server by how much each new ID should

increase. SQL server will manage the allocation of IDs.

From the Column Properties section for our column expand the Identity Specification node, as we need to set the Is Identity property to Yes. This will set the Identity Increment to 1 and the Identity Seed to 1 as well,

as shown in Figure

Identity Values

Page 15: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

As a row is added to a table, rather than enforcing developers to add values to columns that could be populated by SQL Server, such as a column that details using a date and time when a row of data was added, it is possible to place a default value there instead. The default value can be any valid value for that data type.

From column properties insert your value in (Default value or binding) .

Default Values

Page 16: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Once in the Table Designer, select the CustomerId column. This will be the column we are setting the primary key for. Right-click to bring up the pop-up menu shown in Figure

Setting a Primary Key

Page 17: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

Setting a Foreign Key To create the FOREIGN KEY constraint, right-click the upper pane in Table

Designer and then click Relationships. When the Foreign Key Relationships dialog box appears, click Add to create the new constraint. Figure shows the dialog box with the initial foreign key, before any properties have been configured.

Page 18: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

At a minimum, we must modify the Tables And Columns Specification property group, which identifies the referenced and referencing columns in the foreign key. Select the property group listing and then click its associated browse button. When the Tables and Columns dialog box appears, select the Product table as the primary key table and the ProductID column from that table. For the SpecialtyProducts table, which is the foreign key table, select the ProductID column. In addition, modify the name of the foreign key, if desired. I went with fk_ProductID. The Tables and Columns dialog box should now look similar to the one shown in Figure

Page 19: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

You can create a UNIQUE constraint in the Indexes/Keys dialog box by , right-click the upper pane in Table Designer and then click indexes/keys then clicking the Add button.

Unique Key

Page 20: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

To add a check constraint, right-click the upper pane in Table Designer and then click Check Constraints. When the Check Constraints dialog box appears, click the Add button to create the new constraint. Figure 14 shows the dialog box with the initial constraint, before any properties have been configured.

Check constraint

Page 21: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

To create the CHECK constraint, you must configure the Expression property. You can either enter an expression directly or click the browse button to launch the Check Constraint Expression dialog box, where you have more room to work on your expression, as shown in Figure

Page 22: SQL server Section 23. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.

(Assignment)Create the following database and insert data in each table