primary-key

adding column to all tables in a mysql database (unknown table names)

时光怂恿深爱的人放手 提交于 2019-12-03 06:50:43
I need to add a primary key to a set of tables in a given database. I do not know how many tables there will be or what their specific names are. They will all be of the for datatable_##, where ## will vary based everywhere my script will be run. To add the primary key, I am using this query: alter table datatable_?? add column ID int auto_increment not null first, add primary key (ID); So, I need this to run on every table in the database. It seems like I can do this in a PHP script or something, but is their a simpler way to do this just in sql script? If you want to fully script this, you

MySQL order by primary key

对着背影说爱祢 提交于 2019-12-03 06:45:25
Some SQL servers allow for a generic statement such as ORDER BY PRIMARY KEY . I don't believe this works for MySQL, is there any such workaround that would allow for automated selects across multiple tables or does it require a lookup query to determine the primary key? The workaround I have been working on involves calling a SHOW COLUMNS FROM before running the query. Is there a more efficient way of doing this? Can MySQL determine the primary key of a table during the select process? Update: There is no official way of doing this in MySQL or SQL in general as Gordon pointed out. SAP has

Creating django objects with a random primary key

对着背影说爱祢 提交于 2019-12-03 06:39:48
I'm working with an API that wants me to generate opaque "reference IDs" for transactions with their API, in other words, unique references that users can't guess or infer in any way. (is 'infer' proper english?) This is what I've hacked together currently: randomRef = randint(0, 99999999999999) while Transaction.objects.filter(transactionRef = randomRef).count(): randomRef = randint(0, 99999999999999) Transaction.objects.create(user=user, transactionRef=randomRef, price=999) unfortunately my database seems to be missing transactions at the moment. I've realized that my method isn't

Primary key versus key

霸气de小男生 提交于 2019-12-03 05:43:45
When creating a mysql dump containing the structure of my database, one of the tables shows the following: CREATE TABLE `completedTransactions` ( `paymentId` int(10) unsigned NOT NULL, `timestamp` int(15) unsigned NOT NULL, `actionTaken` varchar(25) NOT NULL, `response` varchar(255) NOT NULL, `responseCode` int(5) NOT NULL, PRIMARY KEY (`paymentId`,`timestamp`), KEY `paymentId` (`paymentId`), The primary key is what I was expecting, but I'm unsure what the last line is about? KEY `paymentId` (`paymentId`), Is this related to an index? Yes, the KEY keyword is just an alias for the INDEX keyword

How do I retroactively add a primary key to my table in rails?

孤者浪人 提交于 2019-12-03 05:22:08
I've created a table without a primary key (:id => false), but now it has come back to bite my ass. My app is already in production and I can't just drop it and recreate another one. Is there a way to run a migration to add another auto increment primary key column to my table? The command to add a primary key in a migration is: add_column :my_table, :id, :primary_key However, the wording of your question suggests that your table already has an auto-increment column. Unless I'm mistaken, there are several DBMS that do not allow more than one auto-increment column on a table. If you DO already

alter table drop column syntax error using sqlite

北战南征 提交于 2019-12-03 05:21:49
This is the schema of my table: create table LPCG(ID integer primary key, PCG text, Desc text, test text); I wish to drop the column "test", and hence use the command: alter table LPCG drop column test; This is the error message I get: Error: near "drop": syntax error Can someone please help me correct my error? An additional question is: I understand that ID is the primary key attribute. Would I be able to drop that column? If not, is there a workaround which anyone has used? Thanks in advance for any help. SQLite does not fully support ALTER TABLE statements . You can only rename table, or

SQL: set existing column as Primary Key in MySQL

前提是你 提交于 2019-12-03 04:41:15
问题 I have a database with 3 columns: id, name, somethingelse This table has no index set and i am getting "No index defined!" in phpmyadmin id is a 7digit alphanumeric value, unique to each row. I want to set Drugid to be the primarykey/index (i dont know the difference if there is one) Please explain in detail as i am new to this. Thank you. 回答1: Either run in SQL: ALTER TABLE tableName ADD PRIMARY KEY (id) ---or Drugid, whichever you want it to be PK or use the PHPMyAdmin interface (Table

SQL Server: drop table primary key, without knowing its name

微笑、不失礼 提交于 2019-12-03 04:14:59
问题 HI, Using: SQL Server Database: Northwind I'd like to drop a table primary key, without knowing the PK constraint name.. eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories' I can drop the primary key while knowing the primary key name: ALTER TABLE categories DROP CONSTRAINT PK_Categories; And I can also get the primary key name for the table by table name: select name from sysobjects where xtype =

Define a unique primary key based on 2 columns

泄露秘密 提交于 2019-12-03 04:14:12
问题 I would like to define a unique key for records based on 2 columns : 'id' and 'language' to let the user submits the following strings : id=1 language=en value=blabla english id=1 language=fr value=blabla french I tried to use set_primary_key and also add_index but it didn't work ( add_index :words, ["id", "language_id"], :unique => true ) I have the following model : class Word < ActiveRecord::Base belongs_to :dictionnary belongs_to :language attr_accessible :lang, :rev, :value, :dictionnary

JPA: is @PrimaryKeyJoinColumn(…) the same as @JoinColumn(…, insertable = ?, updatable = ?)?

≡放荡痞女 提交于 2019-12-03 02:46:59
Can you derive from the JPA spec, if @PrimaryKeyJoinColumn(...) , which doesn't have the insertable and updatable parameters, is the same as @JoinColumn(..., insertable = false, updatable = false) or @JoinColumn(..., insertable = true, updatable = true) when used on regular (non-inheritance) associations? Should they be interchangable? What are the insertable and updatable properties set to? Are they set to anything at all? Note, I'm only targeting the read-only attribute that both (seem to) implement... I'm getting rather inconsistent mapping exception with EclipseLink and Hibernate... Here's