Multi-Column Primary Key in MySQL 5

Quoted from the CREATE TABLE Syntax page: A PRIMARY KEY can be a multiple-column index. However, you cannot create a multiple-column index using the PRIMARY KEY key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate PRIMARY KEY(index_col_name, …) clause. Something like this can be … Read more

Hibernate/persistence without @Id

If there’s a combination of columns that makes a row unique, model a primary key class around the combination of columns. If there isn’t, you’re basically out of luck — but you should reexamine the design of the view since it probably doesn’t make sense. There are a couple different approaches: @Entity public class RegionalArticle … Read more

MySQL duplicate entry error even though there is no duplicate entry

Your code and schema are OK. You probably trying on previous version of table. http://sqlfiddle.com/#!2/9dc64/1/0 Your table even has no UNIQUE, so that error is impossible on that table. Backup data from that table, drop it and re-create. Maybe you tried to run that CREATE TABLE IF NOT EXIST. It was not created, you have … Read more

MySQL InnoDB: autoincrement non-primary key

Yes you can. You just need to make that column be an index. CREATE TABLE `test` ( `testID` int(11) NOT NULL, `string` varchar(45) DEFAULT NULL, `testInc` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`testID`), KEY `testInc` (`testInc`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; insert into test( testID, string ) values ( 1, ‘Hello’ ); insert into test( … Read more

Define a unique primary key based on 2 columns

add_index :words, [“id”, “language_id”], :unique => true It should work. Maybe you have already some non-unique data in your db and index can’t be created? But (as @Doon noticed it will be redundant since id is always unique). So you need create primary key on two columns. To define 2 column primary key in rails … Read more

Add primary key to PostgreSQL table only if it does not exist

You could do something like the following, however it is better to include it in the create table as a_horse_with_no_name suggests. if NOT exists (select constraint_name from information_schema.table_constraints where table_name=”table_name” and constraint_type=”PRIMARY KEY”) then ALTER TABLE table_name ADD PRIMARY KEY (id); end if;

What are the pros and cons for choosing a character varying data type for primary key in SQL? [closed]

The advantages you have for choosing a character datatype as a primary key field is that you may choose what data it can show. As an example, you could have the email address as the key field for a users table. The eliminates the need for an additional column. Another advantage is if you have … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)