Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have employee form

where Username, email, phone numbers field.

what I required!


create a script for database schema.it should be possible to run database script multiple time so it should create tables only if they don't exist.

What I have tried:

I do not understand how can I do this.
Posted
Updated 21-Nov-18 3:27am
Comments
Afzaal Ahmad Zeeshan 20-Nov-18 12:17pm    
Are you using Entity Framework, or managing SQL yourself?
F-ES Sitecore 20-Nov-18 12:24pm    
If your database can exist without all tables then it can't be a well designed database as your tables should have references to each other to ensure referential integrity.

SQL Server Management Studio can create database creation scripts for you and you can specify various options. There might be one that checks if the table exists before creating it, but if not you could modify the one it does create.

 
Share this answer
 
Comments
Jaydeep Shah 20-Nov-18 20:42pm    
USE [testDhavalDB] -- Change Your Database name
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Emoployee]') AND type in (N'U')) -- Change Table Name

BEGIN
CREATE TABLE [dbo].[Emoployee](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
CONSTRAINT [PK_Emoployee] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

END
Use this sql script. Table will be created if table is not exist in database.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
           WHERE TABLE_NAME = N'TableName')
BEGIN
  CREATE TABLE tableName(
  ......
  )
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900