Creating a Table
Số trang: 8
Loại file: pdf
Dung lượng: 57.72 KB
Lượt xem: 15
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Creating a Table You can use Enterprise Manager to add a table to a database. In this section, youll add a table to the Northwind database to store the details of a person
Nội dung trích xuất từ tài liệu:
Creating a TableCreating a TableYou can use Enterprise Manager to add a table to a database. In this section, youll add atable to the Northwind database to store the details of a person. This table will be calledPersons, and will contain the columns shown in Table 2.8. Table 2.8: DEFINITION FOR THE COLUMNS OF THE Persons TABLECOLUMN NAME DATABASE TYPE LENGTH ALLOWS NULL VALUES?PersonID int 4 NoFirstName nvarchar 15 NoLastName nvarchar 15 NoDateOfBirth datetime 8 YesAddress nvarchar 50 YesEmployerID nchar 5 NoTo create a table in the Northwind database, you select the Tables node of the Northwinddatabase in Enterprise Manager and select Action ➣ New Table. Youll then see the tabledesigner. Add the columns as shown in Table 2.8 to the table, as shown in Figure 2.20.Figure 2.20: Adding a new tableNote The length of some of the data types is fixed. For example, the int type always uses 4 bytes of storage space, so you cant change the length of an int column from 4. Similarly, the datetime type always uses 8 bytes of storage space. You can change the length of nchar and nvarchar columns because those types are designed to store variable-length data.Click the Save button on the toolbar to save the table. In the Choose Name dialog, enterPersons as the name, and click OK to save your table, as shown in Figure 2.21.Figure 2.21: Entering the name of the tableNote Once youve saved your table, you can return to the table designer at any time by selecting the table in the Tables node of Enterprise Manager, right-clicking the table, and selecting Design Table.In the rest of this chapter, youll learn how to: • Get additional information about the columns in a table using the Columns tab. • Set the primary key of a table. • Set the permissions that allow access to the contents of a table. • Create a relationship between tables. • Create an index to allow faster access to the information in a table. • Create a constraint to restrict values that may be stored in a column.The Columns TabIn the area beneath the grid, youll notice a tab named Columns. The Columns tabcontains additional information about the currently selected column in the grid, andFigure 2.20, shown earlier, shows the information on the PersonID column. As youchange your selected column, the information in the Columns tab will change.You can enter an optional description for a column in the Description field of theColumns tab. The Default Value field allows you to supply an initial value when a newrow is added to the table; you can of course supply your own value to a column that willoverride the default value.The Precision field shows the maximum number of digits that may be used to store anumber, including those that might be stored to the right of a decimal point. The Scalefield shows the maximum number of digits to the right of a decimal point. For example,the precision and scale of an int column are 10 and 0, meaning that an int column canstore up to 10 digits, with no digits to the right of a decimal point-no digits to the rightbecause an int is an integral number. The precision and scale for a money column are 19and 4, meaning that a money column can store up to 19 digits, with up to four of thosedigits to the right of a decimal point.The Identity field allows you specify whether SQL Server should automatically assign avalue to a field. If you set the Identity field to Yes, then you can also specify values forthe Identity Seed and Identity Increment fields. You use the Identity Seed field to set theinitial value for the column, and you use the Identity Increment field to specify theincrement for value. For example, if you set the Identity Seed to 1 and the IdentityIncrement to 1, then the first value for the column would be 1, the next would be 2, andso on. The ProductID column of the Products table is an example of a column that usesan identity to set its value.The IsRowGuid field specifies whether a uniqueidentifier column is a globally uniqueidentifier known as a GUID.Tip SQL Server doesnt automatically supply a value for a GUID. If you want SQL Server to generate a GUID, you can use the SQL Server NEWID() function. The NEWID() function always returns a different value. You can then use the output from this function as the Default Value for your uniqueidentifier column. For example, you would set the Default Value field to [NEWID()]. Youll learn more about SQL Server functions in the next chapter.The Formula field allows you to set a formula that is used to assign a value to a column.The Collation field specifies the rules that are used to sort and compare characters. Youmight need to set this when working with fo ...
Nội dung trích xuất từ tài liệu:
Creating a TableCreating a TableYou can use Enterprise Manager to add a table to a database. In this section, youll add atable to the Northwind database to store the details of a person. This table will be calledPersons, and will contain the columns shown in Table 2.8. Table 2.8: DEFINITION FOR THE COLUMNS OF THE Persons TABLECOLUMN NAME DATABASE TYPE LENGTH ALLOWS NULL VALUES?PersonID int 4 NoFirstName nvarchar 15 NoLastName nvarchar 15 NoDateOfBirth datetime 8 YesAddress nvarchar 50 YesEmployerID nchar 5 NoTo create a table in the Northwind database, you select the Tables node of the Northwinddatabase in Enterprise Manager and select Action ➣ New Table. Youll then see the tabledesigner. Add the columns as shown in Table 2.8 to the table, as shown in Figure 2.20.Figure 2.20: Adding a new tableNote The length of some of the data types is fixed. For example, the int type always uses 4 bytes of storage space, so you cant change the length of an int column from 4. Similarly, the datetime type always uses 8 bytes of storage space. You can change the length of nchar and nvarchar columns because those types are designed to store variable-length data.Click the Save button on the toolbar to save the table. In the Choose Name dialog, enterPersons as the name, and click OK to save your table, as shown in Figure 2.21.Figure 2.21: Entering the name of the tableNote Once youve saved your table, you can return to the table designer at any time by selecting the table in the Tables node of Enterprise Manager, right-clicking the table, and selecting Design Table.In the rest of this chapter, youll learn how to: • Get additional information about the columns in a table using the Columns tab. • Set the primary key of a table. • Set the permissions that allow access to the contents of a table. • Create a relationship between tables. • Create an index to allow faster access to the information in a table. • Create a constraint to restrict values that may be stored in a column.The Columns TabIn the area beneath the grid, youll notice a tab named Columns. The Columns tabcontains additional information about the currently selected column in the grid, andFigure 2.20, shown earlier, shows the information on the PersonID column. As youchange your selected column, the information in the Columns tab will change.You can enter an optional description for a column in the Description field of theColumns tab. The Default Value field allows you to supply an initial value when a newrow is added to the table; you can of course supply your own value to a column that willoverride the default value.The Precision field shows the maximum number of digits that may be used to store anumber, including those that might be stored to the right of a decimal point. The Scalefield shows the maximum number of digits to the right of a decimal point. For example,the precision and scale of an int column are 10 and 0, meaning that an int column canstore up to 10 digits, with no digits to the right of a decimal point-no digits to the rightbecause an int is an integral number. The precision and scale for a money column are 19and 4, meaning that a money column can store up to 19 digits, with up to four of thosedigits to the right of a decimal point.The Identity field allows you specify whether SQL Server should automatically assign avalue to a field. If you set the Identity field to Yes, then you can also specify values forthe Identity Seed and Identity Increment fields. You use the Identity Seed field to set theinitial value for the column, and you use the Identity Increment field to specify theincrement for value. For example, if you set the Identity Seed to 1 and the IdentityIncrement to 1, then the first value for the column would be 1, the next would be 2, andso on. The ProductID column of the Products table is an example of a column that usesan identity to set its value.The IsRowGuid field specifies whether a uniqueidentifier column is a globally uniqueidentifier known as a GUID.Tip SQL Server doesnt automatically supply a value for a GUID. If you want SQL Server to generate a GUID, you can use the SQL Server NEWID() function. The NEWID() function always returns a different value. You can then use the output from this function as the Default Value for your uniqueidentifier column. For example, you would set the Default Value field to [NEWID()]. Youll learn more about SQL Server functions in the next chapter.The Formula field allows you to set a formula that is used to assign a value to a column.The Collation field specifies the rules that are used to sort and compare characters. Youmight need to set this when working with fo ...
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Creating a TableGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 311 0 0 -
74 trang 294 0 0
-
96 trang 290 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 288 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 272 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 262 0 0