How to create a constraint for conditional unique values? - sql

I am using SQL Server. I have this table:
CREATE TABLE Student
(
ID int PRIMARY KEY,
FirstName varchar(100),
LastName varchar(100),
Active bit;
)
I want to have unique(FirstName, LastName) students only if Active = 1. If they are inactive, unique constraint should not trigger. Any idea?

You can't create a CONSTRAINT for that, however, you can create a filtered unique index:
USE Sandbox;
GO
CREATE TABLE dbo.Student (ID int IDENTITY(1, 1) PRIMARY KEY,
FirstName varchar(100),
LastName varchar(100),
Active bit);
CREATE UNIQUE INDEX UQ_StudentName
ON Student (FirstName,LastName)
WHERE Active = 1;
GO
INSERT INTO dbo.Student (FirstName,
LastName,
Active)
VALUES ('Jane', 'Smith', 1); --Success
GO
INSERT INTO dbo.Student (FirstName,
LastName,
Active)
VALUES ('Jane', 'Smith', 0); --Success
GO
INSERT INTO dbo.Student (FirstName,
LastName,
Active)
VALUES ('Jane', 'Smith', 0); --Success
GO
INSERT INTO dbo.Student (FirstName,
LastName,
Active)
VALUES ('Jane', 'Smith', 1); --Fails;
GO
UPDATE dbo.Student
SET Active = 1
WHERE ID = 2; --Fails;
GO
SELECT *
FROM dbo.Student;
GO
DROP TABLE dbo.Student;
I however, highly recommend against the thought that names are unique. I (personally) shared my name and date of birth with another person at several places in my youth and businesses that treated names (and date of birth) as unique on their systems were such a head ache for the both of us (there really were places where I (or they) couldn't register without using an abbreviated name because we "already existed").

Create a filtered unique index:
CREATE UNIQUE INDEX UNI_Student_FirstName_LastName ON Student (FirstName, LastName)
WHERE Active = 1

Related

Importing into Gender Field in SQL/SSIS

How would you handle importing only male (m) or female (f) data into a gender field, so that it won't get populated with any potential extraneous source data, in SQL or SSIS?
You can create a column constraint:
CREATE TABLE Persons
(
Id int NOT NULL,
LastName varchar(255),
FirstName varchar(255),
Gender varchar(1),
CONSTRAINT chk_Gender CHECK (Gender ='m' or Gender='f')
)
Insert into Persons values(1,'smith', 'jane', 'f');
insert into Persons values(2,'smith', 'john', SUBSTRING('male', 1, 1));
/* this will fail
INSERT INTO Persons values (3,'foo', 'bar', 'u');
*/
select * FROM Persons;
Working Fiddle
It's trival to add a check constraint, something like:
CREATE TABLE #Test (
GENDER CHAR(1) CONSTRAINT Constraint_Gender CHECK (GENDER IN ('A','B','C'))
)
INSERT INTO #Test VALUES ('A')
INSERT INTO #Test VALUES ('D')
(1 row(s) affected)
Msg 547, Level 16, State 0, Line 9
The INSERT statement conflicted with the CHECK constraint "Constraint_Gender".
However, you should really cater for non-binary genders in a similar manner to facebook etc https://www.facebook.com/facebookdiversity/posts/774221582674346 (hence A, B, C in my example above)

Removing Duplicates added by foriegn key in Oracle DB

