How to add auto-increment to column in mysql database using phpmyadmin? - mysql

I've been trying to add auto-increment to one of my columns (basically an ID) but I can't find the auto-increment option for my column. Any idea where it is?

To use the GUI:
Click the STRUCTURE tab to see the list of existing fields
To set a field as the PRIMARY FIELD, click the gold key -- it will turn silver.
To set a field (usually the same field) as auto-increment:
a. Click CHANGE for that field
b. Look to the far right and checkmark the AI box
c. Click SAVE button

You can add it like this
ALTER TABLE your_table
MODIFY some_column INT NOT NULL AUTO_INCREMENT;

A couple quick points based on recent experience:
To the original question, how to select auto-increment with phpmyadmin, it's the small AI check box on the change screen for a field name.
When I tried the "ALTER TABLE tablew_name CHANGE id id BIGINT(20) NOT NULL AUTO_INCREMENT;" solution above, phpmyadmin gave me an error message saying the field had to have a key. I selected a Unique key and the error message went away and the field now auto increments.

This wont work if there are any foreign keys defined, and that is very likely for id fields.
use:
ALTER TABLE tablew_name CHANGE id id BIGINT(20) NOT NULL AUTO_INCREMENT;
instead

The SQL script is correct
ALTER TABLE your_table MODIFY some_column INT NOT NULL AUTO_INCREMENT;
but if you try so make before in the visual mode, with the mysql version 4.7.4, in the struct of the table
Appear when you create the table one option to say "A_I" , if you put your mouse appear message with AUTO_INCREMENT (The version of the foto is in Spanish version)

If you are using xampp:
step 1: Go to the structure tab.
step 2: click on change tab for the column you want to auto increment. In my case, i am changing for id column.
step 3: check the AI(autoincrement ) column
And you are done!

Related

How to change column type in mySQL from DATE to DATETIME

I'm trying to change a column from DATE to DATETIME in mySQL using sqlfiddle. But I'm just getting syntax errors, when looking at other stack overflow answers it seems like this method should work? What am I doing wrong? Is the only way (is it safer?) to add a new column, update new column with old and drop the old one?
My current method (assume there is data already in the table after creation)
CREATE TABLE re ( file_id INT NOT NULL AUTO INCREMENT PRIMARY KEY, mydate DATE NOT NULL);
ALTER TABLE re alter column "mydate" DATETIME NOT NULL
The above doesn't work on sqlfiddle but it's the most common answer, other than the previous way. Am I doing something wrong?
There is, at least, one errors in you question:
AUTO INCREMENT should have an underscore between AUTO and INCREMENT.
The ALTER TABLE statement should be written with MODIFY COLUMN, and not with ALTER COLUMN. (This is a possible bug in MySQL?)
see: DBFIDDLE
I created a bug-report for this: https://bugs.mysql.com/bug.php?id=109461
P.S.: the time part of the DATETIME field will be set to '00:00:00', see: DBFIDDLE
EDIT: Response from [22 Dec 13:36] MySQL Verification Team
Thank you for your bug report.
However, it is not a bug.
Our Reference Manual clearly states that ........ ALTER COLUMN
....... and ....... MODIFY COLUMN ..... have two very different
scopes, with the first one being reduced to the changes for only
couple of keywords.
Re-reading the docs, I read: ALTER: "Used only to change a column default value."
and MODIFY COLUMN
Can change a column definition but not its name.
More convenient than CHANGE to change a column definition without renaming it.
With FIRST or AFTER, can reorder columns.

I cannot select a column to add a Referenced column in MySQL workbench when creating a Foreign Key

I am trying to create a foreign key in MySQL Workbench and when I try and add a Reference Column it doesn't tick the box beside the column. This is happening for the second Foreign Key that I'm creating in this table. I was able to create one before but the second one doesn't seem to work. Here is what the first FK looks like:
The second one looks like this:
As you can see there is no tick in the box and when I select the reference column it will reset. Is this a bug or have I done something wrong?
I realised that the data types were not the same. In the table that i was creating the FK the data type was int(11) however in the referencing table it was tinyint(4).

Unique on Compound Columns

