SlideShare a Scribd company logo
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
120
Lession 7: Records Maintenance
Data Import/Export
Data Import
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 importing data.
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.
Practical Learning: Importing Data From an External Source
1. Download the Students text file and save it to your hard drive
2. In the SQL Server Management Studio, right-click the Databases node and click New
Database...
3. Type ROSH and press Enter
4. In the Object Explorer, right-click ROSH, position the mouse on Tasks and click Import
Data
5. On the first page of the wizard, click Next
6. On the second page, click the arrow of the Data Source combo box and select Flat File
Source
7. On the right side of File Name, click the Browse button
8. Locate and select the Students.txt file you had saved
9. On the left side, click Columns
10.On the left side, click Advanced
11.As Column 0 is selected, in the right list, click Name and type StudentID
12.Click DataType and click the arrow of its combo box. Select Two-byte unsigned integer
[DT_UI2]
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
121
13.In the middle list, click each column and change its Name in the right column as follows:
Name DataType OutputColumnWidth
StudentID Two-byte unsigned integer [DT_UI2]
FirstName Unicode string [DT_WSTR] 20
LastName Unicode string [DT_WSTR] 20
DateOfBirth database date [DT_DBDATE]
Gender Unicode string [DT_WSTR] 12
Address Unicode string [DT_WSTR]
City Unicode string [DT_WSTR] 40
State Unicode string [DT_WSTR] 2
ZIPCode Unicode string [DT_WSTR] 12
HomePhone Unicode string [DT_WSTR] 16
EmailAddress Unicode string [DT_WSTR]
ParentsNames Unicode string [DT_WSTR]
SPHome Boolean [DT_BOOL]
EmrgName Unicode string [DT_WSTR]
EmrgPhone Unicode string [DT_WSTR] 16
14.To see the list of columns, under Data Source, click Columns
15.Click Next 3 times:
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
122
16.Click Next twice
17.Click Finish
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
123
18.Click Close
Record Maintenance: Updating Records
Introduction
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.
Practical Learning: Introducing Record Maintenance
1. On the Standard toolbar, click the New Query button
2. To create a database, type the following:
USE master;
GO
IF EXISTS(SELECT name
FROM sys.databases
WHERE name = N'CeilInn2'
)
DROP DATABASE CeilInn2
GO
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
124
CREATE DATABASE CeilInn2
GO
USE CeilInn2;
GO
CREATE TABLE Rooms (
RoomID int identity(1, 1) NOT NULL,
RoomNumber nvarchar(10),
RoomType nvarchar(20) default N'Bedroom',
BedType nvarchar(40) default N'Queen',
Rate money default 75.85,
Available bit default 0
);
GO
INSERT INTO Rooms(RoomNumber) VALUES(104);
GO
INSERT INTO Rooms(RoomNumber, BedType, Rate, Available)
VALUES(105, N'King', 85.75, 1),
(106, N'King', 85.75, 1);
GO
INSERT INTO Rooms(RoomNumber, Available) VALUES(107, 1);
GO
INSERT INTO Rooms(RoomNumber, BedType, Rate)
VALUES(108, N'King', 85.75);
GO
INSERT INTO Rooms(RoomNumber, Available) VALUES(109, 1);
GO
INSERT INTO Rooms(RoomNumber, RoomType, Rate, BedType, Available)
VALUES(110, N'Conference', 450.00, N'', 1);
GO
3. Press F5 to execute
Visually Selecting Records
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.
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
125
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 all Records
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:
In the Object Explorer, you can right the table, position the mouse on Script Table As ->
UPDATE To -> New Query Editor Window
Open an empty query window and type your code
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;
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
126
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:
Record Maintenance: Editing Records
Introduction
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
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
127
is wrong, such as the Director of the the 'The Distinguished Gentleman' video of this table:
Video Title Director © Year Length Rating
A Few Good Men Rob Reiner 1992 138 Minutes R
The Silence of the Lambs Jonathan Demme 1991 118 Minutes
The Distinguished Gentleman James Groeling 112 Minutes R
The Lady Killers Joel Coen & Ethan Coen 104 Minutes R
Ghosts of Mississippi Rob Reiner 130 Minutes
Editing a Record
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 IDENTITYfeature, 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';
Practical Learning: Editing a Record
1. Press Ctrl + A to select everything (the code) in the window and, to update a record, type
the following:
USE CeilInn2;
GO
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
128
UPDATE Rooms
SET BedType = N'Queen'
WHERE RoomNumber = 108;
GO
2. Press F5 to execute
3. Press Ctrl + A to select the code in the window and type the following:
USE CeilInn2;
GO
UPDATE Rooms
SET Available = 0
WHERE RoomNumber = 109;
GO
4. Press F5 to execute
Outputting the Update
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 OUTPUTINSERTED 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.
Practical Learning: Outputting the Update
1. Press Ctrl + A to select the code in the window
2. To update some records and show which ones were changed, type the following:
USE CeilInn2;
GO
UPDATE Rooms
SET Rate = 95.50
OUTPUT INSERTED.*
WHERE BedType = N'Queen';
GO
3. Press F5 to execute
Record Maintenance: Deleting Records
Removing all Records
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
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
129
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.
Removing a Record
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 the Object Explorer, you can right the table, position the mouse on Script Table As -
>DELETE To -> New Query Editor Window
Open an empty query window and type your code
In SQL, to delete a record, use the DELETE FROM statement associate the WHEREoperator.
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
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
130
WHERE VideoTitle = N'The Lady Killers';
Here is an example used to clear the table of all videos:
DELETE FROM Videos;
Outputting the Deleted Result
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
Practical Learning: Ending the Lesson
1. Close the query window without saving the file
2. In the Object Explorer, under the Databases node, right-click WorldStatistics and click
Delete
3. In the dialog box, click OK
4. In the Object Explorer, under the Databases node, right-click CeilInn2 and click Delete
5. In the dialog box, click OK
Data Entry and Permissions
Introduction
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
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
131
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.
Managing Permissions on a Table
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:
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:
ALTER: If this permission is granted, a user can open a table in design view and change its
structure (design). For example, a person can add a new column or delete an existing
column. In most cases, regular users should be denied this right
INSERT: This permission allows a user to create new records. If you don't want a user to
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
132
add new records to the table, then deny this permission
UPDATE: This is one of the most important permissions when it comes to data entry
DELETE: This permission allows a user to delete one or more records. The reverse is to
deny this right so the user can only see the records but cannot remove them
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, that is, the 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 wan to grant the permission(s) to more than one account, separate them with commas.
Extending Permissions
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
Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd
Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn
133
Users or Roles section, select the user. In the Persmissions, 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.