I'am working on a Hostel Project in Oracle DB, I have 2 tables (students,rooms) and when I run query to get name of students ,their room IDs and facilities that are provided to them it returns duplicate student names with all the facilities and with little editing in query it returns duplicate facilities.
How can I fix those duplicates?
Here is the Code
--CREATE TABLE STUDENTS
Create table students(
regno integer primary key,
name varchar2(30),
phonenum number,
address varchar2(30),
roomalloc number
);
Alter Table students Add CONSTRAINT fk_roomalloc
FOREIGN KEY (roomalloc)
REFERENCES rooms(roomno);
Alter table students drop constraint fk_roomalloc
--ALTER TABLE STUDENTS ADD JOIN DATE
alter table students add (joindate Date);
--Alter Table for Gender
Alter Table students add(gender varchar2(2));
Alter Table students MODIFY gender NOT NULL;
--Constraint on Gender
Alter Table students Add CONSTRAINT gen
Check (gender IN('M','F','m','f'));
Alter Table students MODIFY mess default 'M';
--Constraint Gender Check for Room Allocation
Alter Table students Add CONSTRAINTS gen_check
Check (Room IN(Gender='M' , 'Gender='F'));
--Constraints:ADD MESS AS TRUE/FALSE
ALTER TABLE students ADD (mess varchar2(4));
Alter Table students Add CONSTRAINT mess_present
Check (mess IN('Yes','No','yes','no'));
Alter Table students MODIFY mess default 'No';
--Drop Constraint
ALTER TABLE students
DROP CONSTRAINT mess_present
--ALTER TABLE NAME AS NOT NULL
alter table students MODIFY name NOT NULL;
--ALTER TABLE PHONE NO
alter table students MODIFY phonenum UNIQUE;
--ALTER TABLE Room Allocated
alter table students MODIFY roomalloc Not Null;
Describe students;
--DATA ENTRY and Modification
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,mess)
VALUES (1, 'Haseeb', 012345678,'Rawalpindi',1,'1-sep-14','No');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate)
VALUES (2, 'Faisal', 03451111111,'Rawalpindi',1,'12-sep-14');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate)
VALUES (3, 'Shahbaz', 03313214567,'Khewra',1,'15-feb-15');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate)
VALUES (4, 'Muhaddas', 01235131237,'Kashmir',1,'15-feb-15');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,mess)
VALUES (5, 'Haseem', 01254530987,'Islamabad',2,'15-sep-15','Yes');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,mess)
VALUES (6, 'Asim', 03341234567,'Muzzafargarh',3,'15-sep-14','Yes');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,mess)
VALUES (7, 'Izza', 01231234564,'Sialkot',25,'15-sep-15','Yes');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,mess)
VALUES (8, 'Sara', 01231234561,'Narrowal',25,'15-sep-15','Yes');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,mess)
VALUES (9, 'Maria', 01231234567,'Wah',4,'25-sep-15','Yes');
INSERT INTO students(regno, name, phonenum, address, roomalloc,joindate,Gender)
VALUES (10, 'Faha', 0123123452,'Okara',4,'26-sep-15','F');
Update students SET Gender='M' WHERE regno>=1 And regno<=6;
Update students SET roomalloc=4,Gender='F' WHERE regno>=7 And regno<=9;
Describe students;
--Data Fetching Queries
Select *
From students,rooms
Where rooms.facility='Fan/Net/TV'
ORDER BY roomalloc , regno;
Drop table students;
--CREATE TABLE Rooms
Create table rooms(
roomno integer primary key,
facility varchar2(30)
);
--Altering Rooms Types
Alter Table rooms Add CONSTRAINT rtype
Check (facility IN('AC/Net/TV','Fan/Net/TV','Fan/Net'));
Alter Table rooms MODIFY facility default 'Fan/Net';
Alter table rooms drop constraint str
INSERT INTO rooms( roomno, facility)
VALUES (1,'AC/Net/TV');
INSERT INTO rooms( roomno, facility)
VALUES (2,'Fan/Net/TV');
INSERT INTO rooms( roomno, facility)
VALUES (3,'Fan/Net/TV');
INSERT INTO rooms( roomno, facility)
VALUES (4,'Fan/Net');
Alter table rooms Modify facility UNIQUE
Select *
From rooms;
Select students.name,rooms.facility
From students,rooms
Where roomno=4;
Drop table rooms
You need to join the tables properly to get the desired result:
Select students.name,rooms.facility
From students join rooms on students.roomalloc = rooms.roomno
Where rooms.roomno=4;

Insert a row if it doesn't exist via query

