Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating a Database Microsoft SQL Server. Create Database SQL Management Studio 1.In Object Explorer, connect to an instance of the SQL Server Database.

Similar presentations


Presentation on theme: "Creating a Database Microsoft SQL Server. Create Database SQL Management Studio 1.In Object Explorer, connect to an instance of the SQL Server Database."— Presentation transcript:

1 Creating a Database Microsoft SQL Server

2 Create Database SQL Management Studio 1.In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance. 2.Right-click Databases, and then click New Database. 3.In New Database, enter a database name. 4.To create the database by accepting all default values, click OK; otherwise, continue with the following optional steps. 5.To change the owner name, click (…) to select another owner. 6.To change the default values of the primary data and transaction log files, in the Database files grid, click the appropriate cell and enter the new value. For more information, see Add Data or Log Files to a Database.Add Data or Log Files to a Database 7.To change the collation of the database, select the Options page, and then select a collation from the list. 8.To change the recovery model, select the Options page and select a recovery model from the list. 9.To change database options, select the Options page, and then modify the database options. For a description of each option, see Setting Database Options.Setting Database Options 10.To add a new filegroup, click the Filegroups page. Click Add and then enter the values for the filegroup. 11.To add an extended property to the database, select the Extended Properties page. 1.In the Name column, enter a name for the extended property. 2.In the Value column, enter the extended property text. For example, enter one or more statements that describe the database. 12.To create the database, click OK. Ref: http://msdn.microsoft.com/en-us/library/ms186312.aspxhttp://msdn.microsoft.com/en-us/library/ms186312.aspx

3 Create Database T-SQL USE master ; GO CREATE DATABASE Sales ON ( NAME = Sales_dat, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\saledat.mdf', SIZE = 10, MAXSIZE = 50, FILEGROWTH = 5 ) LOG ON ( NAME = Sales_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\salelog.ldf', SIZE = 5MB, MAXSIZE = 25MB, FILEGROWTH = 5MB ) ; GO Ref: http://msdn.microsoft.com/en-us/library/ms186312.aspxhttp://msdn.microsoft.com/en-us/library/ms186312.aspx

4 Create Table SQL Management Studio 1.Right-click the database in which you want the table to appear. A menu is shown. Select "New Table" from the list of menu items.database 2.Type column names on the left side of the table viewer. The table view is the place where you add your column names and data types. 3.Select a data type for each column. The data type is a drop-down menu item 4.Right-click one of the columns that is unique to the table and select "Primary Key." 5.Click the "Save" button on the SQL Server Management Studio toolbar. A window pops up asking for a table name. Enter it into the text box and press the "Ok" button. The table is created and saved in your database.SQL Ref: http://www.ehow.com/how_5877415_create-tables-sql-management-studio.htmlhttp://www.ehow.com/how_5877415_create-tables-sql-management-studio.html

5 Create Table T-SQL CREATE TABLE Names (FirstName VARCHAR (20), LastName VARCHAR (20) NOT NULL, DateOfBirth DATETIME, CONSTRAINT MultiConstraint UNIQUE(FirstName, LastName, DateOfBirth)); Ref: http://devguru.com/technologies/t-sql/7121.asphttp://devguru.com/technologies/t-sql/7121.asp

6 Create Tables Exercise 1.Create a database named demo1 2.Use the SQL statements on the following slides to create and populate the Books, Authors, and AuthorBook tables. 3.Provide any corrections required for T-SQL syntax.

7 Create Books Table CREATE TABLE Books ( BookID SMALLINT NOT NULL PRIMARY KEY, BookTitle VARCHAR(60) NOT NULL, Copyright YEAR NOT NULL );

8 Insert data into Books INSERT INTO Books VALUES (12786, 'Letters to a Young Poet', 1934), (13331, 'Winesburg, Ohio', 1919), (14356, 'Hell\'s Angels', 1966), (15729, 'Black Elk Speaks', 1932), (16284, 'Noncomformity', 1996), (17695, 'A Confederacy of Dunces', 1980), (19264, 'Postcards', 1992), (19354, 'The Shipping News', 1993);

9 Create Authors Table CREATE TABLE Authors ( AuthID SMALLINT NOT NULL PRIMARY KEY, AuthFN VARCHAR(20), AuthMN VARCHAR(20), AuthLN VARCHAR(20) );

10 Insert data into Authors INSERT INTO Authors VALUES (1006, 'Hunter', 'S.', 'Thompson'), (1007, 'Joyce', 'Carol', 'Oates'), (1008, 'Black', NULL, 'Elk'), (1009, 'Rainer', 'Maria', 'Rilke'), (1010, 'John', 'Kennedy', 'Toole'), (1011, 'John', 'G.', 'Neihardt'), (1012, 'Annie', NULL, 'Proulx'), (1013, 'Alan', NULL, 'Watts'), (1014, 'Nelson', NULL, 'Algren');

11 Create AuthorBook Table CREATE TABLE AuthorBook ( AuthID SMALLINT NOT NULL, BookID SMALLINT NOT NULL, PRIMARY KEY (AuthID, BookID), FOREIGN KEY (AuthID) REFERENCES Authors (AuthID), FOREIGN KEY (BookID) REFERENCES Books (BookID) );

12 Insert Data into AuthorBook INSERT INTO AuthorBook VALUES (1006, 14356), (1008, 15729), (1009, 12786), (1010, 17695), (1011, 15729), (1012, 19264), (1012, 19354), (1014, 16284);

13 Basic Join SELECT BookTitle, Copyright, Authors.AuthID FROM Books, AuthorBook, Authors WHERE Books.BookID=AuthorBook.BookID AND AuthorBook.AuthID=Authors.AuthID ORDER BY Books.BookTitle;

14 Basic Join SELECT BookTitle, Copyright, Authors.AuthID FROM Books, AuthorBook, Authors ORDER BY BookTitle; What happens when we leave off the WHERE clause?

15 Basic Join SELECT BookTitle, Copyright, AuthID FROM Books AS b, AuthorBook AS ab WHERE b.BookID=ab.BookID ORDER BY BookTitle;


Download ppt "Creating a Database Microsoft SQL Server. Create Database SQL Management Studio 1.In Object Explorer, connect to an instance of the SQL Server Database."

Similar presentations


Ads by Google