Make 2 columns from 2 different tables unique in sql - sql

I have a database having many tables and I want to make 2 columns from 2 different tables unique.
For example, 1 table named person contains fields like id, name,father_name,date of birth, res. address etc and other table named institute contains id (which is foreign key of id of person table), institute.
I have to make a combination of (name,father_name,date of birth, institute) unique. Can anybody help me??

Related

Inner Join query between 2 tables with multiple conditions

We have 2 tables Application and Address, Application table has columns like home_address,office_address and delivery_address and all these 3 column is of number type having values like say 111,112 and 113 for a row of it.It has many such rows. In a separate table we have say column like id and address having lot of records with different id's. we need to get output like say all above 3 columns plus 3 more columns like say full_homeaddress, full_officeaddress , full_deliveryaddress in every row of output.
If it was only one column like home_address in Application table, i would have easily done the join with primary key of Address table and one foreign key of Application table but here we need to display full address of 3 different fields simultaneosly in each row of output.
I think for one row of output, i will have to get records from 1 row of Application table and 3 different rows of Address table to display in every row of output. For these 3 columns of Application table , required addresses are in 3 different rows of Address table.
Kindly help how to write join for it
SELECT app.*,
home.address_description,
office.address_description,
delivery.address_description
FROM application app,
Address home,
Address office,
Address delivery
WHERE
app.homeaddressId = home.addressId(+)
AND app.officeAddressId = office.addressId(+)
AND app.deliveryAddressId = delivery.addressId(+)

How to make 2 relations to the same table(using diferent columns) on dataset.relations?

I have a table clients and a table country, clients has 2 columns referencing country id (id of the country the client is from, and id from the counry the client wants its packages sent to), while dataset.relations is useful to fuse tables using 1 column(or more as long as they are diferent) i cant figure how to display a table that contains the info of the client, and the NAME of both countires corresponding to the ids.
For just the country of the client i go ass follows
billOrderDataset.Relations.Add("clientCountryRelation",countriesTableCopy.Columns("id"), clientTableCopy.Columns("countryId"))
clientTableCopy.Columns.Add("countryName",GetType(String), "Parent.countryName")
but after that i dont know how would i also add the name of the country to the column "deliveryCountryId" since there is already a relation using both tables and the id column from country, so bascially i would need something like
billOrderDataset.Relations.Add("clientCountryRelation2",countriesTableCopy.Columns("id"), clientTableCopy.Columns("DeliveryCountryId"))
clientTableCopy.Columns.Add("DeliveryCountryName",GetType(String), "Parent.countryName")
The answer is hidden in a paragraph inside the Remarks section in the documentation of the DataColumn.Expression property.
Parent/Child Relation Referencing
....
When a child has more than one
parent row, use Parent(RelationName).ColumnName. For example, the
Parent(RelationName).Price references the parent table’s column named
Price via the relation.
So, in other words, when you have more than one relation on the child table you need to explicitly give the name of the relation in the expression syntax
clientTableCopy.Columns.Add("countryName",GetType(String), "Parent(clientCountryRelation).countryName")
clientTableCopy.Columns.Add("DeliveryCountryName",GetType(String), "Parent(clientCountryRelation2).countryName")

connecting multiple tables in a database using keys in sql

I have a table named student with columns studentid, name, address, email, phone.
Each student may have multiple email-id and multiple phone numbers. I want to record all emails and phone numbers too. So, I manipulates the student table columns by removing email-id column and phone column And created another table studentemail with columns emailserialnumber , email-id. And one more table studentphone with columns phoneserialnumber , phone. student table consists of studentid, name, address columns. should I add anymore columns to student table to maintain relation with other tables. Now I have three tables. now how can I relate the tables using keys. I mean like which column should I make primary key and foreign key so that three tables are connected.
Thank you
Your studentemail and studentphone tables simply need another column to reference the student table. In your case, this would be the studentid column.
You can then bring the data together in a single query. I should note that you might want a column that specifies what the email and phone numbers are -- home, mobile, work, and so on.

More joins or more columns?

