I have a SQL database with a table inside called members and inside that there are some columns, one being an ID which auto-increments.
However I have done a few tests, and the auto-increment does work. But even after deleting the tests the auto-increment will not start from 0 again.
How do I make it will start back from 0 rather than carry on from about 17 or something...
EDIT:
I have worked out the answer:
In the "Operations" tab in phpMyAdmin there is a section called Table Options.
In there you can edit where the auto-increment continues from.
Assuming You're using MySQL:
To reset the next value of *auto_increment* column, you need to use ALTER TABLE statement in the following form:
ALTER TABLE my_table AUTO_INCREMENT=123
(Where "123" is the new next value)
If u are using postgresql u have following sql statement to alter sequence
ALTER SEQUENCE table_name_id_seq RESTART WITH 1
for example, if u have table called users, then u should do
ALTER SEQUENCE users_id_seq RESTART WITH 1
where 1 is the new sequence.
Related
I cannot change the column name in a mysql database I created.
I have tried the following commands, and none of them appear to work.
alter table (mytablename) CHANGE COLUMN (oldcolumnname) (newcolumnname) varchar(120);
alter table (mytablename) RENAME COLUMN (oldcolumnname) (newcolumnname) varchar(120);
ALTER TABLE (mytablename) CHANGE (oldcolumnname) (newcolumnname) varchar(120);
Where (mytablename) is the name of the table that I created, (oldcolumnname) is the original column name, and (newcolumnname) is the new column name.
This is a simple to-do list I created to learn MySQL with the following items:
id
todo
completed
1
Prepare for Take Off
Yes
2
Learn some MySQL
Yes
3
Remember that damn semicolon
No
In this case, I am trying to alter the column 'todo' to say either 'To Do' or 'To-Do' but every time I try these commands. I keep getting the famous "Check your SQL version manual".
Any hints as to what I might be doing wrong? TIA!
I have reviewed multiple tutorial websites and even reviewed another StackOverflow question
UPDATE
The ultimate solution was two-fold. First, I needed to use the TO phrase between the column names. Second, the column names do not like special characters.
The query that ultimately worked was:
ALTER TABLE mytablename CHANGE COLUMN todo TO ToDo
You should keep the column named todo; you can always change the output when you select like:
select id, todo as 'To Do', completed from ...
If you feel you really must include a space or - in the column name, in mysql you can use arbitrary identifiers that have not-usually allowed characters by enclosing them in backticks:
alter table ... rename column todo to `To Do`
but then every time you reference the column in sql you will need to enclose it in backticks:
select id,`To Do`,completed from ... where `To Do` like '%learn%'
I have two tables called HRData and HRDataHistory. HRDataHistory has the same structure as HRData except the first column is an autoincrement field and the last column is a DateTime field.
HRData has a trigger:
CREATE TRIGGER [HR].[HRData_History]
ON [HR].[HRData]
AFTER INSERT, UPDATE
AS
INSERT INTO HR.HRDataHistory
SELECT *, GETDATE()
FROM inserted
;
GO
This is working on an existing development machine. I am trying to mirror this relationship on my local sql server instance so that I can test some changes. Using SSMS I used 'Script Table as Create To...' and created the structure of each table and index on my local sql server instance. However when I do this for the trigger I get the following error:
An explicit value for the identity column in table 'HR.HRDataHistory' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I know the preferred method would be to specify the columns, but I want to mirror production which does not currently do that and further I want to understand why it is working in production but not on my test database.
You're getting this error because you're trying to insert data into an IDENTITY column, which auto-populates itself whenever you insert another row in that table.
Off the top of my head, you can do something like below (although I believe there are more elegant solutions and I do not guarantee that this is a safe solution, nor have I tried something like this and I recommend testing on a TEST database before trying in production/LIVE):
add another column to HRDataHistory table which does not have identity set on it (because you cannot remove identity form a colum once set), but must have the same datatype as the current ID (IDENTITY) column
use a UPDATE query to move all of your ID's from your IDENTITY column to your new column:
UPDATE HRDataHistory
SET new_column = ID
Drop the IDENTITY column (but this might have grave implications if you have any FK set on it and possibly other objects that use it):
ALTER TABLE HRDataHistory
DROP COLUMN ID
Rename the "new_column" to the name of your previous IDENTITY column:
EXEC sp_RENAME 'HRDataHistory.new_column' , 'ID', 'COLUMN'
At this point I believe you can use your trigger to "copy" the newly inserted data from the HRData table into the HRDataHistory, since the column names should match and there is no more conflict due to IDENTITY.
Again, this might (not guaranteed) work so I recommend you first check on a TEST environment.
We are using in-memory tables in MySQL with an auto-incremented primary key.
After restarting MySQL the in-memory tables are emptied as expected.
However we would like to keep the auto increment value, so that the next inserted row will have the ID after the one that was used in the last session.
Is that possible?
After the restart you can use this statement to change the next value to use for AUTO_INCREMENT columns.
ALTER TABLE mytable AUTO_INCREMENT = new_value;
The CREATE TABLE syntax is similar, but since memory tables are recreated after the restart, it won't be much use to you.
If the timing of this call is important I would recommend adding it to the startup script; I wouldn't know any other way.
We could not use the ALTER TABLE statement because a function was necessary to calculate the initial auto increment.
The solution was to:
my.cnf file was changed, after the header [mysqld] we added init-file = [file_path]
The file would execute a insert on the table with a dummy value (e.g. INSERT INTO mytable(id) VALUES (UNIX_TIMESTAMP()). After this, the line can be deleted if wished.
I recently migrated a database from mysql to pgsql 9.
But now when I try to create a new object (in django admin) it tells me that the id I'm trying to use (started at one and has increased each time I tried) is already used.
I'm guessing that there is a pointer or index which needs to be set to the last used id. Am I correct?
When you define your table, the PostgreSQL equivalent to 'auto_increment' is:
CREATE TABLE foo (
id SERIAL,
...
);
If your table is already created (as I suspect it is), you can add this manually:
CREATE SEQUENCE foo_id_seq;
ALTER TABLE foo ALTER COLUMN id SET DEFAULT nextval('foo_id_seq');
Note that if you want to stick with the default name that Pg would have given you use the following format for your sequence name:
<table name>_<column name>_seq
Thus in my example, foo_id_seq.
If the table was migrated and it uses serial to replace the mysql auto increment column, your data was probably migrated without incrementing the serial sequence. Look up the postgresql setval function to set your sequence to a value above the highest existing key in your table.
Is it possible to restore the previous state of a mysql database through phpmyadmin?
I have one column in 4 of the tables that have the auto increment.
And my problem is how to begin again in the count of number 1 when I try to add a data.
I tried deleting all the records then add a record but it doesn't start with 1.
How do I do this without building the database over again by typing and selecting the data types.
What I want to do is to start the counting from 1 again. Is it possible?
Open phpMyAdmin and then select the DB you wish to change from the top left panel.
Now click the "query" button also top left to open a query window where you can then run the sql code below to reset the auto increment count for your table.
ALTER TABLE your_table_name AUTO_INCREMENT=1
If you dont care about the data in the table you can just hit the empty button (make sure you are on the right table). That will tuncate your table and anything you add after that will start at 1. If you set a field to auto increment then each item after that will increase by 1.
Have you tried ...
ALTER TABLE t2 AUTO_INCREMENT = 1;
It will reset the value to the smallest value allowed. Other than that you are out of luck.