In order to access something (such as a computer or a database), the user must be authenticated. The authentication is done using a username and a password. In the absence of this, a user would receive an error:
The user can also take advantage of the group he or she belongs to. For this reason, the ability to be authenticated in order to access the database or resource is called a login. Before creating a login, the person for whom you want to create the account must have a user account on the network. In other words, the person must have an account that allows him or her to establish a connection to the domain. You can create a login either visually or with code. To visually create a login in Microsoft SQL Server, in the Object Explorer, expand the Security node. Right-click Login and click New Login...
This would display the Login - New dialog box. The formula to programmatically create a login is: CREATE LOGIN loginName { WITH <option_list1> | FROM <sources> } <option_list1> ::= PASSWORD = { 'password' | hashed_password HASHED } [ MUST_CHANGE ] [ , <option_list2> [ ,... ] ] <option_list2> ::= SID = sid | DEFAULT_DATABASE =database | DEFAULT_LANGUAGE =language | CHECK_EXPIRATION = { ON | OFF} | CHECK_POLICY = { ON | OFF} | CREDENTIAL =credential_name <sources> ::= WINDOWS [ WITH <windows_options>[ ,... ] ] | CERTIFICATE certname | ASYMMETRIC KEY asym_key_name<windows_options> ::= DEFAULT_DATABASE =database | DEFAULT_LANGUAGE =language To let Microsoft SQL Server generate skeleton code for you, on the Standard toolbar, click New Query to get a text editor. In the Template Explorer, expand the Login node, drag Create SQL Login Must Change Password and drop it in the empty text editor: -- ================================================= -- Create SQL Login Must Change Password template -- ================================================= CREATE LOGIN <SQL_login_name, sysname, login_name> WITH PASSWORD = N'<password, sysname, Change_Password>' MUST_CHANGE, CHECK_POLICY = <check_policy,ON or OFF, ON>; GO You start with the CREATE LOGIN expression, followed by the user name. After creating a login, a user can use it to connect to the server. There are many options, as we will see in the next few sections. After launching Microsoft SQL Server, in the Connect to Server dialog box, the user must select the type of server. This is done in the Server Type combo box:
In most cases, and for our lessons, we will use the Database Engine option. If the SQL Server Management Studio is already opened, to log in, the user can click Connect -> Database Engine...
The user must specify what computer has Microsoft SQL Server. If working from the Connect to Server dialog box, the user can select a computer in the Server Name. If the user wants to connect to another computer on the network, that is, a computer other than the one selected, he can type the computer name or select it in the Server Name combo box. Another option is to click the arrow of the Server Name combo box and select <Browse for more...>:
In this case, intermediate steps allow the user to select the computer. After selecting the computer, in the Authentication combo box, the user must specify the type of authentication to use. There are various choices. If the user is working from PowerShell or from the Windows command prompt, to connect to a local computer, he can type SQLCMD -S followed by the name of the computer and press Enter. You can include space between -S and the name of the server. Here is an example: You can also omit the space between -S and the name of the server. Here is an example:
To use Microsoft SQL Server, the user must provide a user name and a password. In reality, Microsoft SQL Server 2008 requires every user to log in but it provides various options. If the user already has a login, when he starts the computer an logs in to the operating system, when he launches Microsoft SQL Server, if the application recognizes his login account, he can log in without providing a user name and a password. If you are visually creating a login and if you want the Microsoft Windows operating system to take care of logging, in which case the user will be able to connect to Microsoft SQL Server without providing a user name and a password, click the Windows Authentication radio button (it is the default and should be selected already:
If you are creating the login with code, add the FROM WINDOWS flag: CREATE LOGIN See Below
FROM WINDOWS
When Windows Authentication is used or if you add the FROM WINDOWS flag, when the Connect to Server dialog box comes up, the user can select Windows authentication and click Connect. Instead of connecting as the user who is currently logged, the user may want to use a different account. In this case, in the Authentication combo box, the user can select SQL Server Authentication:
If working from the Connect to Server dialog box, if the user selects SQL Server Authentication, he would have to provide a login name. Of course, the login must have been created. If you are visually creating a login, in the Login Name text box, type the name of the domain, followed by \, and followed by the user name as it exists in the domain. Here is an example:
Of course, the user's account must have been created already in the server (domain). If you provide a user name that cannot be found in the system, when trying to finalize, you would receive an error:
If you don't remember the user account, or to help you find the user name, click the Search button. This would open the Select User or Group dialog box. In the bottom text box, type the user name of the account and click Check Names. You can also search for more than one name at the same time. To do this, type each user name and separate them with semicolons. Then click Check Names. Once the name(s) has (have) been found, click OK. If you are creating the login using code, after CREATE LOGIN, type the DomainName\Username combination between [ and ]. Here is an example: USE master; GO CREATE LOGIN [functionx\mwatts] FROM WINDOWS; GO When the user decides to log in, after selecting SQL Server Authentication in the Authentication combo box, the user would type the same login name combination in the Login combo box. If the user had previously successfully connected with that account, it would be listed in the combo box where he can simply select it. If the user is connecting from PowerShell or from the command prompt, he must use -U followed by the login name and press Enter. Here is an example:
If the user decides to use SQL Server Authentication, after specifying a login name, he must also provide a password, which must be associated with the login. If you are visually creating the login, to provide a password, first click the SQL Server Authentication radio button. Then click the Password text box and type the desired password. Click the Confirm Password text box and type the same password. If you are programmatically creating the login, to specify the password, after the login name, type WITH PASSWORD = and provide the password in single-quotes. Here is an example: USE master; GO CREATE LOGIN [functionx\mwatts] WITH PASSWORD = N'Password1'; GO
When creating a login, the password you provide doesn't have to be the same used in the domain. In fact, the first time the user logs in, you can make him change the default password you specified when you created his account. To set this option, if you are visually creating the login, click the User Must Change Password At Next Login check box (this is the default if you click the SQL Server Authentication radio button):
If you are programmatically creating the login, to set this option, add a flag as MUST_CHANGE. After visually creating the login, click OK. If you are creating it with code in a Query window, execute it. Here is an example of creating a login with various options: CREATE LOGIN [FUNCTIONX\pmukoko] WITH PASSWORD = N'Password1' MUST_CHANGE, CHECK_POLICY = ON, CHECK_EXPIRATION = ON; GO Remember that when Microsoft SQL Server is installed, it creates a default account named sa. You can also use it as a login and provide its password:
Microsoft SQL Server allows a user to connect with many login account. After connecting, to make another connection, the user can click Connect -> Database Engine, and provide the necessary pieces of information as we had seen in this section. In the same way, you can create as many connections as possible, using valid login accounts.
The Object Explorer displays a list of items as a tree. One of the most regularly used items will be the name of the server you are using. If you are just starting to learn database development or you are a junior database developer, you may use or see only one server. In some cases, you may be dealing with many servers. Regardless, you should always know what server you are currently connecting to. This is easy to check with the first node of the Object Explorer. In the following example, the server is named Central: If you are connected to more than one server, each is represented by its own node:
The name of the server is followed by parentheses. In the previous section, we saw that, to establish a connection to a server, you must authenticate yourself. In some cases you may use the same account over and over again. In some other cases you may have different accounts that you use for different scenarios, such as one account for database development, one account for database management, and/or one account for database testing. When many connections have been made, each connection is represented in the Object Explorer by its own node and each connection has its own objects (sub-nodes):
To close a connection, you can right-click it and click Disconnect:
Some operations cannot be performed by some accounts. When performing some operations, you should always know what account you are using. You can check this in the parentheses of the server name. In the following connection, an account named Administrator is currently logged in to a server named Central:
We saw that, by default, the right area of Microsoft SQL Server Management Studio displays an empty gray window. When you select something in the Object Explorer, you can use that right area to display more detailed information about the selected item. To do this, on the main menu, click View -> Object Explorer Details. The main area on the right side would then be filled with information: Probably the most regular node you will be interested in, is labeled Databases. This node holds the names of databases on the server you are connected to. Also, from that node, you can perform almost any necessary operation of a database. To see most of the regularly available actions, you can expand the Databases node and some of its children. You can then right-click either Databases or one of its child nodes. For example, to start PowerShell, you can right-click the Databases node or the server name and click PowerShell: When the PowerShell comes up, what it displays depends on what you had right-clicked.
|
|
|||||||||||||||||||||||||||||||||||||||||||
|