More Related Content

What's hot

WPF Application
WPF ApplicationWPF Application
WPF Application
Akshay Sharma
 
Form4 cd4
Form4 cd4Form4 cd4
Form4 cd4
smktsj2
 
Frames tables forms
Frames tables formsFrames tables forms
Frames tables forms
nobel mujuji
 
Notacd04
Notacd04Notacd04
Notacd04
Azmiah Mahmud
 
Notacd04
Notacd04Notacd04
Notacd04
cikgushaharizan
 
Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)
club23
 

What's hot (6)

WPF Application
WPF ApplicationWPF Application
WPF Application
 
Form4 cd4
Form4 cd4Form4 cd4
Form4 cd4
 
Frames tables forms
Frames tables formsFrames tables forms
Frames tables forms
 
Notacd04
Notacd04Notacd04
Notacd04
 
Notacd04
Notacd04Notacd04
Notacd04
 
Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)
 

Viewers also liked

Lession 4 the tables of a database
Lession 4 the tables of a databaseLession 4 the tables of a database
Lession 4 the tables of a database
Đỗ Đức Hùng
 
Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...
Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...
Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...
Dr. Ivo Pezzuto
 
Lession 3 introduction to database
Lession 3 introduction to databaseLession 3 introduction to database
Lession 3 introduction to database
Đỗ Đức Hùng
 
