Based on this, a list is simply an arrangement of information and this information, also called data, is stored in tables.
The information of a table is organized in categories called columns and horizontal arrangements called records or rows. A column holds a category of data that is common to all records. A table must have at least one column. This means that you cannot create a table without defining at least one column. To complete the creation of a table, you must save it. If you are freshly creating a table and decide to save it, you would be prompted to name it. The name of a table:
Besides these rules, you can make up yours. To avoid confusion, here are the rules we will use to name our tables:
To assist you with creating a table, you use a Data Definition Language (DDL) command that is CREATE TABLE, followed by a name. Therefore, to create a table, you start with the following statement: CREATE TABLE TableName; The CREATE TABLE expression is required. The TableName factor specifies the name of the new table. The TableName can use the rules and suggestions we reviewed for the tables. After specifying the name of the table, you must create at least one category, called a column of data.
After creating a table, it becomes part of its database and you can use that table over and over again. In some cases, you may want to create a table to test something and you would not need to use that table the next time you connect to the server. Such a table is referred to as a temporary table. To create a temporary table, start its name with #, followed by the desired name. Once the table has been created, it would be available as long as you are using the same connection to the server. If you close Microsoft SQL Server, the table would be automatically deleted.
To assist you with creating a table, Microsoft SQL Server can generate sample code for you. You can then simply modify or customize it. First display or open an empty query window. To display the Templates Explorer, on the main menu, you can click View -> Templates Explorer. In the Templates Explorer, expand the Table node. Under table, drag Create Table and drop it in the query window. Sample code would be generated for you.
Table maintenance consists of reviewing or changing its aspects. This includes reviewing the list of tables of a database, renaming a table, or deleting it.
Like every other object of a database or of the computer, a table possesses some characteristics that are proper to it. To view these characteristics, in the Object Explorer, right-click the table and click Properties.
Most operations require that you open a table before using it. There are various ways a table displays, depending on how you want to examine it:
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
To see the list of tables of a database in the Object Explorer, you can click the Tables node:
To see the list of tables of a database using SQL, in a Query window, specify the database (using a USE statement), and execute sp_help (it is a stored procedure). Here is an example:
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 N'StaffMembers', N'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.
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.
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:
|
|
|||||||||
|
|