primary-key

Can I use VARCHAR as the PRIMARY KEY?

爱⌒轻易说出口 提交于 2019-11-27 07:45:18
I have a table for storing coupons/discounts, and I want to use the coupon_code column as the primary key, which is a VARCHAR . My rationale is that, each coupon will have a unique code, and the only commands I will be running are SELECT ... FROM ... WHERE coupon_code='..' I won't be doing any joins or indexing, and I don't see there ever being more than a few hundred entries in this table. It seems to me that this will be OK, but I don't know if there is anything I'm missing/not thinking about. Of course you can, in the sense that your RDBMS will let you do it. The answer to a question of

How to make a primary key start from 1000?

人走茶凉 提交于 2019-11-27 07:13:27
create table tablename ( id integer unsigned not null AUTO_INCREMENT, .... primary key id ); I need the primary key to start from 1000. I'm using MySQL. davidosomething If your table has already been created with an auto-increment. so you can use ALTER TABLE tbl AUTO_INCREMENT = 1000; otherwise put the AUTO_INCREMENT = 1000; in your CREATE TABLE it goes before the final ); You can use ALTER TABLE to accomplish this: ALTER TABLE tablename AUTO_INCREMENT = 1000; If you want it as part of the CREATE TABLE statement, just put it after the table definition: CREATE TABLE tablename ( ... ) ENGINE

Does MS Access suppress primary key violations on Inserts?

二次信任 提交于 2019-11-27 07:00:43
问题 I am in the process of re-writing an MS Access database to SQL server and have found an strange issue in Access that I am hoping someone can help with. I have a table let's call it 'Main' with a Primary Key on the Account that is indexed and doesn't allow for duplicates. Seems simple enough but my issue is occurring when data is getting Inserted. My INSERT query is (the number of fields have been limited for brevity) INSERT INTO Main (Account, SentDate, Amount) SELECT C.Account, C.SentDate, C

rails3 bigint primary key

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:48:43
问题 I would like to create a bigint (or string or whatever that is not int ) typed primary key field under Rails 3. I have a given structure of data, for example: things ------ id bigint primary_key name char(32) The approach I'm currently trying to push: create_table :things, :id => false do |t| # That prevents the creation of (id int) PK t.integer :id, :limit => 8 # That makes the column type bigint t.string :name, :limit => 32 t.primary_key :id # This is perfectly ignored :-( end The column

Adding Item with Many-to-Many Relationship In Entity Framework

故事扮演 提交于 2019-11-27 06:47:50
问题 I am getting a primary key violation error when I attempt to add an item with a many-to-many relationship: I have two classes - Articles and Tags which have a many-to-many relationship : public class Article { public int ID { get; set; } public string Text { get; set; } public ICollection<Tag> Tags { get; set; } } public class Tag { [Key] public string UrlSlug { get; set; } public string Name { get; set; } public ICollection<Article> Articles{ get; set; } } When I add a new Article I allow

Alter Column datatype with primary key

青春壹個敷衍的年華 提交于 2019-11-27 06:46:48
问题 I have a ReferenceID varchar(6) column in over 80 different tables. I need to extend this to a varchar(8) throughout the db following a change implemented by the government organisation that assigns the IDs. I was hoping to declare a cursor to get the table names as follows: DECLARE @TableName AS VARCHAR(200) DECLARE TableCursor CURSOR LOCAL READ_ONLY FOR SELECT t.name AS TableName FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id WHERE c.name = 'ReferenceID' OPEN TableCursor

make an ID in a mysql table auto_increment (after the fact)

旧城冷巷雨未停 提交于 2019-11-27 06:17:59
I acquired a database from another developer. He didn't use auto_incrementers on any tables. They all have primary key ID's, but he did all the incrementing manually, in code. Can I turn those into Auto_incrementers now? Wow, very nice, thanks a ton. It worked without a hitch on one of my tables. But a second table, i'm getting this error...Error on rename of '.\DBNAME#sql-6c8_62259c' to '.\DBNAME\dealer_master_events' Bill Karwin For example, here's a table that has a primary key but is not AUTO_INCREMENT : mysql> CREATE TABLE foo ( id INT NOT NULL, PRIMARY KEY (id) ); mysql> INSERT INTO foo

Primary key in MySQL: INT(n) or UUID as varchar(36)

扶醉桌前 提交于 2019-11-27 06:15:44
问题 Does it make sense to use UUID as primary key in MySQL? What would be pros and cons of using UUID instead of regular INT, beside trouble of hand querying? 回答1: The major downside of UUIDs is that you have to create them beforehand if you want to refer back to the record for further usage afterwards (ie: adding child records in dependent foreign keyed tables): INSERT INTO table (uuidfield, someotherfield) VALUES (uuid(), 'test')); will not let you see what the new UUID value is, and since you

How to reset the primary key of a table?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 05:12:52
问题 In my table tbphotos I had a 100 records. I then deleted all the records and now that I want to restart data entry I see that my primary key doesn't start from 1, but it starts from 101, Is there any way to reset the primary key? I am using MySQL administrator account. 回答1: alter table foo AUTO_INCREMENT = 1 回答2: You can reset the auto-increment like this: ALTER TABLE tablename AUTO_INCREMENT = 1 But if you are relying on the autoincrement values, your program is very fragile. If you need to

Should I have a dedicated primary key field?

試著忘記壹切 提交于 2019-11-27 04:48:59
I'm designing a small SQL database to be used by a web application. Let's say a particular table has a Name field for which no two rows will be allowed to have the same value. However, users will be able to change the Name field at any time. The primary key from this table will be used as a foreign key in other tables. So if the Name field was used as the primary key, any changes would need to be propagated to those other tables. On the other hand, the uniqueness requirement would be handled automatically. My instinct would be to add an integer field to act as the primary key, which could be