Pre production jubel evailuation v2 1
Pre production jubel evailuation v2 1Pre production jubel evailuation v2 1
Pre production jubel evailuation v2 1
jubel123
 
Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)
Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)
Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)
Dr. Ivo Pezzuto
 
Ivo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUE
Ivo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUEIvo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUE
Ivo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUE
Dr. Ivo Pezzuto
 
Lession 2 starting with mssqlserver
Lession 2 starting with mssqlserverLession 2 starting with mssqlserver
Lession 2 starting with mssqlserver
Đỗ Đức Hùng
 
Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015
Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015 Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015
Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015
Dr. Ivo Pezzuto
 
Не стареют душой ветераны...
Не стареют душой ветераны...Не стареют душой ветераны...
Не стареют душой ветераны...school44
 
Mobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurMobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi Luhur
Riza Fahmi
 
Lession 6.introduction to records
Lession 6.introduction to recordsLession 6.introduction to records
Lession 6.introduction to records
Đỗ Đức Hùng
 
Hanh trangviet.ucoz.net giao trinh sql server 2005
Hanh trangviet.ucoz.net giao trinh sql server 2005Hanh trangviet.ucoz.net giao trinh sql server 2005
Hanh trangviet.ucoz.net giao trinh sql server 2005Đỗ Đức Hùng
 
Đề Cương ôn tập kiến trúc máy tính và thiết bị ngoại vi
Đề Cương ôn tập kiến trúc máy tính và thiết bị ngoại viĐề Cương ôn tập kiến trúc máy tính và thiết bị ngoại vi
Đề Cương ôn tập kiến trúc máy tính và thiết bị ngoại viĐỗ Đức Hùng
 
Modelos de desarrollo
Modelos de desarrolloModelos de desarrollo
Modelos de desarrollo
Oscar Sandoval
 

Viewers also liked (16)

Lession 4 the tables of a database
Lession 4 the tables of a databaseLession 4 the tables of a database
Lession 4 the tables of a database
 
Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...
Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...
Ivo Pezzuto - UNITED STATES: IS A SLOWDOWN IN THE OFFING? (The Global Analyst...
 
Lession 3 introduction to database
Lession 3 introduction to databaseLession 3 introduction to database
Lession 3 introduction to database
 
Pre production jubel evailuation v2 1
Pre production jubel evailuation v2 1Pre production jubel evailuation v2 1
Pre production jubel evailuation v2 1
 
Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)
Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)
Ivo Pezzuto's Economic and Geopolitical Overview of Mexico (2013)
 
Ivo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUE
Ivo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUEIvo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUE
Ivo Pezzuto - THE GLOBAL ANALYST MAGAZINE AUGUST 2015 ISSUE
 
Lession 2 starting with mssqlserver
Lession 2 starting with mssqlserverLession 2 starting with mssqlserver
Lession 2 starting with mssqlserver
 
Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015
Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015 Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015
Ivo Pezzuto - THE GLOBAL ANALYST JUNE 2015
 
Не стареют душой ветераны...
Не стареют душой ветераны...Не стареют душой ветераны...
Не стареют душой ветераны...
 
Mobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurMobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi Luhur
 
Lession 6.introduction to records
Lession 6.introduction to recordsLession 6.introduction to records
Lession 6.introduction to records
 
Bai giang he qtdl
Bai giang he qtdlBai giang he qtdl
Bai giang he qtdl
 
Hanh trangviet.ucoz.net giao trinh sql server 2005
Hanh trangviet.ucoz.net giao trinh sql server 2005Hanh trangviet.ucoz.net giao trinh sql server 2005
Hanh trangviet.ucoz.net giao trinh sql server 2005
 
