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.
Related
In SQL Server Management System, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for SQL Server Management System.
Also How do I see all primary keys of all tables.
You can use following
EXEC sp_fkeys 'yourTableName'
You can select the tablename in the query in management studio and press ALT+F1 then it display all keys and index on that table
Looking to create a SQL query that rebuilds indexes in SQL on only one table within my database. Can anyone point me in the right direction. Someone earlier suggested Ola Hallengren SQL maintenance, but I think that is too robust for what I'm after.
There's a difference between REORGANIZE and REBUILD. See this blog post for details.
If you truly want to REBUILD, and you want to do it for ALL indexes on a given table, then the command would be (straight from the official docs that npe pointed you at):
ALTER INDEX ALL ON mySchema.myTable REBUILD
Try this:
ALTER INDEX indexName ON mySchema.myTable REORGANIZE;
For more instructions see the official docs. The link points to SQL Server 2014 docs, but the syntax should work on 2005 and 2008 as well.
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.
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
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