unique-key

How add unique key to existing table (with non uniques rows)

别来无恙 提交于 2019-11-27 12:59:20
I want to add complex unique key to existing table. Key contains from 4 fields ( user_id , game_id , date , time ). But table have non unique rows. I understand that I can remove all duplicate dates and after that add complex key. Maybe exist another solution without searching all duplicate data. (like add unique ignore etc). UPD I searched, how can remove duplicate mysql rows - i think it's good solution. Remove duplicates using only a MySQL query? Igor Lozovsky You can do as yAnTar advised ALTER TABLE TABLE_NAME ADD Id INT IDENTITY(1,1) PRIMARY KEY OR You can add a constraint ALTER TABLE

MySQL - Make a pair of values unique

非 Y 不嫁゛ 提交于 2019-11-27 11:56:14
问题 I have a table with two int values that are IDs. On their own these IDs can show up any number of times in the table, but together they should only ever appear once. Is there a way to make a pair of values unique and still allow the individual values to show up multiple times? As a follow up, if this is possible can the pair of values be used as a key? I currently have a 3rd column for a unique auto increment value for my key. 回答1: It's called a composite key. If you want to change your

MySQL - Meaning of “PRIMARY KEY”, “UNIQUE KEY” and “KEY” when used together while creating a table

て烟熏妆下的殇ゞ 提交于 2019-11-27 11:00:35
Can anyone explain about the purpose of PRIMARY KEY , UNIQUE KEY and KEY , if it is put together in a single CREATE TABLE statement in MySQL? CREATE TABLE IF NOT EXISTS `tmp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `tag` int(1) NOT NULL DEFAULT '0', `description` varchar(255), PRIMARY KEY (`id`), UNIQUE KEY `uid` (`uid`), KEY `name` (`name`), KEY `tag` (`tag`) ) ENGINE=InnoDB AUTO_INCREMENT=1 ; How do I convert this query to MSSQL? A key is just a normal index. A way over simplification is to think of it like a card catalog at a

Does an empty SQL table have a superkey? Does every SQL table?

大憨熊 提交于 2019-11-27 09:39:36
I know what the term "SuperKey" in SQL stands for, but I cannot understand a specific thing and I would like some help. In a table with no data , is there a superkey? In any table, will there always exists one? TL;DR "Superkey" is a RM (Relational Model of Data) term. There's no standard use in SQL. The superkeys of an SQL table might reasonably informally be said to be the column sets that you could declare primary key or unique not null , plus maybe {} when a table holds at most one row (although you can't declare it). "Reasonably informally" because SQL tables are not RM relations. But if a

MySQL: ALTER IGNORE TABLE ADD UNIQUE, what will be truncated?

旧时模样 提交于 2019-11-27 07:26:11
I have a table with 4 columns: ID, type, owner, description. ID is AUTO_INCREMENT PRIMARY KEY and now I want to: ALTER IGNORE TABLE `my_table` ADD UNIQUE (`type`, `owner`); Of course I have few records with type = 'Apple' and owner = 'Apple CO'. So my question is which record will be the special one to stay after that ALTER TABLE, the one with smallest ID or maybe the one with biggest as the latest inserted? Galz The first record will be kept, the rest deleted §§ : IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new

#1062 - Duplicate entry '' for key 'unique_id' When Trying to add UNIQUE KEY (MySQL)

南楼画角 提交于 2019-11-27 05:34:25
问题 I've got an error on MySQL while trying to add a UNIQUE KEY. Here's what I'm trying to do. I've got a column called 'unique_id' which is VARCHAR(100). There are no indexes defined on the table. I'm getting this error: #1062 - Duplicate entry '' for key 'unique_id' When I try to add a UNIQUE key. Here is a screenshot of how I'm setting it up in phpMyAdmin: Here is the MySQL query that's generate by phpMyAdmin: ALTER TABLE `wind_archive` ADD `unique_id` VARCHAR( 100 ) NOT NULL FIRST , ADD

Filter unique values from a JSON

橙三吉。 提交于 2019-11-27 02:20:03
问题 I am using angularjs for my page. I want to filter the values from the JSON object, So that no redundancy exists. But I didn't find any way to get the unique values from angular ng-repeat. Is there anyway to do that? Ok, Here is some description about the question. I have a JSON in this format. This JSON I am getting from a service. So we cant expect how the repeated data occure. result = [ { _id: "500004", subject: "Complete the task", date: "25 Aug, 2013" }, { _id: "500004", subject:

Is the Sql Server Unique Key also an Index?

懵懂的女人 提交于 2019-11-27 01:26:50
问题 I've got a column in a table (eg. UserName) which I want to make sure is unique. So I create a unique key for that column and call it IX_Users_UserName. Now, if I do lots of searching for users based on their username I want to make sure there is an index for that field. Do I need to create a separate index, or is the unique key also considered an index, just like the primary key is a clustered unique key? 回答1: Unique Key: Unique Key enforces uniqueness of the column on which they are defined

Unique keys in Entity Framework 4

拥有回忆 提交于 2019-11-26 22:39:37
An existing DB schema has unique, non-primary, keys, and some foreign keys that rely on them. Is it possible to define unique keys, which are not primary keys, in Entity Framework v4? How? Evandro Pomatti The Entity Framework 6.1 now supports uniques with both Data Annotations and Fluent API. Data Annotations ( Reference ) public class MyEntityClass { [Index(IsUnique = true)] [MaxLength(255)] // for code-first implementations public string MyUniqueProperty{ get; set; } } Fluent API ( Reference ) public class MyContext : DbContext { protected override void OnModelCreating(DbModelBuilder

How can I catch UniqueKey Violation exceptions with EF6 and SQL Server?

故事扮演 提交于 2019-11-26 19:33:58
问题 One of my tables have a unique key and when I try to insert a duplicate record it throws an exception as expected. But I need to distinguish unique key exceptions from others, so that I can customize the error message for unique key constraint violations. All the solutions I've found online suggests to cast ex.InnerException to System.Data.SqlClient.SqlException and check the if Number property is equal to 2601 or 2627 as follows: try { _context.SaveChanges(); } catch (Exception ex) { var