GUID_PK in SQL Server - sql-server

When I execute this query, I am getting an error
Cannot find data type dbo.GUID_PK
For this should I create table for CandidateRoleID or what else should I do here?
But, when I googled it I found it saying SQL Server stores GUID. How could I access it or what is the correct way of declaring this table? I searched in google about GUID_PK.
But didn't find any syntax or explanation. Thanks in advance.
CREATE TABLE [dbo].[tbl]
(
[CandidateRoleID] [dbo].[GUID_PK] NOT NULL,
[CandidateID] [uniqueidentifier] NOT NULL,
[RoleID] [int] NOT NULL
);

The SQL server data type for a GUID is UNIQUEIDENTIFIER.
Your script should be:
CREATE TABLE [dbo].[tbl](
[CandidateRoleID] [uniqueidentifier] NOT NULL,
[CandidateID] [uniqueidentifier] NOT NULL,
[RoleID] [int] NOT NULL,
CONSTRAINT PK_Tbl PRIMARY KEY (CandidateRoleID));
If you want it to be 'automatically' created, similar to an integer identity column, give it a default:
CREATE TABLE [dbo].[tbl](
[CandidateRoleID] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
[CandidateID] [uniqueidentifier] NOT NULL,
[RoleID] [int] NOT NULL,
CONSTRAINT PK_Tbl PRIMARY KEY (CandidateRoleID));

CREATE TABLE [dbo].[tbl](
[CandidateRoleID] [uniqueidentifier] primary key NOT NULL,
[CandidateID] [uniqueidentifier] NOT NULL,
[RoleID] [int] NOT NULL,
);
In SQL server GUID is uniqueidentifier data type.

There is no data type 'GUID_PK' in sql server.
GUID is an acronym for Global Unique ID(entifier) and is a unique 16
byte number. The term GUID and UNIQUEIDENTIFIER are often
interchangeable within the SQL Server community.
If you need to auto-generate uniqueidentifier value in your table during data insert, the consider adding default value to the uniqueidentifier data-type.
CREATE TABLE #tbl(
[CandidateRoleID] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
[CandidateID] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
[RoleID] [int] NOT NULL);
GO
INSERT INTO #tbl (ROLEID) SELECT (1)
CandidateRoleID CandidateID RoleID
F20AE15E-8D97-4042-8AA8-DD7BCB0EB6D6 FAE29358-BD34-4BFE-800B-E332375E020E 1

Related

Index for identity in SQL Server automatic or not

I have a huge table for logging. The definition is:
CREATE TABLE [dbo].[TRACELOG]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[TYPE] [varchar](15) NOT NULL,
[DATEHEURE] [datetime] NOT NULL,
[PROGRAMME] [varchar](25) NOT NULL,
[APPLICATION] [varchar](50) NOT NULL,
[DESCRIPTION] [text] NULL,
[UTILISATEUR] [varchar](10) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Indexes are like this:
The table has now about 18 millions or row. When I run a query using ID = 123456, the query is very long.
SELECT *
FROM TRACELOG
WHERE ID = 123456
I'm very surprised... My question is: in a table with IDENTITY, is there an implicit index created on the column in question (not visible in indexes?) or have I to create manually?
NO - having an IDENTITY column does not automatically create an index.
What does create an automatic (and by default clustered) index is the PRIMARY KEY constraint - which is often used on IDENTITY columns.
But not every IDENTITY column has to be the primary key of its table - you have to specify that if you want it that way.

In SQL Server, can I set a foreign key on a column that does not exist?