I am trying to write a query that will insert a group of people into a table if that person does not exist. For example, I have table full of people and I need to add more people into the database and I don't know if they are already there. I do know that the social security number (ssn) will never be the same for two people. Could a query be used to check if the ssn is in the table and if not insert the person into the table? If the ssn is in the table then go to the next person and check?
I was thinking about using a stored procedure, but I do not have any rights to create a store procedure.
You can insert your data into a table variable or temp table and then INSERT INTO table from temp table where it does not exists in your table.
DECLARE #Inserted AS TABLE
(
NAME VARCHAR(50)
,SSN DECIMAL(10, 0)
)
INSERT INTO #Inserted
( NAME, SSN )
VALUES ( 'Bob', 123456789 )
, ( 'John', 123546789 )
, ( 'James', 123456798 )
INSERT INTO MyTable
SELECT *
FROM #Inserted AS i
LEFT OUTER JOIN MyTable AS m
ON i.SSN = m.SSN
WHERE m.SSN IS NULL
Here are a couple ideas to get you started. I use MERGE a lot because it offers so much control. You could also look into the IN clause as part of a WHERE predicate in the INSERT SELECT statement.
MERGE
DECLARE #PERSONTABLE TABLE (ID INT PRIMARY KEY IDENTITY(1,1), FirstName VARCHAR(max))
INSERT INTO #PERSONTABLE (FirstName) VALUES ('Bill'),('Sally'),('Bob')
DECLARE #NEWPEOPLE TABLE (FirstName VARCHAR(max))
INSERT INTO #NEWPEOPLE (FirstName) VALUES ('Jim'), ('Sally')
--MERGE
MERGE INTO #PERSONTABLE AS T
USING #NEWPEOPLE AS S
ON (T.FirstName = S.FirstName)
WHEN NOT MATCHED BY TARGET THEN
INSERT (FirstName) VALUES (S.FirstName);
SELECT * FROM #PERSONTABLE
EXCEPT
DECLARE #PERSONTABLE TABLE (ID INT PRIMARY KEY IDENTITY(1,1), FirstName VARCHAR(max))
INSERT INTO #PERSONTABLE (FirstName) VALUES ('Bill'),('Sally'),('Bob')
DECLARE #NEWPEOPLE TABLE (FirstName VARCHAR(max))
INSERT INTO #NEWPEOPLE (FirstName) VALUES ('Jim'), ('Sally')
--EXCEPT
INSERT INTO #PERSONTABLE (FirstName)
SELECT FirstName FROM #NEWPEOPLE
EXCEPT
SELECT FirstName FROM #PERSONTABLE
SELECT * FROM #PERSONTABLE
You could do it like this if the new people are in another table. If not, then use Vladimir's solution.
INSERT INTO People(ssn, firstname, lastname)
SELECT ssn, firstname, lastname
FROM newpeople
WHERE ssn not in (select ssn from people )
INSERT INTO People(ssn, firstname, lastname)
SELECT np.ssn, np.firstname, np.lastname
FROM newpeople np
LEFT JOIN People p on np.ssn = p.ssn
WHERE p.ssn IS NULL
Here's another option I use a lot. Normally joins are better than sub-selects... if the joined table value is null you know you don't have a hit in the joined table.

Split One table into Two in SQL Server 2008