I have a very basic question, which would be a more efficient design, something that involves more joins, or just adding columns to one larger table?
For instance, if we had a table that stored relatives like below:
Person | Father | Mother | Cousing | Etc.
________________________________________________
Would it be better to list the name, age, etc. directly in that table.. or better to have a person table with their name, age, etc., and linked by person_id or something?
This may be a little simplistic of an example, since there are more than just those two options. But for the sake of illustration, assume that the relationships cannot be stored in the person table.
I'm doing the latter of the two choices above currently, but I'm curious if this will get to a point where the performance will suffer, either when the person table gets large enough or when there are enough linked columns in the relations table.
Id' go for more "Normality" to increase flexibility and reduce data duplication.
PERSON:
ID
First Name
Last Name
Person_Relations
PersonID
RelationID
TypeID
Relation_Type
TypeID
Description
This way you could support any relationship (4th cousin mothers side once removed) without change code.
It is a much more flexible design to separate out the details of each person from the table relating them together. Typically, this will lead to less data consumption.
You could even go one step further and have three tables: one for people, one for relationship_types, and one for relationships.
People would have all the individual identifying info -- age, name, etc.
Relationship_types would have a key, a label, and potentially a description. This table is for elaborating the details of each possible relationship. So you would have a row for 'parent', a row for 'child', a row for 'sibling', etc.
Then the Relationships table has a four fields: one for the key of each person in the relationship, one for the key of the relationship_type, and one for its own key. Note that you need to be explicit in how you name the person columns to make it clear which party is which part of the relationship (i.e. saying that A and B have a 'parent' relationship only makes sense if you indicate which person is the parent vs which has the parent).
Depending on how you plan to use the data a better structure may be
a table for Person ( id , name etc )
a table for relationships (person_a_id, person_b_id, relation_type
etc)
where person_a_id and person_b_id relate to id in person
sample data may look like
Person
ID Name
1 Frank
2 Suzy
3 Emma
Relationship
A B Relationship
1 2 Wife
2 1 Husband
1 3 Daughter
2 3 Daughter
3 1 Father
3 2 Mother

How to merge two identical database data to one?

Two customers are going to merge. They are both using my application, with their own database. About a few weeks they are merging (they become one organisation). So they want to have all the data in 1 database.
So the two database structures are identical. The problem is with the data. For example, I have Table Locations and persons (these are just two tables of 50):
Database 1:
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
1 Location A
2 Location B
Persons:
Id LocationId Name etc...
1 1 Mark
2 2 Ashley
3 1 Ben
We see that person is related to location (column locationId). Note that I have more tables that is referring to the location table and persons table.
The databases contains their own locations and persons, but the Id's can be the same. In case, when I want to import everything to DB2 then the locations of DB1 should be inserted to DB2 with the ids 3 and 4. The the persons from DB1 should have new Id 4,5,6 and the locations in the person table also has to be changed to the ids 4,5,6.
My solution for this problem is to write a query which handle everything, but I don't know where to begin.
What is the best way (in a query) to renumber the Id fields also having a cascade to the childs? The databases does not containing referential integrity and foreign keys (foreign keys are NOT defined in the database). Creating FKeys and Cascading is not an option.
I'm using sql server 2005.
You say that both customers are using your application, so I assume that it's some kind of "shrink-wrap" software that is used by more customers than just these two, correct?
If yes, adding special columns to the tables or anything like this probably will cause pain in the future, because you either would have to maintain a special version for these two customers that can deal with the additional columns. Or you would have to introduce these columns to your main codebase, which means that all your other customers would get them as well.
I can think of an easier way to do this without changing any of your tables or adding any columns.
In order for this to work, you need to find out the largest ID that exists in both databases together (no matter in which table or in which database it is).
This may require some copy & paste to get a lot of queries that look like this:
select max(id) as maxlocationid from locations
select max(id) as maxpersonid from persons
-- and so on... (one query for each table)
When you find the largest ID after running the query in both databases, take a number that's larger than that ID, and add it to all IDs in all tables in the second database.
It's very important that the number needs to be larger than the largest ID that already exists in both databases!
It's a bit difficult to explain, so here's an example:
Let's say that the largest ID in any table in both databases is 8000.
Then you run some SQL that adds 10000 to every ID in every table in the second database:
update Locations set Id = Id + 10000
update Persons set Id = Id + 10000, LocationId = LocationId + 10000
-- and so on, for each table
The queries are relatively simple, but this is the most work because you have to build a query like this manually for each table in the database, with the correct names of all the ID columns.
After running the query on the second database, the example data from your question will look like this:
Database 1: (exactly like before)
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
10001 Location A
10002 Location B
Persons:
Id LocationId Name etc...
10001 10001 Mark
10002 10002 Ashley
10003 10001 Ben
And that's it! Now you can import the data from one database into the other, without getting any primary key violations at all.
If this were my problem, I would probably add some columns to the tables in the database I was going to keep. These would be used to store the pk values from the other db. Then I would insert records from the other tables. For the ones with foreign keys, I would use a known value. Then I would update as required and drop the columns I added.