I am using SQL Server 2005, and I have 3 tables:
CREATE TABLE [dbo].[Workflow]
(
[WorkflowId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[Description] [varchar](1000) NULL
)
CREATE TABLE [dbo].[Application]
(
[ApplicationId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL
)
CREATE TABLE [dbo].[Rel_Workflow_Application]
(
[WorkflowId] [int] NOT NULL,
[ApplicationId] [int] NOT NULL
)
The rule is that in Rel_Workflow_Application, ApplicationId must exist in the Application table or it can be 0. I don't have a record in table Application where ApplicationId = 0 and I don't wish to create one.
How can I set this constraint?
Though it is possible to use the NOCHECK when creating a foreign key, this is a hack that might prove problematic. I would use null instead of 0 and if 0 needs to show up in queries use coalesce(ApplicationID, 0) in the select statements or create a view which does this and query that view instead.
This cleanly indicates that the foreign key does not have a row in Application.

Row update if row exists. Insert it if row doesn't exist

I'm developing a SQL SERVER 2012 express and developer solution.
I will receive an xml in an stored procedure. In the stored procedure I will parse the xml and insert its data into a table.
My problem here is that in this xml could contain data that exists on the table, and I need to update the data on the table with the new one.
I don't want to check if each row in xml exists on the table.
I think I can use IGNORE_DUP_KEY but I'm not sure.
How can I update or insert new data without checking it?
This is the table where I want to insert (or update) the new data:
CREATE TABLE [dbo].[CODES]
(
[ID_CODE] [bigint] IDENTITY(1,1) NOT NULL,
[CODE_LEVEL] [tinyint] NOT NULL,
[CODE] [nvarchar](20) NOT NULL,
[COMMISIONING_FLAG] [tinyint] NOT NULL,
[IS_TRANSMITTED] [bit] NOT NULL,
[TIMESPAN] [datetime] NULL,
[USERNAME] [nvarchar](50) NULL,
[SOURCE] [nvarchar](50) NULL,
[REASON] [nvarchar](200) NULL
CONSTRAINT [PK_CODES] PRIMARY KEY CLUSTERED
(
[CODE_LEVEL] ASC,
[CODE] ASC
)
)
The "IGNORE_DUP_KEY" parameter ,is ignore inserting new row, if he is already exists, but it is not dealing with update in case it exists.
the solution to your request is by MERGE or DML operation (INSERT/UPDATE/DELETE) .
BTW,
The parameter "IGNORE_DUP_KEY" is covering existsnce for the index key only (index column).

Unsupported Data Type when using SQL Data Sync to Azure

I'd like to sync an on-premise SQL Server 2012 SP2 database to Azure using SQL Data Sync.
When I try to do the sync I get "Unsupported Data Type" error on one of the tables for the ID_Index column:
The Azure Management Portal gives no further explanantion for the error.
The table design in SQL Server Management Studio:
The table creation script:
CREATE TABLE [dbo].[FlightPlanData](
[ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_FlightPlanData_ID] DEFAULT (newid()),
[Airline_ID] [int] NOT NULL,
[FlightID_FK] [uniqueidentifier] NOT NULL,
[FlightPlanID] [int] NOT NULL,
[DateInserted] [datetime] NOT NULL CONSTRAINT [DF_FlightPlanData_DateInserted] DEFAULT (getdate()),
[Type] [varchar](20) NOT NULL CONSTRAINT [DF_FlightPlanData_Type] DEFAULT (''),
[FileName] [varchar](100) NOT NULL CONSTRAINT [DF_FlightPlanData_FileName] DEFAULT (''),
[ClientID_FK] [uniqueidentifier] NULL,
[ID_Index] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_FlightPlanData] PRIMARY KEY NONCLUSTERED ([ID] ASC))
CREATE CLUSTERED INDEX [IX_FlightPlanData] ON [dbo].[FlightPlanData]([ID_Index] ASC)
The table has a GUID primary key, but it's not clustered, instead we use a clustered in index (ID_Index).
I can't remove the ID_Index column, and I'd prefer not to make it the primary key. Is there any way to solve this?
I heard Azure requires a clustered index for each table, but it doesn't have to be the primary key. So what's the problem here?
A table cannot have an identity column that is not the primary key. This is one of the general requirements of SQL Data Sync. For more information, please visit this GitHub documentation.

how to alter table Composite primary key

CREATE TABLE [dbo].[INVS_ITEM_LOCATIONS]
([DEPARTMENT_CODE] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[IM_INV_NO] [numeric](10, 0) NOT NULL,
[LOCATION_CODE] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CURR_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__CURR___1352D76D] DEFAULT ((0)),
[DO_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__DO_QT__1446FBA6] DEFAULT ((0)),
[ALLOC_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__ALLOC__153B1FDF] DEFAULT ((0)),
[YOB_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__YOB_Q__162F4418] DEFAULT ((0)),
[FOC_QTY] [numeric](10, 0) NULL CONSTRAINT [DF__INVS_ITEM__FOC_Q__17236851] DEFAULT ((0)),
[USER_CREATED] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DATE_CREATED] [datetime] NOT NULL,
[USER_MODIFIED] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DATE_MODIFIED] [datetime] NULL,
CONSTRAINT [INVS_ITEM_LOCATIONS_PK]
PRIMARY KEY CLUSTERED ([DEPARTMENT_CODE] ASC,
[IM_INV_NO] ASC, [LOCATION_CODE] ASC)
WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
This is my table structure ......how can I remove the composite primary key in table and also I should add foreign key to im_inv_no reference table is invs_location which contain im_inv_no and the department_code should be same primary key .pls help
To remove your composite primary key, use:
ALTER TABLE dbo.INVS_ITEM_LOCATIONS
DROP CONSTRAINT INVS_ITEM_LOCATIONS_PK
To add a foreign key, use this:
ALTER TABLE dbo.INVS_ITEM_LOCATIONS
ADD CONSTRAINT FK_INV_NO_REFERENCE
FOREIGN KEY(IM_INV_NO, DEPARTMENT_CODE)
REFERENCES dbo.invs_location(IM_INV_NO, DEPARTMENT_CODE)
These are all really basic beginner SQL questions - I would strongly recommend you read one of the various good SQL tutorials out there to get used to SQL first, before posting each and every single little question here....
W3Schools SQL Tutorial
Marc
You can create new tables with modifications you need, copy your data then renaming new talbes to the same names as old tables and deleting old ones. It is probably the most efficient way as well.

Resources