Đề cương xử lý ảnh
Đề cương xử lý ảnhĐề cương xử lý ảnh
Đề cương xử lý ảnh
 
Đề Cương ôn tập kiến trúc máy tính và thiết bị ngoại vi
Đề Cương ôn tập kiến trúc máy tính và thiết bị ngoại viĐề Cương ôn tập kiến trúc máy tính và thiết bị ngoại vi
Đề Cương ôn tập kiến trúc máy tính và thiết bị ngoại vi
 
Modelos de desarrollo
Modelos de desarrolloModelos de desarrollo
Modelos de desarrollo
 

Similar to Lession 7 records maintenance

Database Management System - SQL beginner Training
Database Management System - SQL beginner Training Database Management System - SQL beginner Training
Database Management System - SQL beginner Training
Moutasm Tamimi
 
Lsmw by guntupalliharikrishna
Lsmw by guntupalliharikrishnaLsmw by guntupalliharikrishna
Lsmw by guntupalliharikrishna
Hari Krishna
 
Winforms
WinformsWinforms
SAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdfSAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdf
Phani Pavan
 
SAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdfSAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdf
Phani Pavan
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
Andi Simanjuntak
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
Mir Mahmood
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
J. C.
 
Multiple files single target single interface
Multiple files single target single interfaceMultiple files single target single interface
Multiple files single target single interface
Dharmaraj Borse
 
Events Registration System Part 1
Events Registration System Part 1Events Registration System Part 1
Events Registration System Part 1
Adolfo Nasol
 
How to Download IDEA for Windows Based ComputersSelect the dow.docx
How to Download IDEA for Windows Based ComputersSelect the dow.docxHow to Download IDEA for Windows Based ComputersSelect the dow.docx
How to Download IDEA for Windows Based ComputersSelect the dow.docx
wellesleyterresa
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
cmontanez
 
Mca 504 dotnet_unit5
Mca 504 dotnet_unit5Mca 504 dotnet_unit5
Db2
Db2Db2
Db2
yboren
 
Qtp day 3
Qtp day 3Qtp day 3
Qtp day 3
Prashanth BS
 
Lsmw for master data upload simple explanation
Lsmw for master data upload simple explanationLsmw for master data upload simple explanation
Lsmw for master data upload simple explanation
Manoj Kumar
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
AnasYunusa
 
Tables in dd
Tables in ddTables in dd
Tables in dd
Ganesh Kumar
 
Database Course Project-Usage Documentation
Database Course Project-Usage DocumentationDatabase Course Project-Usage Documentation
Database Course Project-Usage Documentation
Justin Tate
 
Lession 5 the columns of a table
Lession 5 the columns of a tableLession 5 the columns of a table
Lession 5 the columns of a table
Đỗ Đức Hùng
 

Similar to Lession 7 records maintenance (20)

Database Management System - SQL beginner Training
Database Management System - SQL beginner Training Database Management System - SQL beginner Training
Database Management System - SQL beginner Training
 
Lsmw by guntupalliharikrishna
Lsmw by guntupalliharikrishnaLsmw by guntupalliharikrishna
Lsmw by guntupalliharikrishna
 
Winforms
WinformsWinforms
Winforms
 
SAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdfSAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdf
 
SAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdfSAP ABAP lsmw beginner lerning tutorial.pdf
SAP ABAP lsmw beginner lerning tutorial.pdf
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
Multiple files single target single interface
Multiple files single target single interfaceMultiple files single target single interface
Multiple files single target single interface
 
Events Registration System Part 1
Events Registration System Part 1Events Registration System Part 1
Events Registration System Part 1
 
How to Download IDEA for Windows Based ComputersSelect the dow.docx
How to Download IDEA for Windows Based ComputersSelect the dow.docxHow to Download IDEA for Windows Based ComputersSelect the dow.docx
How to Download IDEA for Windows Based ComputersSelect the dow.docx
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
 
