unique-key

SQL Unique Key Syntax

♀尐吖头ヾ 提交于 2019-12-05 14:36:29
Very basic question; I'm very new to SQL and trying to decipher an example data base. In the below create table code, why does the define primary key syntax reference only the 'id' column once in parentheses but the unique key definition references the 'category' column twice? both before and within the parentheses. Seems like there is a simple answer to this but cant track one down: CREATE TABLE `categories` ( `id` SMALLINT NOT NULL AUTO_INCREMENT, `category` VARCHAR(30) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `category` (`category`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; It is the key name,

MyISAM unique keys being cut off at 64 bytes, causing collisions

故事扮演 提交于 2019-12-05 08:25:30
I've got a MySQL table that stores urls as unique keys. I'm starting to get collisions on my keys because it seems the keys themselves are only the first 64 bytes (or characters if you prefer, its a latin-1 collated) of any url. So if a url is over 64 characters and I've already got a similar url it throws an error. For example: SELECT l.link_id FROM mydb.links l WHERE url = 'http://etonline.com/tv/108475_Charlie_Sheen_The_People_Have_Elected_Me_as_Their_Leader/index.html' Throws this error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'http://etonline.com/tv/108475

SQL Server 2005 unique key with null value

倖福魔咒の 提交于 2019-12-05 05:25:59
I have a table in SQL Server 2005 with a foreign key, and I want that foreign key to be a unique value or null. I've set it up as a unique key, but it won't allow me to have multiple nulls in the same table. Is it possible to do what I want? Thomas This is a long time complaint about SQL Server's Unique constraints/indexes. The best solution is to create a view with schemabinding and then put a unique index on that column: Create View dbo.MyUniqueColView With SchemaBinding As Select MyColToBeUnique From MyTable Where MyColToBeUnique Is Not Null GO Create Unique Clustered Index IX_MyTable

How to find next free unique 4-digit number

泪湿孤枕 提交于 2019-12-04 21:51:31
In my db application I have a requirement for a unique, 4-digit number field for each customer. Up until 9999 I can just use autoincrements, but after that I will have to reuse numbers of customers that have been deleted (there won't be more than 5000 customers at a given time but there may be more than 9999 customers over the lifetime of the system). Question 1: Is there a (My)SQL statement to find the next reusable free number? Question 2: If I get the number, assign it to a new customer and save the customer all in one transaction, similar transactions taking place at the same time will be

Multiple Unique Columns in SQLite

耗尽温柔 提交于 2019-12-04 20:38:22
I am trying to create a table where I need it to NOT allow rows where 3 fields are the same. When I create the table in Python using SQLLite, I use the follow, but I hardly get any results at all. It usually stops after writing 2 records, so something is obviously believing its duplicated. CREATE TABLE CorpWalletJournal ( date INT, refID INT, refTypeID INT, ownerName1 TEXT, ownerID1 INT, ownerName2 TEXT, ownerID2 INT, argName1 TEXT, argID1 ID, amount INT, balance INT, reason TEXT, accountKey INT, UNIQUE (ownerID1, ownerID2, accountKey, argID1) ); So, I would like the database to NOT allow

MySQL UUID() when not unique?

ⅰ亾dé卋堺 提交于 2019-12-04 16:56:05
What happens when a UUID() generated by MySQL is not unique? If this is for a column that is a primary key, does MySQL error out, or does it try generating another UUID until a truly unique one is found? Well, if you call UUID() twice and get the same results, the most problematic thing would be that "stuff is broken" (tm). It's supposed to be unique and it should be always, as far as I know. There would be no "regenerate" code available: the function is designed to create unique keys even across computers, so how could it even know its result was not unique? from http://dev.mysql.com/doc

MySql - Is primary key unique by default?

て烟熏妆下的殇ゞ 提交于 2019-12-04 15:08:03
问题 If I define a column as a primary key in MySql, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)? I saw this question What is the difference b/w Primary Key and Unique Key that explain the difference between the two but doesn't exactly answer my question. Does PK is UK by default or I need to explicitly define it. 回答1: Primary key is always unique in every SQL. You dont have to explicitly define it as UNIQUE. On a side note: You

Add new keys to a dictionary while incrementing existing values

Deadly 提交于 2019-12-04 04:16:56
I am processing a CSV file and counting the unique values of column 4. So far I have coded this three ways. One uses "if key in dictionary", the second traps the KeyError and the third uses "DefaultDictionary". For example (where x[3] is the value from the file and "a" is a dictionary): First way: if x[3] in a: a[x[3]] += 1 else: a[x[3]] = 1 Second way: try: b[x[3]] += 1 except KeyError: b[x[3]] = 1 Third way: from collections import defaultdict c = defaultdict(int) c[x[3]] += 1 My question is: which way is more efficient... cleaner... better... etc. Or is there a better way. Both ways work

MySql - Is primary key unique by default?

北战南征 提交于 2019-12-03 10:26:20
If I define a column as a primary key in MySql, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)? I saw this question What is the difference b/w Primary Key and Unique Key that explain the difference between the two but doesn't exactly answer my question. Does PK is UK by default or I need to explicitly define it. Primary key is always unique in every SQL. You dont have to explicitly define it as UNIQUE. On a side note: You can only have onePrimary key in a table and it never allows null values. Also you can have only one primary

Find or insert based on unique key with Hibernate

折月煮酒 提交于 2019-12-03 03:47:30
问题 I'm trying to write a method that will return a Hibernate object based on a unique but non-primary key. If the entity already exists in the database I want to return it, but if it doesn't I want to create a new instance and save it before returning. UPDATE: Let me clarify that the application I'm writing this for is basically a batch processor of input files. The system needs to read a file line by line and insert records into the db. The file format is basically a denormalized view of