SQL how to check database data validity? - sql

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

Related

How do I see all foreign keys and primary keys in SQL Server Management System

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

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.

Mandatory primary keys in Sql Azure

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.

Discover primary / unique keys in Sybase ASE

In Sybase ASE, I would like to discover all primary and unique keys. I want to do something similar to what is explained in this answer:
Identifying Sybase tables, fields, keys, constraints
But unfortunately, this doesn't work for me. Somehow the syskeys table does return any rows for my own keys, only for system table keys.
What may I be doing wrong? Some missing grants? I have installed Sybase ASE 15.5 and I'm connecting with user dbo, login sa
When you explicitly declare a key field - say in a CREATE TABLE statement - this doesn't populate the syskeys table. You would use (e.g.) sp_primarykeys to do that. This allows you to 'register' in the syskeys tables the primary, foreign keys, etc. that you would like to be discoverable by an application. You can declare a primary key in this way, even if there is no actual constraint enforced on the field in question.
The stored procedure sp_helpconstraint can be used to find all the keys (etc.) that are defined for a table.
To query for all tables in a database you'd need to use the sysindexes table and look at, e.g. the status field.

SQL Server: Can INFORMATION_SCHEMA Tell Me When a SQL Object Was Created?

Given:
I added a non-nullable foreign key to a table.
I settled on a way to populate the foreign key with default values.
I checked in both changes to a re-runnable DB creation script.
Other developers ran this script, and now they have the foreign key populated with default values on their developer machines.
A few days later however...
I changed my mind.
Now I'd like to populate the foreign key's default values differently.
My Question:
Can SQL Server or INFORMATION_SCHEMA
tell me when SQL objects were created?
Ideally, I'd like to drop and re-add the foreign key if it was created before a certain date/time.
Any help or alternative strategies would be greatly appreciated.
Obviously, I'd like to avoid going to each developer's cube, asking them to drop the foreign key manually.
For SQL 2000 sysobjects (for SQL 2000), column crdate
For SQL 2005+ sys.objects, create_date and modify_date