To Create a Database in SQL

6
1. To create a database in SQL, use the following formula: CREATE DATABASE DatabaseName Here is an example: CREATE DATABASE BethesdaCarRental; If you want the name of the database to be in different words, include them in square brackets. Here is an example: CREATE DATABASE [Bethesda Car Rental]; If you want the name of the database to be in different words, include them in square brackets. To start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Database. Drag Create Database and drop it in the Query window: -- ============================================= -- Create database template -- ============================================= USE master GO -- Drop the database if it already exists IF EXISTS ( SELECT name FROM sys.databases WHERE name = N'<Database_Name, sysname, Database_Name>' ) CREATE DATABASE <Database_Name, sysname, Database_Name> GO 2. To delete a database, you use the DROP DATABASE expression followed by the name of the database. The formula used is: DROP DATABASE DatabaseName; Here is an example: DROP DATABASE RealEstate1; GO To start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Database. Drag the Drop Database node and drop it in the Query window:

Transcript of To Create a Database in SQL

Page 1: To Create a Database in SQL

1. To create a database in SQL, use the following formula:

CREATE DATABASE DatabaseName

Here is an example:

CREATE DATABASE BethesdaCarRental;

If you want the name of the database to be in different words, include them in square brackets. Here is an example:

CREATE DATABASE [Bethesda Car Rental];

If you want the name of the database to be in different words, include them in square brackets.

To start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Database. Drag Create Database and drop it in the Query window:

-- =============================================-- Create database template-- =============================================USE masterGO

-- Drop the database if it already existsIF EXISTS (

SELECT name FROM sys.databases WHERE name = N'<Database_Name, sysname, Database_Name>'

)

CREATE DATABASE <Database_Name, sysname, Database_Name>GO

2. To delete a database, you use the DROP DATABASE expression followed by the name of the database. The formula used is:

DROP DATABASE DatabaseName;

Here is an example: 

DROP DATABASE RealEstate1;GO

To start from a sample code, open an empty Query window and display the Template Explorer. From the Template Explorer, expand Database. Drag the Drop Database node and drop it in the Query window:

-- =========================-- Drop Database Template-- =========================USE masterGO

Page 2: To Create a Database in SQL

IF EXISTS (SELECT name

FROM sys.databases WHERE name = N'<Database_Name, sysname, Database_Name>'

)DROP DATABASE <Database_Name, sysname, Database_Name>GO

3. To change the name of a database, Transact-SQL provides sp_renamedb. The formula used would be:

EXEC sp_renamedb 'ExistingName', 'NewName'

The EXEC sp_renamedb expression is required.

The ExistingName factor is the name of the database that you want to rename.

The NewName factor is the name you want the database to have after renaming it.

Here is an example of renaming a database:

EXEC sp_renamedb 'RentalCars', 'BethesdaCarRentalGO

4. To create a table, you can follow this formula:

CREATE TABLE Country(Column1, Column2, Column3)

or:

CREATE TABLE Country(Column1,Column2,Column3);

Each column is created as:

ColumnName DataType Options

Here is an example:

CREATE TABLE Customers (DrvLicNbr nvarchar(32), DateIssued DATE,DateExpired date,FullName nvarchar(50),Address NVARCHAR(120),City NvarChar(40),State NVarChar(50),PostalCode nvarchar(20),HomePhone nvarchar(20),OrganDonor BIT);

GOTo start from a sample   code , open an empty Query window and display the Template Explorer.

Page 3: To Create a Database in SQL

From the Template Explorer, expand Table. Drag Create Table and drop it in the Query window:

-- =========================================-- Create table template-- =========================================USE <database, sysname, AdventureWorks>GO

IF OBJECT_ID('<schema_name, sysname, dbo>.<table_name, sysname, sample_table>', 'U')

IS NOT NULL DROP TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table>GO

CREATE TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table>( <columns_in_primary_key, , c1> <column1_datatype, , int>

<column1_nullability,, NOT NULL>, <column2_name, sysname, c2> <column2_datatype, , char(10)>

<column2_nullability,, NULL>, <column3_name, sysname, c3> <column3_datatype, , datetime>

<column3_nullability,, NULL>, CONSTRAINT <contraint_name, sysname, PK_sample_table>

PRIMARY KEY (<columns_in_primary_key, , c1>))GO

