Adding A Primary Key to an Existing Table

Adding A Primary Key to an Existing Table

Been trying to add a primary key to a MySQL table using Java for two days with no success. I'm new to Java and JDBC so bear patience with me. It connects to the database without problem so I know that's not the issue..so I suspect my statment syntax but have been unable to find a good example.

My code sample:

try
            {
                alterStatement = connection.createStatement();
                out.println();
                alterStatement.executeUpdate("ALTER TABLE machine ADD PRIMARY KEY (SerialNo)");
                out.println("Primary key added");
            }
            catch (SQLException s)
            {
                out.println("Error inserting primary key");

            }

I get the message: 'error inserting primary key'. Help, please and thank you.

View Answers

July 29, 2011 at 12:19 PM

Hello Friend,

You code works fine here. Print the SQLException object to check which type of error occurs. Then let me know, we will help you.

Thanks


July 29, 2011 at 6:36 PM

Do you mean use 'printStackTrace()'? (I'm clueless about SQL excpetion handling to be honest 8-P )

Did that and got this response...

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ALTER command denied to user 'root'@'localhost' for table 'machine'

Now if that thing is basically telling me "access denied" then I know the computer just doesn't like me because there shouldn't be anything wrong with my account; it was set up properly, performs all the other queries and naturally has full privileges. Meh.

I ended up dropping the table and creating it from scratch with the primary key in the create table statement because I was working under deadline yesterday but I'd still like to know what went wrong.









Related Tutorials/Questions & Answers:
Adding A Primary Key to an Existing Table
Adding A Primary Key to an Existing Table   Been trying to add a primary key to a MySQL table using Java for two days with no success. I'm new...(); alterStatement.executeUpdate("ALTER TABLE machine ADD PRIMARY
Mysql Alter Table Primary Key
Mysql Alter Table Primary Key       Mysql Alter Table Primary Key is used to remove the primary key from the table and add primary key to the existing other column of table
Advertisements
SQL Alter Table Primary Key
SQL Alter Table Primary Key       Alter a Table Primary Key in SQL modifies the existing table and adds a primary key. Create Table Stu_TableADS_TO_REPLACE_1 SQL
Mysql add primary key
data type of Table named employees after adding primary key:ADS... rows in set (0.00 sec) Query to add primary key into the Table...'. mysql> alter table employees -> add primary key (empid
changing primary key of parent table
changing primary key of parent table  I want to change one primary key from a table where primary key is auto created? How can I do that? I have tried UPDATE person SET personid=3494 WHERE personid=8483; and i get the following
how tohow to create a table with primary key and foreign keyhow to create a table with primary key and foreign key
how tohow to create a table with primary key and foreign keyhow to create a table with primary key and foreign key  how to create a table using... key is a field (or fields) that points to the primary key of another table
SQL Alter Table Primary Key tutorial
SQL Alter Table Primary Key       Alter a Table Primary Key in SQL modifies the existing table and adds a primary key. Create Table Stu_TableADS_TO_REPLACE_1 SQL
can we update table in hibernate with out primary key in table?
can we update table in hibernate with out primary key in table?  can we update table in hibernate with out primary key in table
primary key and unique key
primary key and unique key  Whats is mean by primary key and unique key
SQL Primary Key
in a database table. The Primary Key of the Table does not include NULL values. In SQL  Each Table should have primary key and have only one primary key... 'SQL Primary Key'. To understand and grasp this example, we create a table 'users
primary key and unique key
primary key and unique key  hello, What's the difference between a primary key and a unique key?   hii,ADS_TO_REPLACE_1 Unique key.... Primary key: columns entry of duplicate as well as null values are restricted
Mysql Alter Column Primary Key
;    Mysql Alter Column Primary Key is used to modify table and redefine the Primary Key Column in a table. Understand with ExampleADS... Key'. To understand an example, we create a table employees whose primary key
SQL PRIMARY KEY Constraint
a unique values. It should contain NULL values. The Table contain a primary key and each table have only primary key. Understand with ExampleADS... SQL PRIMARY KEY Constraint     
What is a "primary key"?
). A primary key can consist of one or more fields on a table. When multiple fields...What is a "primary key"?  What is a "primary key"?   Hi, Here is the answer,ADS_TO_REPLACE_1 A primary key is used to uniquely identify
SQL Alter Table
. The modification in table relates to add columns, rename column and add primary key... as primary key in the table Stu. ADS_TO_REPLACE_5 Alter Table Stu add primary key (id) ; Describe Stu
composinte primary key in hibernate
composinte primary key in hibernate  I am facing following problem, Inside the database(mssql), i have created a table without primary key... primary key with all the columns of the table and hibernate s/w has created two pojo
difference between a primary key and a unique key?
. But by default primary key creates a clustered index(physical sorting of a database table...difference between a primary key and a unique key?  What's the difference between a primary key and a unique key?   Hi, The column holding
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable...(100,60,1300,600); add(pane); //Action Listener for button //for adding new...); } } }); //end action listener //table addition setVisible(true); setSize
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable...(100,60,1300,600); add(pane); //Action Listener for button //for adding new...); } } }); //end action listener //table addition setVisible(true); setSize
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable...(100,60,1300,600); add(pane); //Action Listener for button //for adding new...); } } }); //end action listener //table addition setVisible(true); setSize
SQL Auto increment Primary Key
primary key field in the table "employee_roseindia"... SQL Auto increment Primary Key       SQL Auto increment Primary Key is used to allow a unique
SQL PRIMARY KEY Constraint
values. It should contain NULL values. The Table contain a primary key and each table have only primary key. Understand with ExampleADS_TO_REPLACE_1... to create a table 'Stu_Class_10'.The following SQL creates a PRIMARY KEY
Create primary key using hibernate
Create primary key using hibernate  How to create primary key using hibernate?   The id element describes the primary key for the persistent class and how the key value is generated. ADS_TO_REPLACE_1   
hibernate annotations with composite primary key
to use hibernate annotations in case of composite primary key in one table. I tried like this, I have created a table without primary key. By default mssql has created a composite primary key with all the columns of the table. table
JDBC insert that returns primary key
JDBC insert that returns primary key  How to write code for jdbc insert that returns primary key? Help me fast Thanks   Hi, Following code can be used: //Add record into database String queryInsert = "insert
Difference between not null and primary key in mysql
Difference between not null and primary key in mysql  Explain not null and primary key in mysql
How to create primary key using hibernate?
How to create primary key using hibernate?  Hi, How to create primary key using hibernate? Thanks
Mysql Alter Foreign Key
;  A Foreign Key is a field that marks to the primary key of another  table. The use of the foreign key is used to ensure referential... create a table 'publisher'. The Table 'publisher' has primary field name
Define sequence generated primary key in hibernate
Define sequence generated primary key in hibernate  How to define sequence generated primary key in hibernate?   Use value "sequence...="table">SEQUENCE_NAME</param> <generator> </id>
ModuleNotFoundError: No module named 'django-uuid-primary-key'
ModuleNotFoundError: No module named 'django-uuid-primary-key'  Hi...: No module named 'django-uuid-primary-key' How to remove the ModuleNotFoundError: No module named 'django-uuid-primary-key' error? Thanks   
Mysql Alter Autoincrement
; existing table and add autoincrement feature property to the primary key... (0.00 sec) Query to alter auto increment to the primary key of the Table...' definition and add autoincrement to the primary key of the table 'employees
How to auto increment primary key in mysql...
How to auto increment primary key in mysql...  How to auto increment primary key in mysql... and one more thing i need to get auto increment value in front end each time for inserting the value[JSP
Foreign key update table
Foreign key update table  How to update table that has the foreign key in SQL..?   ALTER TABLE YourTable ADD CONSTRAINT FK_YourForeignKey FOREIGN KEY (YourForeignKeyColumn) REFERENCES YourPrimaryTable
inserting into mysql database with uid to be primary key and autoincrement
. In the database table, set the uid as the primary key and auto-increment. Then, whenever...inserting into mysql database with uid to be primary key and autoincrement  hello, i am new to jsp and facing problem in creating a database entry
query regarding auto increment primary key
query regarding auto increment primary key  i want to use s1,s2.... as my auto increment primary key.cant i do this in mysql
Mysql Alter Table Identity
INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (ID); After alter the table...; Mysql Alter Table Identity is used to change the existing structure of table and add ID column as identity key that is used to uniquely define
Adding a flex table in another flex table column
Adding a flex table in another flex table column  how to add a flex table in another flex table column
Adding button to each row for the table and adding row to another table
Adding button to each row for the table and adding row to another table  Hi I need to add button to each line in the table(Table data is retrived... row of the table
Adding checkbox in table using that delete table rows
Adding checkbox in table using that delete table rows  I need coding for how to add checkbox in table,the table data from database.Using that checkbox select more than than one rows,and using delete button i want to delete
Adding a Reports tap in table pool
Adding a Reports tap in table pool  strong textHow to write a code... that is written in jsf. I have to edit a jsp file that is written jsf there is a table there and I have to include Reports tap in that table so that whne the user
Mysql Alter Table Syntax
fieldname and datatype respectively. The table have a Primary Key column 'empid...;  Mysql Alter Table is used to redefine the existing table...;date date ->PRIMARY KEY(Empid) ->
how to create a new table for the already existing user in the website
how to create a new table for the already existing user in the website  haiii....i want the code in jsp "how to create a new database for every user that has been already existing in the website?"....plzzzz help me
SQL Alter Table Syntax
Table Name  | ADD [CONSTRAINT [symbol]] PRIMARY KEY[index_type] (index... table structure and add constraint on Primary Key. Alter Table Name ...; SQL Alter Table Syntax modifies the existing table definition
How to copy existing column along with data and column name into another existing table
How to copy existing column along with data and column name into another existing table  i want to copy the existing column along with data and column name also into another existing table... For ex; TableA : Address email
alter table add foreign key mysql
alter table add foreign key mysql  Hi, How to add foreign key in mysql using the query 'alter table add foreign key mysql' Thanks   Hi, We can use the following query: ALTER TABLE emp_address ADD FOREIGN KEY (emp_id
How do we create new table apart from mapping with existing table ?
How do we create new table apart from mapping with existing table ?  Hi, How do we create new table apart from mapping with existing table ? Thanks
Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.
an entity bean's primary key and object identity. Prev Chapter 8... and incorrect statements or examples about an entity bean's primary key and object... of an entity object on the basis of its PRIMARY KEY
Mysql Alter Rename Table
'employees' have a Empid as Primary Key.  Query to create table...; Mysql Alter Rename Table is used to redefine and rename the existing...), ->Empname varchar(60), ->date date ->PRIMARY KEY
Mysql Alter Rename Table
of existing table. Understand with ExampleADS_TO_REPLACE_1 The Tutorial illustrate... a Empid column as primary key.     ADS_TO_REPLACE_2    ...;date date ->PRIMARY KEY(Empid) ->); Query
Adding a New Column Name in Database Table
Adding a New Column Name in Database Table  ... are going to learn about adding a new column in database table. Sometimes it happens... we are going to add columns to an existing database table. After establishing

Ads