Table Constraints Question
Feb 22, 2008Does anybody know how to query for the table constraints, indexes etc..
I believe there are sys tables that contain these but I'm not familiar with them any help would be appreciated.
Does anybody know how to query for the table constraints, indexes etc..
I believe there are sys tables that contain these but I'm not familiar with them any help would be appreciated.
I know this is probably a flick of a switch but I cannot figure out which switch. Setup is SQL Server / Stored Procedures / DAL / BLL(skipped for testing) / PL. The stored procedure queries from only one table and two columns are ignored because they are being phased out. I can run the stored procedure and preview the data in the DAL but when I create a page with an ODS linked to the DAL and a GridView I get this error. I checked every column that does not allow nulls and they all have values. I checked unique columns (ID is the only unique and is Identity=Yes in the table definition). I checked foreign-key columns for values that are not in the foreign table and there are none. Any ideas why do I get this?
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Hi,
I am getting the above error when trying to load a report into my Web Application, I have tracked the error down to one specific field in my database. Even though this field is a NVarChar field and is of size 30 it would seem that there is an issue returning the value from the field. I can write it into the database no problems but when I try to get it out of the database it returns the above error.
e.g
MOB 401.908.804 - Fails
0401.907.324 - okay
8239 9082 (pager) - fails
Anyone got an idea on how to fix this????
Regards..
Peter.
Can anyone recommend a good article/book on the subject? I am trying to find out more about compound primary keys. How many is too many? Does the number of primary key columns affect your performance? If so, how does it affect it? Should some of the constraints be handled from stored procedures?
Any response is greatly appreciated.
:cool:
Hi,
I am new to MsSql. I want to know is there any command through which we can get all the consraints on a table or in a databases. In the same way how to get the all table names in a database from the coomand prompt or thorugh query analyzer.
Thanks
Hi all
Is there anyway you can deactivate/enable all constraints on a particular table so it can be done in a script?
Thanks.
This is an SQL Server 2000 question. Without having access to the "Design Table" option in the pop-up menu of a table, is there another way to see what constraints, indexes, primary key, etc. that a table has?
Thank you.
I'm trying to find a Stored procedure that will perform a complete table copy from the source to the target.
Anyone have one?
Hi,
I am DB programmer, How can i get Table Constraints to XML in SQL-Server 2000 with out data.
I remember once using a command in Query Analyser that gave me all theinformation about a table. It showed details about the columns,constraints, keys, indexes etc.I can't remember how I did this? Can anyone help me?It could of been a stored proc that I called or a query on the systemtables.I do know that it was very simple to do and it was executed in QueryAnalyser.Can anyone help me?
View 2 Replies View RelatedJust started learning SQL recently.But one thing i'm still not clear on is about altering relationships betweentables after they've been created.Instead of creating a foreign key when the table is first created - i createthe table and then run a query to set the foreign key and relationship(one-to-one, one-to-many etc)Anyways, long story short is i want to create a one-to-one relationship witha table but am having problems with adding more than one constraint at atime when altering a table.Understand yet? Easiest thing to do is show you:I have 2 tables: Branch_Table and Employee_TableI want to create a one-to-one relationship between emp_id on theBranch_Table and manager_id on the Employee_Table.The SQL i've written which doesn't seem to work is:ALTER TABLE Branch_TableAdd Constraint Branch_Table_FK1 FOREIGN KEY (manager_id)Add Constraint Branch_Table_UQ1 Unique (manager_id)References Employee_Table (emp_id));Am having trouble with that second Add constraint (UQ1 unique). I know it'ssomething to do with the Add syntax above.So basically, my question is can i create a one-to-one relationship withjust the one SQL Query? And how would i do it?Many thanks in advance.
View 6 Replies View Related
Is it possible to define a constraint for Primary Key on more than 1 column or an alternate index on a column in a return table from an inline table valed function?
Example Header:
alter FUNCTION [dbo].[fntMetaFrame] (@ii_CompanyID int)
RETURNS @tbl_MetaFrame TABLE ( pk_Key int Identity(1,1) primary key,
ObjectID int ,
Seq int null )
I want the primary key to be pk_Key, ObjectID
OR
I want to add another index on ObjectID.
I need to script out data in several tables (30+) and then reload those tables on a different database. The "target" database table data will get overwritten each time, not appended to. Several of these tables have numerous foreign keys and other constraints that must be dealt with. None of them have more than 100 records or so. Then I need to keep this script in source control and reference it as part of a post-deployment step with a Visual Studio DB project.
I have redgate, dbghost and visual studio. Are there any tools will generate the script for me complete with dropping all the constraints, truncating the tables, then re-adding the constraints back? Or, am I looking at simply scripting the table data out, then modifying the script myself to add in the steps to drop/readd the constraints? Further, since some of these tables might have relationships, not just to other tables, but to those in question, there will likely be an order in which the tables need to get loaded.
The approach I am thinking will be needed, since ive done this sort of thing before except with SSIS, is to run something like dbghost or the "generate scripts" option in SSMS as a starting point. From there, change the order around as needed, etc.
I have removed all constraints of a table.I have a copy of the database as back up, now how can i add back the constraints to the removed table.
View 6 Replies View Related
I am testing Insert, Update etc. and messing up my database with "dirty" data. Is there a way to temporarily drop FOREIGN KEY constraints to truncate a table after testing without dropping the whole table and rebuilding it?
Newbie
Hey there,
Trying to build a temporary table based on a parent table such that:select * into #staging from tbl_parent where 1=0
The temp table (#staging) picks up any NOT NULL constraints from the parent (tbl_parent) table and frankly, it doesn't meet my needs.I also tried to build a view of the parent (tbl_parent) table from which to base my SELECT INTO and still, the constraints followed.
I'm thinking perhaps it possble to iterate though the temp table using syscolumns sysobjects and and set the column NULL properties on the temp table AFTER its been created but in this approach, I'm working to avoid directly referencing the columns by name.
Is this possible? A better answer perhaps?
Purpose of exercie is to populate a record in memory before it hits the table.I'll be constructing the record on the fly so that at first, I won't have all the fields to de-Null the columns.Also, I don't wish to lose the fact that some of my fields are Null - once I've fully populated my temp record, I'll insert into my 'real' table and will depend on constraints to throw the appropriate error. In this I won't have to built custom error checking in the stored procedure itself but instead depend on SQL SERVERS built capacity to the throw the error. This opens a bunch of possibilities for extensibility since if at a later date a constraint rule changes, one need only change the parent table definition rather than cracking open the stored procedure.
Thank you for reading and a big thanks to you for replying :)
Also,
Hi,
How can I copy a database table with all its data, indexes and constraints to a new table in the same database in sql server 2005
I'm working on creating a new version of an existing table. I want to keep the old table around, only with a different name. In looking this up I found there's a system routine named sp_rename, which looks like it will work fine.However in thinking about this I realized that the foreign key constraints defined on the table (there's 3 of them) are likely to not be renamed, correct?
If I'm correct, then I suppose I could rename the table, then drop the 3 foreign key constraints, and create them new using different names for those foreign key constraints.
How to copy a table from one server to another server with primary and foreign key constraints.
View 5 Replies View RelatedHow to delete records from multiple tables if main table’s entity is deleted as constraints is applied on all..There is this main table called Organization or TblOrganization.and this organization have branches which are in Brach table called tblBranch and this branch have multiple applications let say tblApplication and these application are used by multiple users called tblUsers.What I want is: when I delete the Organization All branches, application and users related to it must be deleted also.How I can apply that on a button click in asp.net web forms..Right now this is my delete function which is very simple
Public void Delete(int? id){
var str=”DELETE FROM tblOrganization WHERE organizationId=”+ id ;
}
And My tables LOOK LIKE this
CREATE TABLE tblOrganization
(
OrganizationId int,
OrganizationName varchar(255)
[code]...
I am researching the possible use of Peer-to-Peer Transactional Replication with SQL 2005 to improve the availability and scalability of the SQL-farm.
View 5 Replies View RelatedI'm using MS SQL 2000 with enterprise manager I have a customers table called high_customer, it has a primary key called IDI have an invoices table called high_invoices, it has no primary keys but it does have a column for customer ID.I want to set it so if a customer is deleted from the customers table, any invoices with that customerID are deleted from the invoices table.Do I need to specify a constraint? If so how do I do it in enterprise manager.Thanks
View 1 Replies View RelatedHi,
I have the following problem:
I have a table called Jobs with the fields:
JobNumber, Name, Customer, ...
And a table called Customers with the fields:
ID, Name, Address, ...
Obviously Jobs is linked to Customers with the Customer<->ID fields. I want to set it up so that if a Customer is deleted, then any Jobs that had that customer listed now have the Customer field set to NULL. Can I do this with a constraint, or will I need to use a trigger?
Cheers,Little'un
Hello,
I am not a DBA and I need to do the following.
I have 2 tables A and C and both have a common fileds say emp.Emp is pkey in C .
There can be a record in C but not in A.If a record is inserted in A then it checks for the same employee info in C .If not found it do not allow to insert a record in A.
How to go for it?
Thanks!
Hiya peops!!
Listen i was wondering if anyone could tell me whether they know the correct syntax for creating constraints or where i could find it on the net???
Thanks
Hiya peops
im creating a constraint using enterprise manager for 1 of my tables n was wondering how u constructively create a constraint expression, there is a specific style,right??
Thanks
hi there,
i have added my tables and defined foreig keys and primary keys for my table, but when i try to put the data it gives me some error reagards to parent key..and it wont let me drop the table... can anybody tell me how to drop the foregin key..i have tried (
ALTER TABLE module DROP CONSTRAINT ATTENDANCEID_FK;)
this formula and it wont work,
heres my tables and relationships:
CREATE TABLE ATTENDANCE
(ATTENDANCEID char(4) NOT NULL,
StudID char(4) NOT NULL,
ModuleID char(4) NOT NULL,
Week1 VARCHAR(10),
Week2 VARCHAR(10),
Week3 VARCHAR(10),
Week4 VARCHAR(10),
Week5 VARCHAR(10),
Week6 VARCHAR(10),
PRIMARY KEY (ATTENDANCEID));
ALTER TABLE ATTENDANCE
ADD FOREIGN KEY (studID) REFERENCES student(studID);
ALTER TABLE ATTENDANCE
FOREIGN KEY (moduleID) REFERENCES module(moduleID));
insert into ATTENDANCE values
('F1','A1','E1','Yes','Yes','Yes','Yes','Yes','Yes');
insert into ATTENDANCE values
('F2','A2','E2','Yes','No','Yes','Yes','No','Yes');
insert into ATTENDANCE values
('F3','A3','E2','Yes','Yes','Yes','No','Yes','Yes');
insert into ATTENDANCE values
('F4','A4','E2','Yes','Yes','Yes','Yes','No','Yes');
insert into ATTENDANCE values
('F5','A5','E1''Yes','Yes','No','Yes','Yes','Yes');
insert into ATTENDANCE values
('F6','A6','E2','Yes','Yes','Yes','Yes','Yes','No');
insert into ATTENDANCE values
('F7','A7','E1','Yes','Yes','Yes','Yes','No','No');
insert into ATTENDANCE values
('F8','A8','E2','Yes','Yes','Yes','No','Yes','Yes');
insert into ATTENDANCE values
('F9','A9','E3','Yes','Yes','No','Yes','Yes','Yes');
CREATE TABLE MODULE
(ModuleID char(4) NOT NULL,
ATTENDANCEID char(4) NOT NULL,
ModuleName varchar(45),
ModuleCode varchar(6),
PRIMARY KEY (ModuleID));
ALTER TABLE MODULE
ADD FOREIGN KEY (attendanceID) REFERENCES attendance(attendanceID);
insert into MODULE values
('E1','F1','Workshop Research and Project Implementation','Im3023');
insert into MODULE values
('E2','F2','Advanced Database Development','SD3042');
insert into MODULE values
('E3','F3','Advacned Information Systems Develpment','SD3043');
charles
I am fairly new at SQL and also new to this forum. So I don't know if this topic belongs here or not.
But anyways, I found this tutorial online. However, I am having trouble completing it. Can someone please help me?
Given the following table definitions:
CREATE TABLE USERS
(
USERID CHAR(5) PRIMARY KEY,
NAME VARCHAR(30),
RATING CHAR(1) CHECK (RATING IN (A, B,C,D)
)
CREATE TABLE ITEMS
(
ITEM_NO NUMBER PRIMARY KEY,
DESCRIPTION VARCHAR(50),
OFFERED_BY CHAR(5) REFERENCES USERS(USERID) ON UPDATE CASCADE ON DELETE CASCADE,
START_DATE DATETIME,
END_DATE DATETIME,
RESERVE_PRICE NUMBER
)
CREATE TABLE BIDS
(
USERID CHAR(5) REFERENCES USERS ON UPDATE CASCADE ON DELETE SET NULL,
ITEMNO NUMBER REFERENCES ITEMS(ITEM_NO) ON UPDATE CASCADE ON DELETE CASCADE,
BID NUMBER,
BID_DATE DATETIME,
PRIMARY KEY(USERID,ITEMNO,BID_DATE)
)
Directions: Indicate whether the statement is accepted or rejected; and if accepted indicate its effects. Evaluate each statement INDEPENDENTLY.
1) INSERT INTO ITEMS VALUES(1009, Red Bicycle,U01,1999-01-08, 1999-02-10, 30)
2) UPDATE ITEMS SET ITEMNO=1008 where ITEMNO=1001
3) DELETE FROM ITEMS WHERE ITEMNO=1005
4) INSERT INTO ITEMS VALUES(1010, Green Bicycle,U07,1999-01-08, 1999-02-10, 30)
5) UPDATE BIDS SET ITEMNO=1011 WHERE ITEMNO = 1001
6) DELETE FROM USERS WHERE USERID=U01
7) UPDATE ITEMS SET ITEMNO=1020 WHERE ITEMNO=1001
8) DELETE FROM BIDS WHERE BID > 1001
Thanks. I would greatly appreciate your help.
My table structure is something like this :
ORDER_ITEM (OrderSK, ItemNumber, Qty, ServiceSK, UnitPrice)
SERVICE (ServiceSK, Description, UnitPrice)
How could I modify the database structure to enforce the data constraint that ORDER_ITEM.UnitPrice must be equal to SERVICE.UnitPrice for the corresponding service.
i have a question about constraints in SQL server.
i have three tables:
Client
---------
clientID
name
etc...
Bank
-------
BankID (a virtual number, identity)
ClientID (FK: Client)
BankAccountNumber
BankName
Order
--------
OrderID
ClientID
BankID
a Client an have one or more bankAccounts.. if he place an order, he can select wich bankaccount will be used to pay.
how do i have to set the constraints so that the bankAccount in Order, is allways of THAT client in order.???
Thanx...
I have to change someones username in my database in a couple of tables. When I try to make any changes, I get an error that there is a constraint. Is there a way to make the changes without removing all the constraints then replacing them?
Thanks
Hi,
Just getting into scripting sql. I have a little script that adds and drops a default value for a column. But I cannot get it to run when the column already has a default value specified. If I didn't script the contraint and specify a name I have no name to specify when trying to drop it.
ALTER TABLE users ADD CONSTRAINT dev1 DEFAULT 1 FOR ns_email_flag
ALTER TABLE users DROP CONSTRAINT dev1
How do I remove any existing default values using script?
TIA,
Seoras.
How does one know what a particular trigger or constraint is supposed to be doing on a table. I am just two days old on this position, and no form of documentation, at the same time I am expected to trouble-shoot some error messages. Thanks for your help.
View 3 Replies View Related