Answer the question
In order to leave comments, you need to log in
How to check if a varchar column is unique?
There is a task:
1. Drive a list of words (surnames, first names, ranks, etc.) into 1 column
2. When adding a list, you need to check the uniqueness of the values, if not unique - do not write
3. Pull the list, but without duplicates back to a text file .
Let's deal with the first and last (import wizard). The question is how to ensure the uniqueness of values in a column when creating a database (structure)? It is advisable not to use an index (no work with this database is required, and the volume of the database grows a lot) and not to use MERGE.
Now we have a variant with a primary key (without checking for uniqueness):
CREATE TABLE dic
( dic_id BIGINT NOT NULL IDENTITY(1, 1), /* Код слова */
dic_value VARCHAR(30) NOT NULL, /* слово */
CONSTRAINT PK_dic PRIMARY KEY (dic_id))
WITH (DATA_COMPRESSION = PAGE);
GO
Answer the question
In order to leave comments, you need to log in
it is necessary to separate the flies from the cutlets
SELECT DISTINCT ...
What is the DBMS?
MySQL, for example, simply creates a unique index:
CREATE TABLE dic(
...
dic_value VARCHAR(30) NOT NULL UNIQUE
...);
This can only be done with a unique index. When adding a non-unique record, it will throw an error.
Otherwise, you will have to manually do the same thing as him, much less efficient in terms of speed and resource costs.
ps if there are a lot of columns, then instead of adding them to the index, you can add only a hash row based on them (i.e. add another field in the table and when adding, manually programmatically or using sql, the hash from the sum of concatenate your columns, only between rows add some character to separate the columns), just take something not the simplest so that the probability of collisions is as small as possible (collision in your case is a false positive on lines that are not duplicates)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question