I need to break one table (structure built by someone else but I need the data it contains thousands of records) into two new tables I created.
Table Name: Customers_Info (Old Table)
FullName Telephone Address
Adam Johnson 01555777 Michigan
John Smith 01222333 New York
John Smith 01222333 New Jersey
Lara Thomas 01888999 New Mexico
The above is the old table. Now I created two tables to hold the data, one table for customers with a default address, and the other table to hold additional addresses. In the shown example I need 3 persons to be listed in the Customers table, and the address of "John Smith" (the second one New Jersey) to be listed in the Addresses table.
The common field to look at here is "Telephone" and it's unique for every customer.
Here's how the result should be display.
Table Name: Customers (New Table)
CustomerID FullName Telephone Default_Address
1 Adam Johnson 01555777 Michigan
2 John Smith 01222333 New York
3 Lara Thomas 01888999 New Mexico
Table Name: Addresses (New Table)
AddressID CustomerID Address
1 2 New Jersey
Of course it was easy to copy all data into the new Customers table, but what I'm stuck at now, is how to remove the duplicates from Customers and insert them into the Addresses table with the Customer ID and Address only.
Thanks!
Give a try with below code and let me know the comments/results.
CREATE TABLE [Customers_Info]
(
FullName VARCHAR(50)
,Telephone VARCHAR(50)
,Address VARCHAR(50)
)
GO
CREATE TABLE Customers
(
CustomerID INT IDENTITY(1,1)
,FullName VARCHAR(50)
,Telephone VARCHAR(50)
,Default_Address VARCHAR(50)
)
GO
ALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers
PRIMARY KEY CLUSTERED (CustomerID);
GO
CREATE TABLE Addresses
(
AddressID INT IDENTITY(1,1)
,CustomerID INT
,[Address] VARCHAR(50)
)
GO
ALTER TABLE dbo.Addresses ADD CONSTRAINT PK_Addresses
PRIMARY KEY CLUSTERED (AddressID);
GO
ALTER TABLE Addresses ADD CONSTRAINT FK_CustomerID_Addresses_Customers FOREIGN KEY (CustomerID)
REFERENCES dbo.Customers(CustomerID);
GO
INSERT INTO [Customers_Info] VALUES ('Adam Johnson', '01555777', 'Michigan')
INSERT INTO [Customers_Info] VALUES ('John Smith' , '01222333', 'New York')
INSERT INTO [Customers_Info] VALUES ('John Smith' , '01222333', 'New Jersey')
INSERT INTO [Customers_Info] VALUES ('Lara Thomas' , '01888999', 'New Mexico')
INSERT INTO [Customers_Info] VALUES ('Lara Thomas' , '01888999', 'New Mexico1')
INSERT INTO [Customers_Info] VALUES ('Lara Thomas' , '01888999', 'New Mexico2')
INSERT INTO [Customers_Info] VALUES ('Adam Johnson', '01555777', 'Michigan1')
INSERT INTO [Customers_Info] VALUES ('Adam Johnson', '01555777A', 'Michigan')
INSERT INTO [Customers_Info] VALUES ('Adam Johnson', '01555777A', 'Michigan2')
GO
SELECT * FROM [Customers_Info]
--DELETE FROM Customers
--TRUNCATE TABLE Addresses
------------------------------------------------------------------------------------------------------------------
;WITH a as
(
SELECT FullName,Telephone,[Address],
rn = row_number() over (partition by FullName, Telephone order by FullName)
FROM [Customers_Info]
)
INSERT INTO Customers SELECT
FullName,Telephone,[Address] from a where rn = 1
------------------------------------------------------------------------------------------------------------------
;WITH b as
(
SELECT FullName,Telephone,[Address],
rn = row_number() over (partition by FullName, Telephone order by FullName)
FROM [Customers_Info]
)
INSERT INTO Addresses SELECT CI.CustomerID,b.[Address] FROM Customers CI
INNER JOIN b ON b.FullName=CI.FullName AND b.Telephone=CI.Telephone
WHERE b.rn>1
SELECT * FROM Customers
SELECT * FROM Addresses
DROP TABLE [Customers_Info]
GO
DROP TABLE Addresses
GO
DROP TABLE Customers
GO
It would be more normalized if you broke it up into one more table for three total tables. Have the Customers table that has only customer data, have the Address table (which you could possibly rename to State) that has only the Address, then a CustomerAddress table that has both keys to each of those tables as Foreign Keys.
I will start you off to begin:
INSERT INTO Customers (FullName, Telephone)
SELECT DISTINCT FullName, Telephone
FROM Customers_Info
You would do the same for Address. For the 3rd table, you would perform the lookups like this:
INSERT INTO CustomerAddress (CustomerID, AddressID)
SELECT C.CustomerID, A.AddressID
FROM Customers_Info CI
INNER JOIN Customers C
ON CI.Telephone = C.Telephone
INNER JOIN Address A
ON CI.Address = A.Address

