Mandatory primary keys in Sql Azure - sql

I have been told that in SQL Azure that primary keys are mandatory on each table. However, I've been unable to find anything to back this up.
There are a number of references to mandatory clustered indexes, for example...
http://blogs.msdn.com/b/sqlazure/archive/2010/05/12/10011257.aspx
...but this article is from 2010, so I'm not sure if this is even relevant any more.
Can anyone tell me if primary keys are mandatory in each table in Sql Azure databases?

With Azure SQL Database V12 it is not mandatory anymore to have a clustered index.
See also https://www.itprotoday.com/sql-server/indexes-azure-sql-database-v12.

A primary key is not required. Clustered index - yes.

Related

Eliminate Gaps in Primary Key Field in MS Access

I'm in the process of building a database in MS Access (SQL Server Compatible Syntax enabled) and I'm having a question regarding my primary keys. Specifically, I would like to find a way to eliminate the gaps in primary keys so that the keys will be continuous, even after a record has been deleted. I don't think that this should create referential integrity issues, as foreign keys are set to ON UPDATE CASCADE.
Note: I'm writing the table creation statements in SQL, so I can code the solution in a SQL script and run it.
Is there a built in function that can accomplish this, or is there a trigger/stored procedure that I should create?
Thanks for the help.
Don't do this. A auto-number/identity primary key cannot be reused. There is no performance loss by having gaps so no harm done. Just leave it as is no problems. This will create a massive update and reorganization of your database. The gaps are normal for any database engine where a insert failed for example. Oracle, MySQl and SQL Server all do this.

Error when trying to publish database to Azure without clustered indexes

I'm getting the following error when trying to publish my database to my Azure site:
17:13:29: Could not publish the site. Unable to publish the database. For more information, see "http://go.microsoft.com/fwlink/?LinkId=205387"
17:13:29: Error detail:
17:13:29: An error occurred during execution of the database script. The error occurred between the following lines of the script: "332" and "334". The verbose log might have more information about the error. The command started with the following:
17:13:29: "INSERT [dbo].[DBStatus] ([LastIndex], [LastUpdated"
17:13:29: Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again. http://go.microsoft.com/fwlink/?LinkId=178587
17:13:29: Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_SQL_EXECUTION_FAILURE.
This link explains the error: http://blogs.msdn.com/b/sqlazure/archive/2010/04/29/10004618.aspx
Is there an easy way to convert my database to have clustered indexes or should I just choose a host with SQL Server 2012 hosting?
I wouldn't change your hosting option simply due to this. Add the clustered indexes.
There is a tool called SQL Database Migration Wizard which can analyze a SQL database and make a migration script and even move the data. One of the things it will do is suggest a clustered index on tables that don't have them. However, like any tool, make sure you look at what it is suggesting and see if it makes sense in your scenario.
My suggestion is to look at the tables that do not have a clustered index and determine a reasonable index to create. A tool like the one above can make suggestions, but they are just suggestions and may not be exactly what your table would benefit from.
The requirement for clustered indexes for Azure SQL Database comes from the fact that they replicate the data in triplicate and the clustered indexes makes that a faster process.
I've done these steps:
1- Get a list of Tables without Primary Key by running this query:
USE DatabaseName;
GO
SELECT SCHEMA_NAME(schema_id) AS SchemaName,name AS TableName
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
ORDER BY SchemaName, TableName;
GO`
2- Create Alert table scripts for all tables, add them a Identity field and make it Primary by scripts like this:
ALTER TABLE [dbo].[tableName] ADD identityFieldName BIGINT IDENTITY;
ALTER TABLE [dbo].[tableName] ADD CONSTRAINT PK_Table_identityFieldName_1 PRIMARY KEY CLUSTERED (identityFieldName);
Repeat above query for all tables that does not have Primary Key

SQL how to check database data validity?

Is there a simple way to run a check on the databases after turning constrains back on, to check things like:
foreign keys still exist in their primary tables,
primary key are unique,
etc...
I am working with MS-SQL Server 2005.
Mitch Wheat linked to the correct answer.
DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS

Copy database error (DNN): No primary or candidate keys in the referenced table that match the referencing column list in the foreign key

DNN version: 5.6.2
Tool: SQL Server Management Studio
Action: Copy database
Error: There are no primary or candidate keys in the referenced table
'Roles' that match the referencing column list in the foreign key
'FK_UserRoles_Roles'
I tried searching for this error online, but all searches lead to topics where this error is encountered while creating a foreign key to a non-unique column. In my case, the key is already created. I double-checked that the RoleId column is the primary key on the Roles table and it is indeed the column the foreign key 'FK_UserRoles_Roles' references.
Any help to fix this is appreciated. Alternatively, what will be a quick way to copy the structure and data of my database apart from the "Copy Database" option?
Thanks!
There is also an aspnet_Roles table in the DotNetNuke database. This is part of the ASP.NET authentication provider. Your problem may be not migrating the data in both the aspnet_ tables as well as the DNN tables.

SQL server command help and tutorials for specific commands?

I saw the command in my book project (book - teach yourself SQL in 10 mins, 2004):
ALTER TABLE Customers WITH NOCHECK
ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (cust_id);
Can you tell me what these commands mean (or give links with simple tutorials for these commands) :
WITH NOCHECK
CLUSTERED
Are there any alternatives to the above commands? Can I remove them ?
I am using the free edition of SQL Server 2008 R2 with latest updates.
You may download or use online book from MSDN.
Microsoft SQL Server 2008 Books Online
SQL Server Books Online
WITH NOCHECK will tell SQL Server not to validate the particular constraint. CLUSTERED tells SQL Server to create a clustered index with the key cust_id. That will turn it from a heap to a clustered index.
There are plenty resources online. You can start here: It should have most of the basics covered. Start form the basics, such as create database, create tables, select data from tables, etc. More advanced topics such as clustered indexes and With Non-check option will make it more confusing for you.