Once a table contains information, you can review it using either SQL Server Management Studio or an external application.
The tables of a database display in the Object Explorer under their database node. To open a table for data entry, right-click it and click Edit Top 200 Rows.
Data Navigation consists of displaying and viewing data. Because information of a database is stored in tables, your primary means of viewing data consists of opening a table in a view that displays its information.
When a table displays its records, you navigate through its fields using the mouse or the keyboard. With the mouse, to get to any cell, you can just click it. To navigate through records using the keyboard, you can press:
As far as users are concerned, the primary reason for using a database is to open a table and use its records. You on the other hand need to control who has access to a table and what a particular user can do on it. Fortunately, Microsoft SQL Server can let you control all types of access to the records of any table of your database. As seen for databases, you can grant or deny access to a table, to some users individually or to a group of users. Of course, you can work visually or programmatically.
Before exercising security on a table for a user, you must have created a user account for that user.
To visually grant or deny operations at the table level, in the Object Explorer, right-click the table and click Properties. In the Select a Page list, click Permissions. In the Users or Roles list, click the name of the user or click Select to locate the user. In the Permissions column, locate the type of permission you want. Manage the operations in the Grant and in the Deny columns.
In Microsoft SQL Server, every operation of a table has its own permissions. The list of permissions can be seen in the lower section of the Table properties if you click Permissions:
More than on a database, the permissions of a table are very interconnected. This means that giving one type of access may not be enough to achieve the right result. This also means that you must know how to combine permissions:
The basic formula to programmatically grant one or more permissions to a user is:
GRANT Permission1,Permission2, Permission_n ON [ OBJECT :: ][ schema_name ].object_name [ (Column1, Column2, Column_n ] ) ] TO Login1, Login2, Login_n ]
The basic formula to programmatically deny (a) permission(s) is:
DENY Permission1,Permission2, Permission_n ON [ OBJECT :: ][ schema_name ].object_name [ (Column1, Column2, Column_n ] ) ] TO Login1, Login2, Login_n ]
You start with the GRANT (or DENY) keyword. To grant a permission, type it. After specifying the types of permissions you want, type ON or ON OBJECT::. This is followed by the name of the object, like a table, on which you want to grant permissions. If necessary, or this is optional, precede the name of the object with the name of the schema. After the name of the object, type TO, followed by the login name that will receive the permission. Here is an example:
USE master; GO CREATE DATABASE Exercise1; GO USE Exercise1; GO CREATE TABLE Employees ( EmployeeNumber nchar(10), FirstName nvarchar(20), LastName nvarchar(20), ); GO CREATE USER [Peter Mukoko] FOR LOGIN rkouma; GO GRANT ALTER ON OBJECT::Employees TO [Peter Mukoko]; GO
If you want to combine permissions, separate them with commas. Here is an example:
USE Exercise1 GRANT INSERT, UPDATE ON OBJECT::dbo.Payroll TO [James Galvin]; GO
If you want to grant the permission(s) to more than one account, separate them with commas.
As mentioned for a database, you can give one account the ability to grant or deny permissions to other accounts. To do this visually, access the Database Properties for the database. In the Users or Roles section, select the user. In the Persmissions section, use the check boxes in the With Grant column.
The formula to programmatically give an account the ability to grant or deny permissions to other accounts is:
GRANT Permission1,Permission2, Permission_n TO Login1, Login2, Login_n WITH GRANT OPTION
In this formula, you add the WITH GRANT OPTION expression.
As you are probably aware already, columns are used to organize data by categories. Each column has a series of fields under the column header. One of the actual purposes of a table is to display data that is available for each field under a particular column. Data entry consists of providing the necessary values of the fields of a table. Data is entered into a field and every time this is done, the database creates a row of data. This row is called a record. This means that entering data also self-creates a row.
For a user to be able to create records on a table, his or her account must be granted the INSERT permission. Of course, the user must be allowed to open the table.
Probably the easiest and fastest way to enter data into a table is by using SQL Server Management Studio. Of course, you must first open the desired table from an available database. In the Object Explorer, after expanding the Databases and the Tables nodes, open a table for data entry. If the table does not contain data, it would appear with one empty row:
If some records were entered already, their rows would show and the table would provide an empty row at the end, expecting a new record.
To perform data entry on a table, you can click in a field. Each column has a title, called a caption, on top. This gray section on top is called a column header. In Microsoft SQL Server, it displays the actual name of the column. You refer to the column header to know what kind of data should/must go in a field under a particular column. This is why you should design your columns meticulously. After identifying a column, you can type a value. Except for text-based columns, a field can accept or reject a value if the value does not conform to the data type that was set for the column. This means that in some circumstances, you may have to provide some or more explicit information to the user.
To programmatically perform data entry, you use a Data Definition Language (DDL) command known as INSERT. To start, if you are working in Microsoft SQL Server:
INSERT TableName VALUES(Column1, Column2, Column_n);
Alternatively, or to be more precise, you can use the INTO keyword between the INSERT keyword and the TableName factor to specify that you are entering data into the table. This is done with the following syntax:
INSERT INTO TableName VALUES(Column1, Column2, Column_n)
The TableName factor must be a valid name of an existing table in the database you are using. If the name is wrong, the SQL interpreter would simply consider that the table you are referring to doesn't exist. Consequently, you would receive an error.
The VALUES keyword indicates that you are ready to list the values of the columns. The values of the columns must be included in parentheses.
If the column is a BIT data type, you must specify one of its values as 0 or 1.
If the column is a numeric type, you should pay attention to the number you type. If the column was configured to receive an integer (int, bigint, smallint), you should provide a valid natural number without the decimal separator.
If the column is for a decimal number (float, real, decimal, numeric), you can type the value with its character separator (the period for US English).
If the column was created for a date data type, make sure you provide a valid date.
If the data type of a column is a string type, you should include its entry between single quotes. For example, a shelf number can be specified as 'HHR-604' and a middle initial can be given as 'D'.
The most common technique of performing data entry requires that you know the sequence of fields of the table in which you want to enter data. With this subsequent list in mind, enter the value of each field in its correct position.
During data entry on adjacent fields, if you don't have a value for a numeric field, you should type 0 as its value. For a string field whose data you don't have and cannot provide, type two single-quotes '' to specify an empty field.
To perform data entry in an order of your choice, you must provide your list of the fields of the table. You can either use all columns or provide a list of the same columns but in your own order. In the same way, you don't have to provide data for all fields, just those you want, in the order you want.
In previous sections, we added a single record to each use of the INSERT formula. You can add various records with one call to INSERT. If you are adding a value to each column of the table, after the name of the table, type VALUES, open and close the first parentheses. Inside the parentheses, include the desired values. To add another record, type a comma after the closing parenthesis, open a new parenthesis, list the new values, and close the parenthesis. Do this as many times as you need to add records. Here is an example:
CREATE DATABASE VideoCollection GO USE VideoCollection GO CREATE TABLE Videos ( Title nvarchar(50), Director nvarchar(50), Rating nchar(10), YearReleased int ) GO INSERT INTO Videos VALUES(N'Her Alibi', N'Bruce Beresford', N'PG-13', 1998), (N'Memoirs of a Geisha', N'Rob Marshall', N'PG-13', 2006), (N'Two for the Money', N'D.J. Caruso', N'R', 2008); GO
This is valid for adjacent data entry. If you want to follow your own order of columns, on the right side of the name of the table, include a list of columns in parentheses. Then, when giving the values, for each record, follow the order in which you listed the columns. Here is an example:
INSERT INTO Videos(Rating, Title, Director) VALUES(N'R', N'Wall Street', N'Oliver Stone'), (N'', N'Michael Jackson Live in Bucharest', N'Andy Morahan'), (N'PG-13', N'Sneakers', N'Paul Alden Robinson'), (N'R', N'Soldier', N'Paul Anderson'); GO
Imagine you have a series of records and you want to add them to a table. Transact-SQL allows you to specify whether to insert all of the records, a certain number of records, or a portion of the records.
Based on a number of records, to insert a fraction of the records, after the INSERT keyword, type TOP (Number) followed by the name of the table and the rest of the formula we have used so far. Here is an example:
USE VideoCollection GO INSERT TOP (2) INTO Videos(Rating, Title, Director) VALUES(N'PG-13', N'Big Momma''s House ', N'Raja Gosnell'), (N'G', N'Annie', N'John Huston'), (N'PG', N'Incredibles (The)', N'Brad Bird'), (N'PG-13', N'Mission: Impossible', N'Brian De Palma'), (N'R', N'Negotiator (The)', N'F. Gary Gray'); GO
This code instructs the database engine to insert only 2 records from the list, regardless of the number of records that are provided.
Instead of specifying a fixed number of records, you can ask the database engine to insert a certain percentage of records. In this case, after TOP (Number), add the PERCENT word. Here is an example:
USE VideoCollection GO INSERT TOP (40) PERCENT INTO Videos(Rating, Title, Director) VALUES(N'', N'Professionals (The)', N'Richard Brooks'), (N'R', N'Trading Places', N'John Landis'), (N'PG-13', N'Cellular', N'David R. Ellis'), (N'R', N'Negotiator (The)', N'F. Gary Gray'), (N'PG-13', N'Big Momma''s House ', N'Raja Gosnell'), (N'G', N'Annie', N'John Huston'); GO
The code provides six records but asks the database engine to add 40% of them. That is 6 / (100/40) = 6 / 2.5 = 2.4. The closest higher integer to this number is 3. Therefore, 3 records are added.
In the techniques we have used so far, when or if the records have been added to a table, whether the operation was successful or not, we had no way of immediately finding out (we would have to open the table). One way you can get this information is to store the inserted records in another table. To support this, Transact-SQL provides the OUTPUT operator. The formula to use it is:
INSERT INTO TableName OUTPUT INSERTED.Columns VALUES(Value_1, Value_2, Value_X)
You start with the normal record insertion with the INSERT INTO TableName expression. This is followed by the OUTPUT operator followed by the INSERTED operator and a period. If you are adding a value for each record, follow the period with *. The statement continues with the VALUES operator that is followed by parentheses in which you list the values to be added to the table. Here is an example:
USE VideoCollection; GO CREATE TABLE Videos ( Title nvarchar(50), Director nvarchar(50), WideScreen bit, Rating nchar(10), YearReleased int ) GO INSERT INTO Videos OUTPUT inserted.* VALUES(N'War of the Roses (The)', N'Dany de Vito', 0, N'R', 2001), (N'Memoirs of a Geisha', N'Rob Marshall', 1, N'PG-13', 2006), (N'Last Castle (The)', N'Rod Lurie', 1, N'', 2001), (N'Sneakers', N'Phil Alden Robinson', 1, N'PG-13', 2003); GO
When this statement executes, if you are working in the Microsoft SQL Server Management Studio, the lower part would display a list of the records that were added:
If you use the above formula, when you close the database, the reference is lost. If you want to store the list of newly created records in a table, on the right side of the INSERTED operator and its period, type INTO followed by the name of the table that will receive the values. The table must have been created; that is, it must exist at the time this insertion operation is taking place. Here is an example:
USE VideoCollection; GO CREATE TABLE Archives ( Title nvarchar(50), Director nvarchar(50), WideScreen bit, Rating nchar(10), YearReleased int ) GO INSERT INTO Videos OUTPUT inserted.* INTO Archives VALUES(N'Two for the Money', N'D.J. Caruso', 1, N'R', 2006), (N'Wall Street', N'Oliver Stone', 0, N'R', 2000); GO
In this case, a copy of the newly created record(s) would be stored in the indicated table.
The above techniques assume that you are adding a complete record; that is, you are providing a value for each column of the table. We already saw that if you want to provide values for only some columns, after the name of the table, provide the list of columns in parentheses. To get the list of newly inserted records, after the OUTPUT keyword, type INSERTED followed by a period and followed by the name of the first column. Do this for each column and separate them with commas. The formula to use is:
INSERT INTO TableName(Column_1, Column_2, Column_X) OUTPUT INSERTED.Column_1, INSERTED.Column_2, INSERTED.Column_X VALUES(Value_1, Value_2, Value_X)
Of course, you can list the columns in any order of your choice, as long as both the TableName and the OUTPUT section use the exact same order. Here is an example:
USE VideoCollection; GO INSERT INTO Videos(Director, Rating, Title) OUTPUT inserted.Director, inserted.Rating, inserted.Title VALUES(N'Jonathan Lynn', N'R', N'Distinguished Gentleman (The)'), (N'Paul Anderson', N'R', N'Soldier'); GO
In this case, when the statement has executed, the result would display in the lower portion of the Microsoft SQL Server Management Studio. If you want to store the result in a table, use the following formula:
INSERT INTO TableName(Column_1, Column_2, Column_X) OUTPUT INSERTED.Column_1, INSERTED.Column_2, INSERTED.Column_X INTO TargetTable VALUES(Value_1, Value_2, Value_X)
Here is an example:
USE VideoCollection; GO CREATE TABLE Entertainment ( Title nvarchar(50), Director nvarchar(50) ) GO INSERT INTO Videos(Title, Director) OUTPUT inserted.Title, inserted.Director INTO Entertainment VALUES(N'Michael Jackson Live in Bucharest', N'Andy Morahan'), (N'Outfoxed', N'Robert Greenwald'); GO
One of the techniques used to get data into one or more tables consists of importing already existing data from another database or from any other recognizable data file. Microsoft SQL Server provides various techniques and means of getting or importing data.
A script is a regular text-based file. In Microsoft SQL Server, the file should have the extension .sql. The script can have any type of code that the database engine can execute. That is, a Transact-SQL script can have any of the topics we will study throughout our lessons.
Using a script in Microsoft SQL Server is usually simple. Probably the easiest way to use a script is to open it as a file in the SQL Server Management Studio (you open the file like any other). Once it is opened, you can execute it. An alternative is to execute a file at the command prompt, in which case you can use either PowerShell or the DOS Command Prompt. To do this, at the prompt, use the following formula:
SQLCMD -i Filename
You start with the SQLCMD application and add the -i flag. This is followed by either only the name of the file or the complete path of the file. Of course, the file name must have the .sql extension.
It is possible to import a Microsoft Access database but it is easier if the file is in the .mdb format.
Spreadsheets are probably the easiest files to import in Microsoft SQL Server. This is because a spreadsheet is already created as a table, with the necessary columns and rows. The only real concern is when you are creating the spreadsheet. Although you can put anything in it, you should make sure the Microsoft SQL Server database engine would be able to identify the area where the actual records are (where the records start and where they end).
One of the types of data you can import into Microsoft SQL Server is a 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 records.
After importing data, you should verify and possibly format it to customize its fields.
To import a text file that contains records: