How do you make a non-key column unique in SSMS? - tsql

In SQL Server Management Studio (SSMS) I have a table with a numeric, surrogate primary key. I would like to make another column unique, however. It seems like only primary keys can be made unique, however.
How does one make a non-key column unique, so that the same value may not be entered twice?
SQL Server 2012,
SSMS 2012
Also, if it matter, I used a numeric surrogate key on the table instead of a natural key because values in the column above, that I need to make unique, may change in the future.

It seems like only primary keys can be made unique, however.
Try using a unique constraint: https://msdn.microsoft.com/en-us/library/ms190024.aspx

Related

Is it bad for columns in composite keys to have mismatching types?

Problem:
I'd like to make a composite primary key from columns id and user_id for a postgres database table. Column user_id is a foreign key with an integer type, whereas id is a string. Will this cause a conflict because the types are different?
Edit: Also, are there combinations of types that would cause problems?
Context:
I obviously should match the type of the User.id field for its foreign key. And, the id for my table will be derived from a uuid to prevent data leaks. So I would prefer not to change the types of either field I want in this table.
Research:
I am using sqlalchemy. Their documentation mentions how to create a composite primary key, but it doesn't discuss dealing with different types for each column.
No, this won't be a problem.
Your question seems to indicate that you think, the values of the indexed columns are somehow concatenated and then stored in the index as a single value. This is not the case. Each column value is stored independently but together. Similar to the way the column values are stored in the actual table.

Is there efficient difference between varchar and int as PK

Could somebody tell is it good idea use varchar as PK. I mean is it less efficient or equal to int/uuid?
In example: car VIN I want to use it as PK but I'm not sure as good it will be indexed or work as FK or maybe there is some pitfalls.
It depends on which kind of data you are going to store.
In some cases (I would say in most cases) it is better to use integer-based primary keys:
for instance, bigint needs only 8 bytes, varchar can require more space. For this reason, a varchar comparison is often more costly than a bigint comparison.
while joining tables it would be more efficient to join them using integer-based values rather that strings
an integer-based key as a unique key is more appropriate for table relations. For instance, if you are going to store this primary key in another tables as a separate column. Again, varchar will require more space in other table too (see p.1).
This post on stackexchange compares non-integer types of primary keys on a particular example.

computed hashybtes column used as primary key

I have long gene sequence which could be the natural primary key, but I'm looking for a way to find a more succinct alternative representation of the natural key. Do not want to use a surrogate key. Not worried about performance, since there will not be a lot of joins to worry about where the efficiency of the PK is an issue.
Is this possible?
create table foo(
myvalue varchar(2000) not null,
md5 as hashbytes('MD5',myvalue) PERSISTED PRIMARY KEY NOT NULL -- bad syntax
)
If so, what's the correct syntax? The above is not correct.
Also can I create a child table and set up an FK relationship? I do not find the Limitations section in the documentation clear about this:
create table fooChild(
id int primary key not null,
md5 varbinary(16)
)
alter table fooChild add constraint FK_FOOCHILD_FOO
foreign key(md5) references FOO(md5)
Here is the answer. http://www.devx.com/tips/Tip/15397
Basically you have to ensure a function cannot return null so wrap hashbytes in isnull or coalesce.

what is the right data type for unique key in postgresql DB?

which data type should I choose for a unique key (id of a user for example) in postgresql database's table?
does bigint is the one?
thanks
Use the serial type for automatically incrementing unique ids.
If you plan to have more than two billion entries, use bigserial. serial is the PostgresSQL equivalent of MySQL's AUTO_INCREMENT.
PostgresSQL Documentation: Numeric Types
bigint (or bigserial if you need auto-incrementing keys) is just fine.
If know for certain that you are not going to load too many rows, you might consider integer (or a regular serial) and potentially save some harddisk space.
According to this answer the current recommended approach to doing auto-increment unique IDs is to use the generated as identity syntax instead of serial.
Here's an example:
-- the old way
create table t1 (id serial primary key);
-- the new way
create table t2 (id integer primary key generated always as identity);

Primary key defined by many attributes?

Can I define a primary key according to three attributes? I am using Visual Paradigm and Postgres.
CREATE TABLE answers (
time SERIAL NOT NULL,
"{Users}{userID}user_id" int4 NOT NULL,
"{Users}{userID}question_id" int4 NOT NULL,
reply varchar(255),
PRIMARY KEY (time, "{Users}{userID}user_id", "{Users}{userID}question_id"));
A picture may clarify the question.
Yes you can, just as you showed.(though I question your naming of the 2. and 3. column.)
From the docs:
"Primary keys can also constrain more than one column; the syntax is similar to unique constraints:
CREATE TABLE example (
a integer,
b integer,
c integer,
PRIMARY KEY (a, c)
);
A primary key indicates that a column or group of columns can be used as a unique identifier for rows in the table. (This is a direct consequence of the definition of a primary key. Note that a unique constraint does not, by itself, provide a unique identifier because it does not exclude null values.) This is useful both for documentation purposes and for client applications. For example, a GUI application that allows modifying row values probably needs to know the primary key of a table to be able to identify rows uniquely.
A table can have at most one primary key (while it can have many unique and not-null constraints). Relational database theory dictates that every table must have a primary key. This rule is not enforced by PostgreSQL, but it is usually best to follow it.
"
Yes, you can. There is just such an example in the documentation.. However, I'm not familiar with the bracketed terms you're using. Are you doing some variable evaluation before creating the database schema?
yes you can
if you'd run it - you would see it in no time.
i would really, really, really suggest to rethink naming convention. time column that contains serial integer? column names like "{Users}{userID}user_id"? oh my.