Академический Документы
Профессиональный Документы
Культура Документы
<a
Introduction to Databases
Introduction to Database Creation
Introduction
Probably before using a database, you must first have one. A database is primarily a group of computer files that each has a name and a location. Just as there are different ways to connect to a server, in the same way, there are also different ways to create a database. To create a new database in SQL Server Management Studio, in the Object Explorer, you can rightclick the Databases node and click New Database... This would open the New Database dialog box.
A name can start with either a letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z), a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), an underscore (_) or a non-readable character. Examples are _n, act, %783, Second After the first character (letter, digit, underscore, or symbol), the name can have combinations of underscores, letters, digits, or symbols. Examples are _n24 or act_52_t A name can include spaces. Example are c0untries st@ts, govmnt (records), or gl0b# $urvey||
Because of the flexibility of SQL, it can be difficult to maintain names in a database. Based on this, there are conventions we will use for our objects. In fact, we will adopt the rules used in C/C++, C#, Pascal, Java, and Visual Basic, etc. In our databases:
q
Unless stated otherwise (we will mention the exception, for example with variables, tables, etc), a name will start with either a letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z) or an underscore After the first character, we will use any combination of letters, digits, or underscores A name will not start with two underscores If the name is a combination of words, at least the second word will start in uppercase. Examples are Countries Statistics, Global Survey, _RealSport, FullName, or DriversLicenseNumber
After creating an object whose name includes space, whenever you use that object, include its name between [ and ]. Examples are [Countries Statistics], [Global Survey], or [Date of Birth]. Even if you had created an object with a name that doesn't include space, when using that name, you can still include it in square brackets. Examples are [UnitedStations], [FullName], [DriversLicenseNumber], and [Country].
1. To launch Microsoft SQL Server, click Start -> (All) Programs -> Microsoft SQL Server 2005 -> SQL Server Management Studio 2. On the Connect to Server dialog box, make the correct selections and provide the appropriate information, then click Connect 3. In the Object Explorer, expand the server name followed by the Databases node 4. Right-click Databases and click New Database...
2. Notice the file name and the location. Scroll up to the name (or letter) of the drive to see where the path starts 3. Click Cancel
Using a Wizard
To specify more options with code, Microsoft SQL Server ships with various sample codes you can use for different assignments. For example, you can use sample code to create a database. The sample codes that Microsoft SQL Server are accessible from the Template Explorer. To access the Template Explorer, on the main menu, you can click View -> Template Explorer. Before creating a database, open a new query window. Then:
q
To create a new database using sample code, in the Template Explorer, expand the Databases node, then drag the Create Database node and drop it in the query window. The new database would be created in the server that holds the current connection If you have access to more than one server, to create a database in another server or using a different connection, in the Template Explorer, expand the Databases node, right-click Create Database and click Open. In the Connect to Database Engine dialog box, select the appropriate options, and can click OK
With any of these actions, Microsoft SQL Server would generate sample code for you: -- ============================================= -- Create database template -- ============================================= USE master GO -- Drop the database if it already exists IF EXISTS ( SELECT name
file:///C|/Documents%20and%20Settings/michael.musyoki/Desktop/SS/Lesson02.htm (5 of 10)11/27/2008 8:03:15 AM
FROM sys.databases WHERE name = N'<Database_Name, sysname, Database_Name>' ) DROP DATABASE <Database_Name, sysname, Database_Name> GO CREATE DATABASE <Database_Name, sysname, Database_Name> GO You would then need to edit the code and execute it to create the database. From the previous lessons and sections, we have reviewed some characters uch as the comments -- and some words or expressions such as GO, CREATE DATABASE, and SELECT. We will study the other words or expressions in future lessons and sections.
Database Maintenance
Introduction
If you have created a database but don't need it anymore, you can delete it. It is important to know, regardless of how you create a database, whether using SQL Server Management Studio, code in the query window, or the Command Prompt, every database can be accessed by any of these tools and you can delete any of the databases using any of these tools. As done with creating a database, every tool provides its own means.
1. On the Standard toolbar, click the New Query button 2. To delete a database, type: DROP DATABASE RealEstate1; GO
Database Routines
The Current Database
While writing code in a Query Window, you should always know what database you are working on, otherwise you may add code to the wrong database. To programmatically specify the current database, type the USE keyword followed by the name of the database. The formula to use is: USE DatabaseName; Here is an example: USE GovernmentStatistics;
Besides identifying who accesses the system, the master database also keeps track of everything you do on the server, including creating and managing databases. You should not play with the master database; otherwise you may corrupt the system. For example, if the master database is not functioning right, the system would not work.
A Namespace
A namespace is a technique of creating a series of items that each has a unique name. For example, if you start creating many databases, there is a possibility that you may risk having various databases with the same name. If using a namespace, you can isolate the databases in various namespaces. In reality, to manage many other aspects of your database server, you use namespaces and you put objects, other than databases, within those namespaces. Therefore, a namespace and its content can be illustrated as follows:
To manage the schemas in a namespace, you need a way to identify each schema. B ased on this, each schema must have a name. In our illustration, one schema is named Schema1. Another schema is named Schema2. Yet another schema is named Schema_n. Inside of a schema, two objects cannot have the same name, but an object in one schema can have the same name as an object in another schema. Based on this, if you are accessing an object within its schema, you can simply use its name, since that name would be unique. On the other hand, because of the implied possibility of dealing with objects with similar names in your server, when accessing an object outside of its schema, you must qualify it. To do this, you would type the name of the schema that contains the object you want to use, followed by the period operator, followed by the name of the object you want to use. From our illustration, to access the Something1 object that belongs to Schema1, you would type: Schema1.Something1 There are two types of schemas you can use, those built-in and those you create. When Microsoft SQL Server is installed, it also creates a few schemas. One of the schemas is called sys. The sys schema contains a list of some of the objects that exist in your system. One of these objects is called databases (actually, it's a view). When you create a database, its name is entered in the databases object using the same name you gave it.
Lesson Summary
Exercise: Utility Company
q
Using Microsoft SQL Server Management Studio, create a database named UtilityCompany1 (Accept the default settings)
Exercise: US Regions
file:///C|/Documents%20and%20Settings/michael.musyoki/Desktop/SS/Lesson02.htm (9 of 10)11/27/2008 8:03:15 AM
1. Perform some research on the Internet to find out about different regions of the United States. On a piece of paper, create a list of the regions of the US 2. Connect to the server from the Command Prompt 3. From the Command Prompt, create a database named UnitedStatesRegions1 4. Exit the Command Prompt
Next