Mca 504 dotnet_unit5
Mca 504 dotnet_unit5Mca 504 dotnet_unit5
Mca 504 dotnet_unit5
 
Db2
Db2Db2
Db2
 
Qtp day 3
Qtp day 3Qtp day 3
Qtp day 3
 
Lsmw for master data upload simple explanation
Lsmw for master data upload simple explanationLsmw for master data upload simple explanation
Lsmw for master data upload simple explanation
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
 
Tables in dd
Tables in ddTables in dd
Tables in dd
 
Database Course Project-Usage Documentation
Database Course Project-Usage DocumentationDatabase Course Project-Usage Documentation
Database Course Project-Usage Documentation
 
Lession 5 the columns of a table
Lession 5 the columns of a tableLession 5 the columns of a table
Lession 5 the columns of a table
 

Recently uploaded

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 

Recently uploaded (20)

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 

Lession 7 records maintenance

  • 1. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 120 Lession 7: Records Maintenance Data Import/Export Data Import 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 importing data. 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. Practical Learning: Importing Data From an External Source 1. Download the Students text file and save it to your hard drive 2. In the SQL Server Management Studio, right-click the Databases node and click New Database... 3. Type ROSH and press Enter 4. In the Object Explorer, right-click ROSH, position the mouse on Tasks and click Import Data 5. On the first page of the wizard, click Next 6. On the second page, click the arrow of the Data Source combo box and select Flat File Source 7. On the right side of File Name, click the Browse button 8. Locate and select the Students.txt file you had saved 9. On the left side, click Columns 10.On the left side, click Advanced 11.As Column 0 is selected, in the right list, click Name and type StudentID 12.Click DataType and click the arrow of its combo box. Select Two-byte unsigned integer [DT_UI2]
  • 2. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 121 13.In the middle list, click each column and change its Name in the right column as follows: Name DataType OutputColumnWidth StudentID Two-byte unsigned integer [DT_UI2] FirstName Unicode string [DT_WSTR] 20 LastName Unicode string [DT_WSTR] 20 DateOfBirth database date [DT_DBDATE] Gender Unicode string [DT_WSTR] 12 Address Unicode string [DT_WSTR] City Unicode string [DT_WSTR] 40 State Unicode string [DT_WSTR] 2 ZIPCode Unicode string [DT_WSTR] 12 HomePhone Unicode string [DT_WSTR] 16 EmailAddress Unicode string [DT_WSTR] ParentsNames Unicode string [DT_WSTR] SPHome Boolean [DT_BOOL] EmrgName Unicode string [DT_WSTR] EmrgPhone Unicode string [DT_WSTR] 16 14.To see the list of columns, under Data Source, click Columns 15.Click Next 3 times:
  • 3. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 122 16.Click Next twice 17.Click Finish
  • 4. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 123 18.Click Close Record Maintenance: Updating Records Introduction 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. Practical Learning: Introducing Record Maintenance 1. On the Standard toolbar, click the New Query button 2. To create a database, type the following: USE master; GO IF EXISTS(SELECT name FROM sys.databases WHERE name = N'CeilInn2' ) DROP DATABASE CeilInn2 GO
  • 5. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 124 CREATE DATABASE CeilInn2 GO USE CeilInn2; GO CREATE TABLE Rooms ( RoomID int identity(1, 1) NOT NULL, RoomNumber nvarchar(10), RoomType nvarchar(20) default N'Bedroom', BedType nvarchar(40) default N'Queen', Rate money default 75.85, Available bit default 0 ); GO INSERT INTO Rooms(RoomNumber) VALUES(104); GO INSERT INTO Rooms(RoomNumber, BedType, Rate, Available) VALUES(105, N'King', 85.75, 1), (106, N'King', 85.75, 1); GO INSERT INTO Rooms(RoomNumber, Available) VALUES(107, 1); GO INSERT INTO Rooms(RoomNumber, BedType, Rate) VALUES(108, N'King', 85.75); GO INSERT INTO Rooms(RoomNumber, Available) VALUES(109, 1); GO INSERT INTO Rooms(RoomNumber, RoomType, Rate, BedType, Available) VALUES(110, N'Conference', 450.00, N'', 1); GO 3. Press F5 to execute Visually Selecting Records 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.
  • 6. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 125 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 all Records 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: In the Object Explorer, you can right the table, position the mouse on Script Table As -> UPDATE To -> New Query Editor Window Open an empty query window and type your code 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;
  • 7. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 126 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: Record Maintenance: Editing Records Introduction 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
  • 8. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 127 is wrong, such as the Director of the the 'The Distinguished Gentleman' video of this table: Video Title Director © Year Length Rating A Few Good Men Rob Reiner 1992 138 Minutes R The Silence of the Lambs Jonathan Demme 1991 118 Minutes The Distinguished Gentleman James Groeling 112 Minutes R The Lady Killers Joel Coen & Ethan Coen 104 Minutes R Ghosts of Mississippi Rob Reiner 130 Minutes Editing a Record 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 IDENTITYfeature, 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'; Practical Learning: Editing a Record 1. Press Ctrl + A to select everything (the code) in the window and, to update a record, type the following: USE CeilInn2; GO
  • 9. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 128 UPDATE Rooms SET BedType = N'Queen' WHERE RoomNumber = 108; GO 2. Press F5 to execute 3. Press Ctrl + A to select the code in the window and type the following: USE CeilInn2; GO UPDATE Rooms SET Available = 0 WHERE RoomNumber = 109; GO 4. Press F5 to execute Outputting the Update 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 OUTPUTINSERTED 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. Practical Learning: Outputting the Update 1. Press Ctrl + A to select the code in the window 2. To update some records and show which ones were changed, type the following: USE CeilInn2; GO UPDATE Rooms SET Rate = 95.50 OUTPUT INSERTED.* WHERE BedType = N'Queen'; GO 3. Press F5 to execute Record Maintenance: Deleting Records Removing all Records 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
  • 10. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 129 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. Removing a Record 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 the Object Explorer, you can right the table, position the mouse on Script Table As - >DELETE To -> New Query Editor Window Open an empty query window and type your code In SQL, to delete a record, use the DELETE FROM statement associate the WHEREoperator. 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
  • 11. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 130 WHERE VideoTitle = N'The Lady Killers'; Here is an example used to clear the table of all videos: DELETE FROM Videos; Outputting the Deleted Result 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 Practical Learning: Ending the Lesson 1. Close the query window without saving the file 2. In the Object Explorer, under the Databases node, right-click WorldStatistics and click Delete 3. In the dialog box, click OK 4. In the Object Explorer, under the Databases node, right-click CeilInn2 and click Delete 5. In the dialog box, click OK Data Entry and Permissions Introduction 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
  • 12. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 131 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. Managing Permissions on a Table 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: 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: ALTER: If this permission is granted, a user can open a table in design view and change its structure (design). For example, a person can add a new column or delete an existing column. In most cases, regular users should be denied this right INSERT: This permission allows a user to create new records. If you don't want a user to
  • 13. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 132 add new records to the table, then deny this permission UPDATE: This is one of the most important permissions when it comes to data entry DELETE: This permission allows a user to delete one or more records. The reverse is to deny this right so the user can only see the records but cannot remove them 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, that is, the 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 wan to grant the permission(s) to more than one account, separate them with commas. Extending Permissions 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
  • 14. Lecturer: Tran Dinh Vuong Home page: www.fit.vimaru.edu.vn/~vuongtd Phone : 0982.113.274 Email : vuongtd@vimaru.edu.vn 133 Users or Roles section, select the user. In the Persmissions, 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.