I'm new to sql and
have a table dbo.Student
**column** **datatype**
Student Id Primary Key int not null
ClassId Foriegn Key int not null
BaseId Foriegn Key int not null
ClassId can have multiple base Id but not duplicates
I'm looking to write a condition If any ClassId have duplicate rows with same BaseId's
Example for good data
a b c
1 2 1
2 2 2
Example for bad data
a b c
1 2 1
2 2 1
Thanks
You can use below query to check if you have any duplicate rows in the table
select ClassId,baseid, COUNT(*) NumberOfDuplicate
from student
group by ClassId,baseid
having count(*) >1
There are "unique" constraints in SQL server. You can add one to that column and it will prevent the entry of a row in which that column value is not unique. Unique constraints are allowed on columns that are not keys.
This SO answer has both the gui solution and script solution:
stackoverflow.com/questions/5181877/
Gist of linked answer:
ALTER TABLE TableName ADD CONSTRAINT ConstraintName UNIQUE(ColumnName1, ColumnName2)
This will put a unique constraint on the combination of Column1 and Column2.
In SSMS object Explorer if you expand the table and look under "Keys" to see your constraint.
GUI steps:
Open SQL Server Management Studio.
Expand the Tables folder of the database where you want to create the
constraint.
Right-click the table where you want to add the constraint and click Design.
In the Table Designer menu, click Indexes/Keys.
In the Indexes/Keys dialog box, click Add.
Choose Unique Key in the Type drop-down list.
Related
How can I make an int column unique - on an existing table with data in - but allow multiple nulls - in SSMS.
There are many records in the table now - and they all have NULL in this column now.
I have seen ways to do this using a unique filtered index in TSQL - and I can see ways in the UI to do it without allowing NULLS.
Is this just not possible using the SSMS GUI?
As an aside what's the best way to do this just using pure TSQL? A unique filtered index?
With code:
create unique nonclustered index uixf_mycol
on dbo.t (col)
where col is not null;
In SSMS:
create a new nonclustered index by right clicking the index folder from
the expanded table in object explorer
add the column
check the unique checkbox
add the where clause in the filter panel.
You can create UNIQUE constraint on the column.
CREATE TABLE Test
(
P_Id int,
CONSTRAINT uc_ID UNIQUE (P_Id)
)
I just have a table that has two rows of data and want to delete the rows and SQL Server Management Studio is choking on my delete big time. Here is the error pop up.
All I'm doing is highlighting the row and clicking the delete key. This error doesn't make any sense to me at all! The only way I've been able to get rid of the rows is to drop and recreate the tables but that isn't acceptable down the road when I only want to remove one or two rows.
You have to add Primary Key to your table. Run this two queries
First add a candidate column:
ALTER TABLE dbo.tbl_admin ADD TempID int IDENTITY(1, 1);
Then make this column the table primary key
ALTER TABLE dbo.tbl_admin
ADD CONSTRAINT pk_tbl_admin
PRIMARY KEY (TempId);
I have three tables: A, AB and B.
A.ID and B.ID are int primary keys. AB.AID and AB.BID are int and (AID, BID) is the primary key of AB.
I need to make sure that AB.AID will be a foreign key which will reference A(ID) and AB.BID will be a foreign key which will reference B(ID).
If I, using Microsoft SQL Server Management Studio right-click the table and click Design and after the table appears I right-click and click on Relationships and add the foreign key, will I have the very same result as if I would:
Create a temporary table with the same structure
Migrate all records from AB there
Remove all records from AB
Change the structure of AB to have two foreign keys
Copy the data back
Remove the temporary table
?
There is no difference. Note that when attempting to build a relationship between tables, it will not allow the constraint to be created if there is conflicting data like:
Tbl1 (tbl1_id PK)
-----
1
2
Tbl2 (tbl2_id PK, tbl1_id)
-----
1
2
3
if you tried to create the foreign key, it would face as 3 does not exist within your primary table.
So yes, they are equivalent, just note that SQL server has built in safe guards to protect against invalid relational data.
I have a table which contained the following columns.
ID int PK
Name nvarchar(50) NotNull
FID int FK reference to ID
ID Name PK
1 A Null
2 B Null
3 C 1
4 D 1
5 E 1
6 F 2
So, The primary key includes as a primary key in a table. I want to do that if the primary key is deleted, the rows which is contained the primary key as a foreign is deleted automatically. (example: When I delete ID 1 row, I want to delete automatically ID 3, 4, 5 rows.). How to make that the primary key is included as a foreign key in a table? How can I do this. Thanks.
You need to implement a "trigger" that does a "cascading delete".
Here's a good link:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=142564
CREATE TRIGGER test_trig
ON dbo.Table_1
FOR DELETE
AS
BEGIN
delete a from dbo.Table_2 a
JOIN
deleted d
ON a.joincol = d.joincol
END
Here are some other alternatives:
http://www.mssqltips.com/sqlservertip/1508/foreign-key-vs-trigger-referential-integrity-in-sql-server/
And here is a link to Microsoft's documentation on "Cascading Referential Integrity Constraints":
http://msdn.microsoft.com/en-us/library/ms186973.aspx
NOTE: In Microsoft SQL, a cascading delete to a self-referencing table is not allowed. You must either use a trigger, create a stored procedure, or handle the cascading delete from the calling application. An example of this is where a single table has an ID as identity and a ParentID with a relationship to ID in the same table.
see here
The only way will be to add a trigger you can refer the following links for more information.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/b777ec73-e168-4153-a669-835049a96520
another link
Imagine I have a table with 3 columns:
ID (unique ID)
Description (varchar 2000)
IsDeleted (boolean)
I want to add a unique constraint so that all descriptions are unique. BUT only for active records i.e. IsDelete = false. If a user flags a record as deleted, they can re-add a record with the same description as a deleted record, but not the same as a active record.
So, I only want the constraint to effect record where IsDeleted = false.
Tim
In SQL 2008 you can do this using unique index with a filter:
CREATE UNIQUE NONCLUSTERED INDEX ix_table_UNC ON table(Description) WHERE IsDeleted = 0
Pre-SQL 2008, you'd need to create the unique index on a view of the table selecting only the non-deleted records:
CREATE VIEW dbo.vw_ActiveDescriptions WITH SCHEMABINDING
AS
SELECT Id, Description
FROM Table
WHERE IsDeleted= 0
GO
CREATE UNIQUE CLUSTERED INDEX ix_vw_ActiveDescriptions_UC ON dbo.vw_ActiveDescriptions(Description);
Create a new table for the deleted rows. Then enforce uniqueness with a UNIQUE constraint. You would have to make Description smaller because the maximum key size in SQL Server is 900 bytes.