SQL Server: Can INFORMATION_SCHEMA Tell Me When a SQL Object Was Created? - sql-server-2000

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

Related

How to drop a table with foreign key constraint in SQL Server

I would like to drop a table with foreign key constraint and re-create the same table (with all the relationships) and with additional data (which I have from “Generate Scripts…”)
How can I do it? I'm using an Azure SQL Server.
If I disable the constraint, truncate the table, insert new data, and re-enable it? Will this work?
Any leads, please? Thanks.
One of the ways is to create a .sqlproj. Inside this, you can create the tables, stored procedures, indexes, data population, and much more. The deployment from .sqlproj will take care of any changes you do to the data store. Please refer to the links below.
URL:
https://learn.microsoft.com/en-us/azure/devops/pipelines/targets/azure-sqldb?view=azure-devops&tabs=yaml
https://learn.microsoft.com/en-us/sql/ssdt/download-sql-server-data-tools-ssdt?view=sql-server-ver15

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.

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

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.