Download Using SQL Server Management Studio

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Serializability wikipedia , lookup

DBase wikipedia , lookup

IMDb wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft Access wikipedia , lookup

Concurrency control wikipedia , lookup

Functional Database Model wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SQL wikipedia , lookup

Ingres (database) wikipedia , lookup

Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

PL/SQL wikipedia , lookup

Versant Object Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

ContactPoint wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
Using SQL Server Management Studio
Microsoft SQL Server Management Studio 2005 is a graphical tool for database designer or programmer.
With SQL Server Management Studio 2005 you can:
• Create databases
• Insert tables to databases
• Insert column to tables
• Insert primary keys and indexes to tables
• Create SQL-sentences graphically
• And so on.
1. Starting SQL Server Management Studio
You can obtain Microsoft SQL Server Management Studio from two different sources:
1. You can install Microsoft SQL Server 2005. SQL Server Management Studio 2005 is part of the
product.
2. You can download SQL Server Management Studio 2005 from Microsoft SQL Server Express
download page. The address of the page is:
http://msdn.microsoft.com/vstudio/express/sql/download/
When Microsoft SQL Server Management Studio is started you will have the dialog window below:
Picture: The Connect window starting SQL Server Management Studio
In dialog window you will have the following information:
Server type gives you the possibility to choose from the Database Engine and different type of services.
You should select Database Engine.
Server name is the name of the server you are using. SQL Server Express has typically the computer
name, back slash and SQLEXPRESS concatenated. SQL Server 2005 is recognized with the computer
name only. You can browse for the servers if they are not available.
Authentication is needed for rights to use the server. You can choose Windows Authentication (you will
get the rights when you sign in) or SQL Server Authentication (you have to give correct user name and
password).
After you have filled the dialog you press Connect command button. Connection to database is opened.
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
Picture: Connection to the database is opened
When connection to the database is opened you will have the window above. You can find three frames:
1. Registered Servers frame show you the list of the databases that can be achieved using the
Database Engine. In the example above there is Microsoft SQL Server 2005 (do3023|18, not
connected, red icon) and Microsoft SQL Server Express (do3023|18\sqlexpress, connected, green
icon)
2. Object Explorer frame shows you the main content or objects of database you have connected
to.
3. Summary frame is the main working area for you. The content of the frame will change
according to the work you are doing.
2. Creating a database
When you start with Microsoft SQL Server Management Studio you have to create the database:
1. Choose Databases object from the Object Explorer frame and press right mouse button
2. Choose command New Database from the menu
You will get the New Database Window. In window you have the following information:
1.
2.
3.
Database name is the name of the new database you are creating
Owner is the owner of the database. Usually default will do.
Database files are the new files you are creating for your database. Usually you have two files
the database file and the log file. Consider the size of the database and reserve the initial size
according to that. Remember that file extensions added later will work but are slower than the
database file you create at the beginning.
When you have added the name of the database to the window, you press command button OK and the
database is created.
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
Picture: New Database window
When you have created a database called customer your Object Browser frame might look like this:
Picture: Object Browser frame after creating a Customer database
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
3. Adding tables to database
When the database is created you can add tables to the database:
1. Choose Customer database object from the Object Explorer frame and press right mouse button
2. Choose command New Table from the menu
Picture: Creating tables to the database
The summary frame is divided to two parts. Upper part is used to create columns to the table. The part
below has more precise information about each column.
Create the columns you need for the table. For each column you have to give:
• The name of the column. Remember – this is the name you have to use in the program.
• Data type for the column. Choose the date type according to the content of the column.
• Information about null-values. You can allow or permit the null values.
• Information about primary keys. Primary key can be seen at the beginning of the line
• Close the Table folder
• Question dialog asks a permission to save the table declaration. Answer Yes to the question
dialog
• Choose Name –dialog asks name to the table. Enter name to the table and press OK -button.
Picture: Choose Name –dialog for naming the table
Helsinki Business Polytechnic
Markku Kuitunen
4. Data
Manual
20.04.2006
types
4.1 Number types
Number data types are used to express numbers. Number data types can be divided to exact number types
and approximate number types.
Name
bigint
int
smallint
tinyint
bit
decimal
numeric
money
smallmoney
Decimal range
Binary size
(-9,223,372,036,854,775,808 –
9,223,372,036,854,775,807)
(-2,147,483,648 - 2,147,483,647)
(-32,768 - 32,767)
( 0 – 255)
Storage size
2 ** 63
8 bytes
2 ** 31
2 ** 15
2 ** 8
4 bytes
2 bytes
1 byte
Byte for 8 bits
10 numbers 4 types
Functionally equivalent to decimal
-922,337,203,685,477.5808 to
922,337,203,685,477.5807
8 bytes
- 214,748.3648 to 214,748.3647
4 bytes
Table: Exact number types
Approximate number types are called float and real. Actually real is also a floating point number that is
expressed with 24 bit in mantissa, when float uses 48 bits in mantissa.
Data type
Precision in digits
real
float
Mantissa
7 digits
15 digits
Storage size
24 bits
53 bits
4 bytes
8 bytes
Picture: Approximate number types
4.2 Date and time types
Date and time types are used to save information about date and time or date or time.
Data type
Start date
datetime
smalldatetime
End date
01.01.1753
01.01.1900
Accuracy
31.12.9999
06.06.2079
3,33 ms
1 min
Storage size
8 bytes
4 bytes
Picture: Date and time types
Examples:
01/10/2006 12:30:00,500
01/10/2006 12:30
4.3 Character strings
Character strings are user to save character data.
Data type
char
varchar
text
Information
Non-Unicode character data
Variable length non-Unicode
character data
Variable length non-Unicode
Use
When static length
When variable length
Don’t use. Will be
Storage size
Length
Length + 2
Helsinki Business Polytechnic
Markku Kuitunen
nchar
nvarchar
ntext
Manual
20.04.2006
data
Unicode character data
Variable length Unicode
character data
Variable length Unicode data
removed in future
When multi-language
data is needed
When variable length
Length
2 * Length + 2
Don’t use. Will be
removed in future
Picture: Types for character data
4.4 Binary strings
Binary string save binary type data to the database.
Data type
binary
varbinary
image
Information
Binary data
Variable length binary data
Variable length binary data
handled as a picture
Use
When static length
When variable length
When picture data is
needed
Storage size
Length
Length + 2
Length + 2
Picture: Types for binary data
4.5 Other data types
cursor
sql_variant
table
timestamp
uniqueidentifier
xml
is a data type for variables or stored procedures output parameters when cursor data
type is needed.
enables database objects to support values of other data types.
is used to store a result set for processing later.
is used to determine whether any value in the row has changed since the last time it
was read.
is used to comparing the bit patterns of the two values.
is data type for xml document instance.
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
5 Adding database diagram and relationship between tables
When tables are created it is possible to create a database diagram and relationships between tables. When
you open a database object in object explorer you can find folders Database Diagrams and Tables. Tables
folder contains all the tables in the database. When tables have been created it is possible to construct a
database diagram for the database.
Picture: Object explorer when tables have been created for the database
Create a database diagram:
1. Choose Database Diagrams from the Customer database object folders and press right mouse
button
2. Choose command New Database Diagram from the menu
3. Add Table dialog window is opened
4. Choose the table you want to add from the dialog and press Add –command button
5. When all necessary tables have been chosen press Close command button.
Picture: Database Diagram Add Table –dialog window
When all tables have been chosen the Database Diagram is opened.
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
Picture: The Database Diagram when tables had been added to the diagram
Now it is possible to add relationships between the tables. Before the connection can be created there must
be foreign keys added for the relationships.
To create a relationship:
1. Choose the identifier that is used for the relationship (customer –table id –column). You can
choose several column if it is necessary
2. Drag and drop the column to pointing foreign key column in other table (order-table cid-column
in the example)
3. Dialog window for the relationship definition is opened
4. Press OK button in the dialog window when the relationship is correctly defined.
Picture: Dialog window for relationship definition
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
Dialog window has a textbox for relationship name. It is possible to use the generic name the dialog
suggests or give an own name to the relationship. It is possible to change the primary key table if it is
needed.
Picture: Foreign Key Relationship dialog window
In the Foreign Key Relationship dialog window following properties can be found:
1. Check Existing Data On Creation Or Re-Enabling. Possible values are Yes and No. Usually
Yes is selected to check the validity of the reference
2. Tables and Columns Specification is a list of the table and column declaration for the reference.
It can be opened from the plus-icon
3. Enforce For Replication when insert, update or delete is performed by the replication agent
4. Enforce Foreign Key Constraint specify is changes are allowed for columns connected to the
relationship if this would invalidate the relationship
5. INSERT And UPDATE Specification give a possibility to add cascading rules for the
commands:
• No Action causes an error message if the deletion is not allowed
• Cascade deletes the rows connected to the relationship
• Set Null causes null value for the filed if all foreign key columns for the table can accept
null values
• Set Default sets the default value for the column if all foreign key columns for the table
have defaults defined
6. Identity gives the possibility to define the name and description for the relationship.
When the dialog is filled the relationship is graphically displayed in the picture.
Picture: Graphical representation of the relationship
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
6. Adding content to the tables
Content can be added to the tables using Microsoft SQL Server Management tool. Do the following:
1. Choose the database folder from the Object Explorer
2. Choose folder Tables from the Object Explorer
3. Choose the table from the Tables folder and press right button
4. Choose Open Table from the menu
5. Add content to the table
6. Close the table folder.
Picture: Adding content to table
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
7. Designing queries
Microsoft SQL Server Management Tool can give graphical help for designing queries. Do the following:
1. Press New Query icon in the icon toolbar
2. Select the database from the icon toolbar
3.
4.
5.
Choose Design Query Editor from the toolbar
Choose the tables from dialog window
Fill the Query Designer window
Picture: Adding tables –dialog window
Picture: Query Designer Window
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
To design a query in Query Designer window do the following:
1.
2.
3.
4.
5.
Choose the columns needed from the tables by selecting the checkboxes
Choose the sort column by filling the sort order number to the textbox of the column used for
sorting
Choose the sorting order selecting the sorting order from the Sort Type combobox
Add filtering rules to the Filter text box of the filtering column
Click OK-button
Query Design window looks like this:
Picture: Query Designer window in use
Now the query has been designed. You can save the query selecting Save SqlQuery As from the File
menu.
Picture: Microsoft SQL Server Management Studio window when the query has been designed
Helsinki Business Polytechnic
Markku Kuitunen
Manual
20.04.2006
When the query has been designed it can also be executed. Press the !Execute icon from the icon toolbar.
When the query has been executed the resultset will be shown at the bottom of the window. The resultset
or text form
can be seen either in table form
toolbar. The resultset can also be saved to a file.
by selecting the corresponding icon from the icon
Picture: The resultset in the window when the query have been executed
It is possible to modify the query in the window and execute the query again. If there are errors in the
query the error message will be shown at the bottom of the window instead of the resultset.
It is possible to run other type of commands instead of the SELECT command. For instance INSERT,
UPDATE od DELETE commands can be executed.