You can then modify/customize this code.

5. To delete a table using SQL, use the following formula:DROP TABLE TableName

The DROP TABLE expression is required and it is followed by the name of the undesired table. Here is an example:

DROP TABLE Students;GO

6. To rename a table using code, execute thesp_rename stored   procedure  using the following formula:

sp_rename ExistingTableName, TableNewName;

Here is an example:

sp_rename 'StaffMembers', 'Employees';GO

7. The formula to programmatically create a view is:

CREATE VIEW ViewNameASSELECT Statement

Here is an example:

Page 4: To Create a Database in SQL

USE Exercise;GOCREATE SCHEMA Personnel;GOCREATE TABLE Personnel.Employees(EmplNbr nchar(10), FirstName nvarchar(20),

LastName nvarchar(20), Salary money, FullTime bit);GOINSERT INTO Personnel.EmployeesVALUES(N'29730', N'Philippe', N'Addy', 20.05, 1), (N'28084', N'Joan', N'Shepherd', 12.72, 0), (N'79272', N'Joshua', N'Anderson', 18.26, 0), (N'22803', N'Gregory', N'Swanson', 15.95, 0), (N'83084', N'Josephine', N'Anderson', 20.02, 1);GO

CREATE VIEW Personnel.StaffMembersASSELECT FirstName, LastName, SalaryFROM Personnel.Employees;GO

Here is an example that includes a condition:

CREATE VIEW Personnel.GoodSalariesASSELECT FirstName, LastName, SalaryFROM Personnel.EmployeesWHERE Salary >= 16.00;GO

Here is an example of a view that uses two tables:

CREATE VIEW People.ListOfMenASSELECT People.Genders.Gender, People.Persons.FirstName, People.Persons.LastNameFROM People.Genders INNER JOIN People.Persons ON People.Genders.GenderID = People.Persons.GenderID;GO

Here is an example of a view that uses two tables:

CREATE VIEW People.ListOfMenASSELECT People.Genders.Gender, People.Persons.FirstName, People.Persons.LastNameFROM People.Genders INNER JOIN People.Persons ON People.Genders.GenderID = People.Persons.GenderIDWHERE (People.Genders.Gender = N'Male');GO

Here is an example of a view with alias names:

CREATE VIEW dbo.MenAndWomen([First Name], [Last Name], Gender)ASSELECT dbo.Persons.FirstName, dbo.Persons.LastName, dbo.Genders.Gender

Page 5: To Create a Database in SQL

FROM dbo.Genders INNER JOIN dbo.PersonsON dbo.Genders.GenderID = dbo.Persons.GenderID;GO

Views and Functions

To create more complex or advanced views, you can involve functions. As always, probably the easiest functions to use are those built-in. 

If there is no built-in function that performs the operation you want, you can create your own. Here is an example:

CREATE FUNCTION Registration.GetFullName(

@FName nvarchar(20),@LName nvarchar(20)

)RETURNS nvarchar(41)ASBEGIN

RETURN @LName + N', ' + @FName;ENDGO

 Once you have a function you want to use, you can call it in the body of your view as you judge it necessary. Here is an example:

CREATE VIEW Registration.StaffMembersASSELECT Registration.GetFullName(FirstName, LastName) AS [Full Name]FROM Registration.Teachers;GO

SELECT * FROM Registration.StaffMembers;GO

8. The basic formula to programmatically modify a view is:

ALTER VIEW ViewNameASSELECT Statement

Here is an example:

ALTER VIEW dbo.ListOfMenASSELECT dbo.Persons.FirstName, dbo.Persons.LastNameFROM dbo.Genders INNER JOIN dbo.PersonsON dbo.Genders.GenderID = dbo.Persons.GenderIDWHERE (dbo.Genders.Gender = N'Male');

9. The formula to programmatically delete a view is:

DROP VIEW ViewName

Here is an example

Page 6: To Create a Database in SQL

DROP VIEW Personnel.StaffMembers;GO