Oracle How to assign usernames

How would I assign usernames for students using their initials followed by numbers that increment? I'm not sure how many students I have in my database and I would like their usernames to look like so "AC001, JD003 for example. And also, If the have similar initials I would like the username to continue incrementing in that order. For example "AC001" and "AC002"
You will need to use a trigger on BEFORE INSERT event and a sequence to implement this.
I will assume your table is as follows:
TABLE user
( userid VARCHAR2(100) -- PRIMARY KEY COLUMN
, firstname VARCHAR2(100)
, lastname VARCHAR2(100)
, .... -- Other columns
);
Create a sequence as follows:
CREATE SEQUENCE sq_userid;
You insert rows with userid column set to null.
INSERT INTO user VALUES (null, 'Jack', 'Daniels', ....);
Then, your trigger should look like this:
CREATE OR REPLACE TRIGGER trg_user
BEFORE INSERT ON user
REFERENCING new AS new old AS old
FOR EACH ROW
BEGIN
IF :new.userid IS NULL THEN
:new.userid := UPPER(LEFT(firstname, 1))
|| UPPER(LEFT(lastname, 1))
|| TO_CHAR(sq_userid.nextval, '099');
END IF;
END;
/
The trigger will put 'JD001' AS the value for userid in this instance.
For more information- Coding triggers, sequences and to_char.
i think this will also work
1st Create sequence user_seq;
insert into user values('user_name'||user_seq.nextval);
you can modify above query as per your requirement.
I fiddled a solution, but it's in MySQL. I'm sure you can get the general idea and convert it into Oracle (there's an Oracle 11g option in SQL Fiddle itself): http://sqlfiddle.com/#!2/e8ed5/1
Schema:
CREATE TABLE ALPHA_ROLL (
ALPHA1 CHAR(1),
ALPHA2 CHAR(1),
ROLL INTEGER,
PRIMARY KEY(ALPHA1, ALPHA2)
)//
CREATE TABLE user (
USERID VARCHAR(5),
FIRSTNAME VARCHAR(100),
LASTNAME VARCHAR(100),
PRIMARY KEY (USERID)
)//
CREATE TRIGGER trg_user
BEFORE INSERT ON user
FOR EACH ROW
BEGIN
SET #gen_user_id =
(SELECT CONCAT(ALPHA1,ALPHA2,LPAD((ROLL+1), 3, '0'))
FROM ALPHA_ROLL
WHERE ALPHA1 = UPPER(LEFT(NEW.firstname, 1))
AND ALPHA2 = UPPER(LEFT(NEW.lastname, 1))
LIMIT 1);
IF (#gen_user_id IS NULL) THEN
INSERT INTO ALPHA_ROLL (ALPHA1, ALPHA2, ROLL)
VALUES (UPPER(LEFT(new.firstname, 1)), UPPER(LEFT(new.lastname, 1)), 1);
SET #GEN_USER_ID =
CONCAT(UPPER(LEFT(new.firstname, 1)),
UPPER(LEFT(new.lastname, 1)),
'001');
ELSE
UPDATE ALPHA_ROLL SET ROLL = ROLL + 1
WHERE ALPHA1 = UPPER(LEFT(new.firstname, 1))
AND ALPHA2 = UPPER(LEFT(new.lastname, 1));
END IF;
SET new.userid = #GEN_USER_ID;
END//
SQL:
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Johnnie', 'Walker');
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Jason', 'Mraz');
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Mariah', 'Carrie');
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Justin', 'Morg');
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Matthew', 'Congoman');
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Jack', 'Waltson');
INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES ('Jojo', 'Wonderful');
SELECT * FROM USER;
SELECT * FROM ALPHA_ROLL;
Result:
USERID FIRSTNAME LASTNAME
JM001 Jason Mraz
JM002 Justin Morg
JW001 Johnnie Walker
JW002 Jack Waltson
JW003 Jojo Wonderful
MC001 Mariah Carrie
MC002 Matthew Congoman
ALPHA1 ALPHA2 ROLL
J M 2
J W 3
M C 2