There are many ways to retrieve unique datasets within SQL. The last generations of SQL standards denoted a new keyword called DISTINCT. What that does is it removes duplication among returned result sets.
So if you have a set whch contains { 1 , 2, 3 , 3, 4 , 4 , 4 ,5 } it will return { 1 ,2 , 3 ,4 , 5 } which is similar to this query SELECT DISTINCT num FROM mytable;
Now your question can be stated in two different questions. So my other understanding to your question is that "how do you make unique data in sql". That is relatively simple subject! SQL standards gave us KEYS to work with.
Primary Keys , Foreign Keys, and Unique Keys, or Keys.
Keys are just normal representation of keys.
Primary Keys denote ONE and ONLY one that instance which is the index as well. Every table should have a primary key. It can either be a normal attribute that is guaranteed to be unique or it can be generated by the DBMS (such as a globally unique identifier). It can consist of a single attribute or multiple attributes.
For example if we have while creating our table:
CREAT TABLE users (
firstname TEXT,
lastname TEXT,
PRIMARY KEY(firstname));
or
CREAT TABLE users (
firstname TEXT,
lastname TEXT,
PRIMARY KEY(firstname,lastname));
The difference between the tables above is that when we do primary key(firstname) we are saying there could be NO DUPLICATION of firstname in that table. But the second one says we CANNOT HAVE ANY DUPLICATION of FIRSTNAME nand LASTNAME combined. So if I say
===========
SET A
{John | Smith}
{John | Smiths}
{Johns | Smiths}
===========
Set A is valid when we do PRIMARY KEY (firstname , lastname)
But not valid when we do PRIMARY KEY (firstname) since we see duplications!
A primary key adds an index which makes it unique for every row. You CANNOT have more than one primary key, if you ever want one just make the other columns UNIQUE key. If your referencing another column from another table within this current table then use FOREIGN KEY.
Read a book and you will understand!
Good Luck
2006-07-03 16:17:39
·
answer #1
·
answered by ? 6
·
1⤊
0⤋
this is extremely greater useful to apply 3 tables to try this. A products table, containing product identity and different information fields. An factors table, containing an get right of entry to for each a threat element with element identity and different information fields. A product_ingredients table that contains a product identity and an element identity field, and not the rest (that's your institutions table). extremely, in case you have product identity a million, and that product makes use of element ids a million, 2, and 3, then your products_ingredients table could contain the entries (a million,a million), (a million,2), and (a million,3). this is basically a rely of writing a question to connect the tables once you're able to be able to desire to retrieve the advice.
2016-11-01 04:14:19
·
answer #2
·
answered by ? 4
·
0⤊
0⤋
The DISTINCT keyword will pull unique values for a record set as in:
SELECT DISTINCT lastName,firstName FROM users
this will pull all of the unique first and last name combinations in the users table.
if the table has the records:
lastName | firstName
-------------------------------
Elder | Steve
Smith | John
Savage | Dan
Smith | John
Smith | Ed
you will get records 1,2,3 and 5, but not 4
2006-07-03 16:11:51
·
answer #3
·
answered by John J 6
·
0⤊
0⤋
If you elaborate more about what you are trying to do I can give you an answer
2006-07-03 15:14:26
·
answer #4
·
answered by Juan C 2
·
0⤊
0⤋
we need more details. :)
2006-07-03 15:32:25
·
answer #5
·
answered by Anonymous
·
0⤊
0⤋