The easiest type of data that can be imported into SQL Server, and which is available on almost all database environments, is the text file. Almost every database environment allows you to import a text file but data from that file must be formatted appropriately. For example, the information stored in the file must define the columns as distinguishable by a character that serves as a separator. This separator can be the single-quote, the double-quote, or any valid character. Data between the quotes is considered as belonging to a distinct field. Besides this information, the database would need to separate information from two different columns. Again, a valid character must be used. Most databases, including Microsoft SQL Server, recognize the comma as such a character. The last piece of information the file must provide is to distinguish each record from another. This is easily taken car of by the end of line of a record. This is also recognized as the carriage return. These directives can help you manually create a text file that can be imported into Microsoft SQL Server. In practicality, if you want to import data that resides on another database, you can ask that application to create the source of data. Most applications can do that and format the data. That is the case for the data we will use in the next exercise: it is data that resided on a Microsoft Access database and was prepared to be imported in Microsoft SQL Server. After importing data, you should verify and possibly format it to customize its fields.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Record maintenance includes viewing records, looking for one or more records, modifying one or more records, or deleting one or more records. These operations can be performed visually or programmatically using a Data Definition Language (DDL) command.
Before visually performing some operations on a table, you must first select one or more records. In the Table window, to select one record, position the mouse on the left button of the record and click:
To select a range of records, click the gray button of one of the records, press and hold Shift, then click the gray button of the record at the other extreme. To select the records in a random fashion, select one record, press and hold Ctrl, then click the gray button of each desired record:
To select all records of a table, click the gray button on the left of the first column:
To visually modify one or more records on a table, first open it (you right-click the table in the Object Explorer and click Open Table) to view its records. Locate the record and the field you want to work on and perform the desired operation. Updating a record consists of changing its value for a particular column. To visually update a record, in the Object Explorer, right-click its table (or view, in some cases) and click Edit Top 200 Rows. This would open the table as a spreadsheet, as seen above. Locate the value under the desired column header, and modify the value as you see fit. To programmatically update a record, you use a Data Definition Language (DDL) command. If you are working in Microsoft SQL Server:
The DDL command to update a record is UPDATE. The basic formula to use is: UPDATE TableName SET ColumnName = Expression With this formula, you must specify the name of the involved table as the TableName factor of our formula. The SET statement allows you to specify a new value, Expression, for the field under the ColumnName column. Consider the following code to create a new database named VideoCollection and to add a table named Videos to it: CREATE DATABASE VideoCollection; GO USE VideoCollection; GO CREATE TABLE Videos ( VideoID INT NOT NULL IDENTITY(1,1), VideoTitle nvarchar(120) NOT NULL, Director nvarchar(100) NULL, YearReleased SMALLINT, VideoLength nvarchar(30) NULL, Rating nchar(6) ); GO INSERT INTO Videos(VideoTitle, Director, YearReleased, VideoLength) VALUES(N'A Few Good Men','Rob Reiner',1992,'138 Minutes'); INSERT INTO Videos(VideoTitle, Director, VideoLength) VALUES(N'The Lady Killers', N'Joel Coen & Ethan Coen', N'104 Minutes'); INSERT INTO Videos(VideoTitle, Director, YearReleased, VideoLength) VALUES(N'The Silence of the Lambs','Jonathan Demme',1991,'118 Minutes'); INSERT INTO Videos(VideoTitle, Director, VideoLength) VALUES(N'The Distinguished Gentleman', N'James Groeling', N'112 Minutes'); INSERT INTO Videos(VideoTitle, Director, VideoLength) VALUES(N'Ghosts of Mississippi', N'Rob Reiner', N'130 Minutes'); GO Imagine you want to indicate that all these videos are rated R. To do this, in our formula, specify the table name. In the SET expression, specify the column name as Rating and assign it R as a string. This would be done as follows: USE VideoCollection; GO UPDATE Videos SET Rating = N'R'; GO If you use the UPDATE statement like this, it acts on all records. The above code would produce:
Editing a record consists of changing a value in a field. It could be that the field is empty, such as the © Year of the the 'The Lady Killers' video of the following table. It could be that the value is wrong, such as the Director of the the 'The Distinguished Gentleman' video of this table:
To edit a record, first open the table to view its records. Locate the record, the column on which you want to work, and locate the value you want to change, then change it. To edit a record, you must provide a way for the interpreter to locate the record. To do this, you would associate the WHERE operator in an UPDATE statement using the following formula: UPDATE TableName SET ColumnName = Expression WHERE Condition(s) The WHERE operator allows you to specify how the particular record involved would be identified. It is very important, in most cases, that the criterion used be able to uniquely identify the record. In the above table, imagine that you ask the interpreter to change the released year to 1996 where the director of the video is Rob Reiner. The UPDATE statement would be written as follows: UPDATE Videos SET YearReleased = 1996 WHERE Director = N'Rob Reiner'; In the above table, there are at least two videos directed by Rob Reiner. When this statement is executed, all video records whose director is Rob Reiner would be changed, which would compromise existing records that didn't need this change. This is where the identity column becomes valuable. We saw earlier that, when using it with the IDENTITY feature, the interpreter appends a unique value to each record. You can then use that value to identify a particular record because you are certain the value is unique. Here is an example used to specify the missing copyright year of a particular record: UPDATE Videos SET YearReleased = 1996 WHERE VideoID = 5; GO Here is an example used to change the name of the director of a particular video: UPDATE Videos SET Director = N'Jonathan Lynn' WHERE VideoTitle = N'The Distinguished Gentleman';
After making changes to a table using SQL, you don't get a visual display of what happened. With Transact-SQL, you can temporarily display the result of this operation or you can store it in a table. We already saw how to do this when creating records. You follow the same formula when updating records. The formula is: UPDATE TableName SET ColumnName = Expression OUTPUT INSERTED.Columns VALUES(Value_1, Value_2, Value_X) Besides the formula we have used so far, after the SET expression, start with an OUTPUT INSERTED expression, followed by a period. If you are to show all columns of the table, add the * operator. Otherwise, type INSERTED followed by a period, followed by the name of the column you want to show.
If you think all records of a particular table are, or have become, useless, you can clear the whole table, which would still keep its structure. To delete all records from a table, first select all of them, and press Delete. You would receive a warning:
If you still want to delete the records, click Yes. If you change your mind, click No. The DDL command to clear a table of all records is DELETE. It uses the following formula: DELETE TableName; When this statement is executed, all records from the TableName factor would be removed from the table. Be careful when doing this because once the records have been deleted, you cannot get them back.
If you find out that a record is not necessary, not anymore, or is misplaced, you can remove it from a table. To remove a record from a table, you can right-click its gray box and click Delete. You can also first select the record and press Delete. You would receive a warning to confirm your intention. To programmatically delete a record:
In SQL, to delete a record, use the DELETE FROM statement associate the WHERE operator. The formula to follow is: DELETE FROM TableName WHERE Condition(s) The TableName factor is used to identify a table whose record(s) would be removed. The Condition(s) factor allows you to identify a record or a group of records that carries a criterion. Once again, make sure you are precise in your criteria so you would not delete the wrong record(s). Here is an example used to remove a particular record from the table: DELETE FROM Videos WHERE VideoTitle = N'The Lady Killers'; Here is an example used to clear the table of all videos: DELETE FROM Videos; When some record(s) has(have) been deleted, the operation is performed behind the scenes and you don't see the result. If you want to see a list of the records that were deleted, you can use the OUTPUT operator to display the result. To show the list of the records from a table that was completely emptied, you can use the following formula: DELETE FROM TableName OUTPUT DELETED.Columns The OUTPUT INSERTED expression follows the description we have seen for the record update. Here is an example: USE VideoCollection6; GO DELETE FROM Videos OUTPUT deleted.* GO To show the list of the records that were deleted based on a condition, use the following formula: DELETE FROM TableName OUTPUT DELETED.Columns WHERE Condition(s) Here is an example: USE VideoCollection6; GO DELETE FROM Videos OUTPUT deleted.* WHERE YearReleased IS NULL; GO
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||
| Previous | Copyright © 2009 FunctionX, Inc. | Next |
|
|
||