I am trying to ensure that the values in RESTAURANTID and TABLENUMBER, together, are Unique using PowerDesigner (12.5). I've tried creating an alternate key which resulted in the following in my .sql file:
create table TABLES
(
TABLEID int not null,
RESTAURANTID int not null,
TABLENUMBER int not null,
primary key (TABLESID),
key AK_mykey (RESTAURANTID, TABLENUMBER)
);
However with this, I can still enter identical values for RESTAURANTID and TABLENUMBER more than once.
I used this http://www.tek-tips.com/viewthread.cfm?qid=403554 to create the alternate key in PowerDesigner.
Would anyone know the proper way to achieve this in PowerDesigner?
Note: This isn't a duplicate of the question posted above as I'm looking for a way to achieve this in PowerDesigner without having to edit the generated sql file afterwards.
The unique property for keys (other than primary) in MySQL is stored as an extended attribute on the key.
You can modify it by displaying, and going to the MySQL tab in the Key properties dialog.
Or, in Model>Keys, you can use the Customize Columns and Filter button to show the Ext Unique (extended) property in the list of keys, so that you can set this unique property on several keys at once.
Or, you can create your own copy of the MySQL DBMS, and edit it. Under Profile>Key (using the right-click), add an event handler Initialize with the following Event Handler Script, so that each new key has ExtUnique set:
Function %Initialize%(obj)
obj.setextendedattribute "ExtUnique",true
%Initialize% = True
End Function
You are just adding a normal index. What you need is a unique index. Just replace key AK_mykey (RESTAURANTID, TABLENUMBER) with unique key AK_mykey (RESTAURANTID, TABLENUMBER) in your query and you are done.
In Power Designer:
Open the "TABLES" table properties (right click -> properties)
Keys tab
Insert row (name it to "AK_mykey")
Apply (previously inserted row saved)
On the "AK_mykey" row: right click -> properties
Columns tab
Add columns button
Select the required columns (RESTAURANTID, TABLENUMBER)
OK, OK, OK buttons
+1 You can check the result in table properties preview tab.
alter table TABLES
add unique AK_mykey (RESTAURANTID, TABLENUMBER);

How to create an primary key in phpmyadmin?

I have two tables, the first one is called BOOKSNAME and the second is called AUTHORNAME.
I have made the ID column for the BOOKSNAME table as the primary key (BIGINT auto increment) then my AUTHORNAME table has NAME and ADDRESS as it's columns but I have no ID/Primary key on this.
I want to make a relation between them by primary key, if anyone has an example of how this is achieved, could they share it?
What you need to do is add a new column to your AUTHORNAME table called AuthorID or something similar, you can select 'Primary' on a drop down list under the INDEX when adding a new column to your table, and you also want to tick the A_I box (auto increment).
If you are unsure how to add a new column, follow these steps:
Firstly go to your phpmyadmin, and select the table in which you need to add this new column/ primary key to, and select the structure tab.
Here you will see the current column headings that your table already has, so from here you want to look towards the bottom of that list of columns where it will display options on how to add a new column to your table.
Ideally, you want to add the ID at the beginning of the table, as it will make it look far more structured and easier to read.
From here you want to enter the name of the column, in your case AuthorID, the type will be an INT, and you will want to make your index as PRIMARY, then lastly you will need to tick the A_I / Auto increment box (This may appear differently depending on what version of phpmyadmin you are running).
If you then want to make relations between the two tables, you can use something called JOINS, if you do a simple Google search, you can find many guides on how these are carried out.
Also add integer primary key to the author table, next you can have author_id field to another table, which is same datatype as authors table primary key, and store author id to that field.
Also if you use Innodb engine you can add foreign key constraint, it is very useful, for more see documentation: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Auto-increment primary key in SQL tables

Using Sql Express Management Studio 2008 GUI (not with coding), how can I make a primary key auto-incremented?
Let me explain: there is a table which has a column named "id" and the items of this column are set to be primary keys. I want to make this column auto-incremented, but how?
Cheers
Presumably you are in the design of the table. If not: right click the table name - "Design".
Click the required column.
In "Column properties" (at the bottom), scroll to the "Identity Specification" section, expand it, then toggle "(Is Identity)" to "Yes".
Although the following is not way to do it in GUI but you can get autoincrementing simply using the IDENTITY datatype(start, increment):
CREATE TABLE "dbo"."TableName"
(
id int IDENTITY(1,1) PRIMARY KEY NOT NULL,
name varchar(20),
);
the insert statement should list all columns except the id column (it will be filled with autoincremented value):
INSERT INTO "dbo"."TableName" (name) VALUES ('alpha');
INSERT INTO "dbo"."TableName" (name) VALUES ('beta');
and the result of
SELECT id, name FROM "dbo"."TableName";
will be
id name
--------------------------
1 alpha
2 beta
Right-click on the table in SSMS, 'Design' it, and click on the id column. In the properties, set the identity to be seeded # e.g. 1 and to have increment of 1 - save and you're done.
for those who are having the issue of it still not letting you save once it is changed according to answer below, do the following:
tools -> options -> designers -> Table and Database Designers -> uncheck "prevent saving changes that require table re-creation" box -> OK
and try to save as it should work now
I don't have Express Management Studio on this machine, so I'm going based on memory. I think you need to set the column as "IDENTITY", and there should be a [+] under properties where you can expand, and set auto-increment to true.
I think there is a way to do it at definition stage like this
create table employee(
id int identity,
name varchar(50),
primary key(id)
)..
I am trying to see if there is a way to alter an existing table and make the column as Identity which does not look possible theoretically (as the existing values might need modification)