Wednesday, 25 February 2015

Set A Primary Key

The primary key of database table (known in Transact-SQL as a column constraint) is a field that holds unique data to identify a row (or record) in that table. Typically, a table may only have one primary key and no two data elements in a primary key are identical. You can set a primary key on the "ISBN" field of the "Books" table in a SQL Server database in one of two ways.


Instructions


Access the Database Server


1. Locate the SQL Server Management Studio icon by clicking "Start", "All Programs", "Microsoft SQL Server 2005" (or 2008).


2. Select "Database Engine" from the Server Type drop-down menu.


3. Enter the server name or select it from the "Server name" drop-down menu (if it already exists).


4. Select the authentication option from the "Authentication" drop-down menu (either "Windows Authentication" or "SQL Server Authentication"). You are required to enter your user name and password if using "SQL Server Authentication".


5. Click "Connect".


Set a Primary Key Using T-SQL


6. Click "New Query" from the "File" menu or "Standard" toolbar.


7. Type "ALTER TABLE dbo.Books ADD CONSTRAINT" and press "Enter" on your keyboard.


8. Type "PK_Books PRIMARY KEY CLUSTERED (ISBN)" and press "Enter" on your keyboard.


9. Type "WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]".


10. Click the "Execute" button on the SQL toolbar or press "F5" on your keyboard.


Set a Primary Key Using the Table Designer


11. Expand the database containing the "[dbo].[Books]" table in the "Object Browser".


12. Right-click on the table and left-click "Design" in the menu that appears.


13. Right-click on the "ISBN" field and left-click "Set Primary Key" in the menu that appears.


14. Click the "Save" (disk) icon on the toolbar or use the "Ctrl + S" keyboard combination to save the changes.

Tags: drop-down menu, your keyboard, Books table, Enter your, Enter your keyboard, from Server