Microsoft SharePoint Team Blog
The official blog of the Microsoft SharePoint Product Group
Renaming a Table
If you find out that the name of a table is not appropriate, you can change it. To change the name of a table in the SQL Server Management Studio, in the Object Explorer, right-click the table and click Rename. Type the desired name and press Enter.
To change the name of a table with code, execute sp_rename, followed by the current name of the table, a comma, and the new desired name of the table. The formula to use is:
sp_rename ExistingTableName, TableNewName;
The names of tables should be included in single-quotes. Here is an example:
sp_rename 'StaffMembers', 'Employees';
GO
In this case, the interpreter would look for a table named StaffMembers in the current or selected database. If it finds it, it would rename it Employees. If the table doesn't exist, you would receive an error.
Deleting a Table
If you have an undesired table in a database, you can remove it. To delete a table in the SQL Server Management Studio, in the Object Explorer, right-click the table under its database node and click Delete. You will receive a warning giving you a chance to confirm your intentions. If you still want to remove the table, click OK.
To delete a table using SQL, use the following formula:
DROP TABLE TableName
The DROP TABLE expression is required and it is followed by the name of the undesired table. When you execute the statement, you will not receive a warning before the table is deleted.
You can also use sample code that Microsoft SQL Server can generate for you. First display an empty query window. Also display the Templates Explorer and expand the Table node. Under Table, drag Drop Table and drop it in the empty query window. Sample code would be generated for you. You can then simply modify it and execute the statement.
Referring to a Table
In future lessons, we will write various expressions that involve the names of tables. In those expressions, you will need to specify a particular table you want to use. There are three main ways you can do this. To refer to, or to indicate, a table:
You can simply type its name. An example would be Students
You can type dbo, followed by the period operator, followed by the name of the table. An example would be dbo.Students
You can type the name of the database to which the table belongs, followed by the period operator, followed by dbo, followed by the period operator, and followed by the name of the table. An example would be RedOakHighSchool.dbo.Students
The Columns of a Table
Introduction
In our introduction to tables, we saw that a list could be organized in categories called columns. Here is the example we saw:
Name Age Gender Relationship
Judie 18 Female Sister
Ernest 24 Male Cousin
Bill 52 Unknown Uncle
David 36 Male Brother
Hermine 12 Unknown Niece
As you can see from this arrangement, a column is used to particularly classify one type of data. For example, one column can be used to list some names. Another column can be used to list numbers. Yet another column can be used for a select list of items that keep repeating those items.
To organize the information that a column holds, a table needs a series of details about each column. Two aspects are particularly important: a name and the type of data that a column should/must/can hold.
The Name of a Column
To be able to recognize the categories of information that a column holds, the column should have a name. In Microsoft SQL Server, the name of a column displays in the top, the header part, of the column. The name of a column allows the database as a file to identify the column. The name of a column also will help you, the database developer, to identify that column. There are rules and suggestions you must or should follow when naming the columns of a table.
The name of a column:
Can start with a letter, a digit, or an underscore
Can include letters, digits, and spaces in any combination
After respecting these rules, you can add your own rules. In our lessons, here are the rules we will use to name our columns:
A name will start with a letter. Examples are n, act, or Second
After the first character as an underscore or a letter, the name will have combinations of underscores, letters, and digits. Examples are n24 or col_52_t
Unless specified otherwise, a name will not include special characters such as !, @, #, $, %, ^, &, or *
If the name is a combination of words, each word will start in uppercase. Examples are Date Hired, LastName, Drivers License Number, or EmailAddress
Practical Learning: Setting Columns Names
Under the Column Name column, double-click name to highlight it
Type FirstName to replace it and press Enter
The Types of Data
After deciding on the name of a column, the database needs to know what kind of information the column would hold. Since there are various kinds of information a database can deal with, we saw in Lesson 3 the types of data that Microsoft SQL Server supported. Therefore, you must specify the data type that is necessary for a particular column.
Practical Learning: Setting Data Types
Click the arrow of the combo box under the Data Type column
Scroll down and select varchar from the list
Click the first empty field under FirstName and type MI
Press the down arrow key to position the cursor under MI
Type LastName and press the down arrow key
Type DateHired
Press Tab and type d
Notice that the datetime data type is selected.
Press Enter three times to position the mouse cursor under DateHired
Type EmployeeNumber and press the down arrow key
Complete the table as follows
source : http://blogs.msdn.com/sharepoint/default.aspx
Minggu, 15 Februari 2009
Langganan:
Postingan (Atom)