Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




With INSERT Can I Increment An Existing Value In A Column?


I have a table of addresses and a seperate table with contact names -
All addresses tie to one or more names - I would like to keep track of
the number of names 'belonging' to an address and have thus included a
column in my address table called num_of_contacts. Everytime I add a
new contact, I would like to increment the num_of_contacts column in the
address table. Is this possible?

Me thinks not (or at least, my attempts so far have failed me) so I'd
appreciate it if someone could tell me if I am wasteing my time trying
and instead SELECT the record, increment it, then UPDATE it.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Add Auto Increment To Existing Column
I have a table with 126 rows in it. The guy that originally created the table didn't think to make the id column an auto incremented one.

For the PHP script I am writing I could really do with making the id column auto increment.

I've tried

CODEALTER TABLE products CHANGE product_id product_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

Existing Column Auto Increment
Can you make an existing column in a table auto increment? The column in question has unique consequtive values.

Auto Increment Existing Table
I have an existing table with many rows of data. I want to add auto increment to it so I do not have to manually asign each row a unique record number.

Increment Column Without Auto Increment
What's the fastest way I can use an INSERT statement to insert a new record including an 'ID' which is one greater than the current highest ID in the table.

Is this possible without first doing a SELECT query? I'll be using php too btw, so if there's a way to do this in php, then I'm open to such suggestions also.

Insert Row Into Existing Table
I have a table with rougly 70 threads, I now need to go back and insert a couple new entries into my table, but they cant go at the end, they need to go in the middle, say like ID 40,41,and 42. How do I do this?

Insert A Duplicate Of An Existing Row
I have a table with an auto-increment columns (absid) and I want to take a row with a certain absid and re-insert it as a new row with a new absid (but all other columns as from the copied row). Running mysql 3.23.55. Might something like the following work?

insert into mytable (absid,*) values 0 select * from mytable where absid=20;

or something similar? With 3.23.55 I suspect it won't work anyway but I have no access to a version 4 installation and wouldn't want to upgrade if what I want to do is inherently not possible.

How Do I Add Default To Existing Column?
It should be easy but i've searched from mysql manual to here...

Search Table Column For Existing Value
I would like to search a table's column so that I can match an entered email address against the data in that column. I'm writing an aplication for an event and I don't want duplicate email addresses in the table. I'm using PHP4.

How Do You Discern The Type Of An Existing Mysql Table Column?
I have a program that needs to be able to know whether a MySQL column is a number type or other. Is there a function that does this, or can anyone else suggest a way of discerning this reliably using SQL statements?

Auto-increment And Update/insert
I have the following problem with a MySQL update/insert.

my problem lies in the fact that I have to insert a new row between 2 existing rows. the table is sorted by ID and has ID as auto-increment so I cannot insert a new row between 2 rows as inserting will assign the insert a new ID number...

I figured now I have to do an update and then loop till the end of the table and then do an insert at the end..

How Do I Insert An Auto Increment ID Into Other Tables
I have been trying to insert An auto_increment ID of one table into other tables and I am getting wierd results. I am trying to insert the CLIENT table primary key into the other three tables and it is inserted correctly into the CONTACTS table but the JOBSHEET and KNOWLEDGE tables both insert 1 into the clientID field. Code:

Autom Increment Column
how to create an auto increase column (1,2,3...) in a Mysql table ?

Increment Based On Another Column
I'm having a little trouble working out the best way of doing something.

I'm storing a key split across two fields, the first field is 3 letters, the second is three numbers:
CREATE TABLE table1 (
id_a CHAR(3) NOT NULL,
id_b SMALLINT(3) NOT NULL,
...
);
What I'd like to do is have it so that when a new row is entered 'id_b' is calculated as MAX(id_b) + 1 WHERE id_a = new_id_a.

Is it safe to do this as some sort of nested query or is there a better way?

Is It Possible To Force A Zero Into Auto-increment Column?
I want to add MySQL support to a SQL Server application that contains many IDENTITY (auto-increment) columns with zero in the id field. We use that as a proxy for no entry to keep joins from dropping out entries with no value.

Is there any way to insert a zero into an auto-increment column?

I know you can specify a specific value in the insert if it does not already exist and if it's 0 or no entry is specified in the insert then you get the next available value.

How would you force a zero into the column?

Alter Column To Auto Increment
I have a table that has data in it. The table had an auto increment column but I had to take auto increment off to insert some old data from a backup site.
I get an error when I try to alter table to make the column auto increment again. I need some assistance.

InnoDB Auto-Increment On Secondary Key Column?
MySQL MyISAM keys that use an auto-increment attribute as the secondary part of a multi-column key, allow the generated value to be tightly associated with the primary part of the key, while the auto-increment in InnoDB is just a running number. This forces you to use an attribute for the auto-increment attribute that can accomodate the maximum table size even though a single grouping may never exceed ten. Depending the order of entry the comparison might be:

MyISAM:
dog/1
dog/2
cat/1
cat/2
fish/1
fish/2
fish/3

InnoDB:
dog/4
dog/5
cat/1
cat/3
fish/2
fish/6
fish/7

While it does accomplish a natural grouping, it also forces inefficient usage of storage, since you have to accomodate the largest table size. Is there any way around it? Facing Future - IZ

Alter A Primary Key Auto Increment Column
I need to change my primary key column type from smallint to int.
I have tried:
ALTER TABLE livegroup MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT;
But get an error message certainly since my id-column is primary key
and references other tables as well.

This is my table definition

livegroup (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(60) NOT NULL,
name CHAR(60) NOT NULL,
public TINYINT NOT NULL,
creator SMALLINT UNSIGNED NOT NULL,
lastmodified TIMESTAMP(8),
PRIMARY KEY (id),
INDEX (creator),
FOREIGN KEY (creator) REFERENCES user (id) ON DELETE CASCADE
) TYPE=INNODB;

livegroups (
data_id SMALLINT UNSIGNED NOT NULL,
livegroup_id SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (data_id, livegroup_id),
INDEX (data_id),
INDEX (livegroup_id),
FOREIGN KEY (data_id) REFERENCES livedata (id) ON DELETE CASCADE,
FOREIGN KEY (livegroup_id) REFERENCES livegroup (id) ON DELETE
CASCADE
) TYPE=INNODB;

Table Joins On Existing/Non-Existing Data
I have a question that involves a joining tables on potentially non-existing data, but we will know the expected values whether they exist or not.

Say we're keeping statistics on any given number of sports for attendances over weekly time periods. There's two tables to hold data.

------------------------------
TABLE: StatisticTypes
id
name
------------------------------
TABLE: Statistics
id
statisticTypeId
value
date
------------------------------

Then, say, we have three entries in the StatisticTypes table for types of sports we're keeping data on...

Copying Column Names And Properties From An Existing Table To A New Table?
I've got a table with 42 columns, set to different types (string, datetime, date, integer). It has 8 million rows. I want to make a new table with the exact same columns, but with no data in it. Is there an easy way to do this without re-defining every column?

Insert New Rows With Qty Values From Existing Rows
I am trying to make all my products unique in my db.
Lets say I have a row with an id, it also has all relevant details about the product and it has a quantity value of 4.
What I would like to do is take the entire row and duplicate it by the number of the quantity field.
This would result in the same product 4 times instead of one product with qty of 4.
Reason, I am going to serialise all the products so that they each have unique barcode from the id field.
I will encounter the reverse issue when running an insert statement for inputting new data, i.e. insert a new row for each item depending on its qty value??

Insert Column BETWEEN
Is it possible to (like phpmyadmin) insert new columns between existing columns using a php script?

I have quite a few databases that I need to update with new columns in some of the tables and don't what to have to do it manually for every db. Writing a script would be much easier (and faster?).

How To Insert A Column
Using the MySQL Administrator | MySQL Table Editor:

How do I insert a new column when designing a table? As far as I see I can only add a new column to the bottom of the list....

I know it can be done with the Query Browser like this:

ALTER TABLE `mydb`.`mytable` ADD COLUMN `newcol` INTEGER UNSIGNED NOT NULL DEFAULT 0 AFTER `oldcol`;

but I was just wondering if it could be done easily with the Table Editor.

Insert AM/PM In Datetime Column
I have a datetime column (0000-00-00 00:00:00) in my table. I update the column from form fields for month, day, year, hour and minute. I always set the seconds to 00. This is all in PHP.

Where and how can I indicate AM or PM values? Are they part of the datetime column? I know you can use '%p' in the SELECT for display but how do I insert them?. Do I have to use the 24 hour time format? I would prefer not to do that.

Insert A Modification Of An Exsisting Column
I have a column called "filename" that has entries such as this:
AAA.wmv
BBB.wmv
CCC.wmv
3434.wmv
DJerr344.wmv
and so on...

I would like to create another column "modder" that would have entries such that:
depart_AAA.jpg
depart_BBB.jpg
depart_CCC.jpg
depart_3434.jpg
depart_DJerr344.jpg
and so on...

Sort Column/Insert Alphabetically
I'd like a column to be in alphabetical order. I know that in the select statement I can order the entries, but I was wondering if theres a way to sort the column permanantly or when inserting a new column insert it alphabetically so its always in order?

Insert Copy Auto_increment To Another Column
I am coming from MS SQL Server background. And too used to SPs. Here is my question-
I want to insert data into a table and one of the columns is a copy of the primary key which is an auto increment field. How can I do this without running additional Update query?

Eg. INSERT INTO mytable ( copy_id, f_name, l_name ) VALUES ([this is the new id assigned to this row, MAX(id)+1 failed for me], 'fname', 'lname');

Table schema:

ID - auto increment
copy_id - int
f_name - varchar
l_name - varchar

I tried using Last_insert_id (expr) but every time it inserts 1. I am using C# with MySQL.

INSERT Values In Three Tables Using ID Column
how to insert values in different tables?

Table t1 got a column named "id" and this value should appear in t2.t1_id

In other words: How to fill all columns in all tables with one INSERT / UPDATE ?

t1

id bigint autoincrement
name varchar()
bla
bla


t1
id ....

How To Insert A Calculated Value Based On Diff Column
let's say I have 3 columns
create table test1 (
id int,
dateCreated timestamp default current_timestamp,
localDateCreated datetime);

the localDateCreated is always 8 hours ahead dateCreated.

so when i insert id number, lacalDateCreated will be calculated based on dateCreated. How to do that?

I thought trigger might help, but it gave error

mysql> delimiter |
mysql> create trigger trgtest2ai after insert on test2
-> for each row begin
-> update test2 set local=new.ts where id=last_insert_id();
-> end;
-> |
Query OK, 0 rows affected (0.03 sec)

mysql> insert into test2 set id=10;
-> |
ERROR 1442 (HY000): Can't update table 'test2' in stored function/trigger because it is already used by statement which invoked this stored fun
ction/trigger.
mysql> delimiter ;

How To Insert A Binary Data Size Of 4GB Into A Longblob Column.
I was using unixODBC as the driver manager and mysql ODBC driver to connect to the mysql server.  I am using Linux platform and coding with C++.  I had created a table with a column type of longblob and wish to use ODBC function call to insert a 4GB of data into the mysql database. 

At first, I got error saying that the data size is over the limit of max_allowed_packet size, so I changed the max_allowed_packet to around 1GB (that is the max max_allowed_packet variable can take). 

Then I start to sent a 1GB data to the mysql database.  It will complaint about "Out of memory (Needed 500000048 bytes)".  Seems like I could only sent max of 500 MB of data, otherwise the mysql server will complaint about the above out of memory problem.  Not sure how to get around this problem.

Increment Of Auto Increment Is Not Consistent
I have a table with one the the column set as auto increment. MY storage engine is innodb. The problem is the my increment of the auto increment is not consistent at all it was growing from 1 till 6 then suddenly it went to 802 ? Can some one guide how to make sure its increment is one by one ? Thanks.

My.cnf Not Existing
this is my first posting here so plz do not flame me too much if my quest=
ion=20
marks me as a total noob.

My problem is, i play around a bit with mysql (4.0.13) and want to test t=
he=20
Replication. The Documentation at http://www.mysql.com says i have to do =
the=20
following:
Stop databases - done.
Build tarball of data and bring it on slave - done.
Modify my.cnf - ???

Here is my problem. There is no my.cnf existing on my machine. Maybe its=20
called different with the newer version? Or can it be considered empty an=
d i=20
have to build a new one? But if i have to, in which directory would i do=20
that?

ON EXISTING Clause
What I'm trying to do is update a table of mine in mySQL. However, some of the values may not exist in the table yet, while most will already be there.

I have done some searching and I think I need help. I'm assuming UPDATE won't simply act as an INSERT if the existing value is not found. From my searching it seems I need to use the ON EXISTING clause with an INSERT. However, it seems that ON EXISTING can only be used when there is a primary key. In my case, thats not the case here.

However, for me this is query confusing at the moment. Can someone provide a hand?

My table is called "rankings" and has the following structure:
comp_id - int (and an index)
uid - int
networth - int



How To Add A New Row To An Existing Table?
I am able to create database, tables, inserting values, and so forth. My question is this:

Let's say I have a table on the list of items I owned, and some time later, I need to add a new item between after row 12 and before 13. How do I do that? I did look at MySQL 5.0 Manual and didn't find anything similar to what I wanted to do.

Check For Existing Row ?
How do I determine if a row already exists ?

// see if a row has id=100

$id=100;
$query="SELECT * FROM mytable WHERE id=$id";
$result=mysql_query($query);

...what do I check now ?

Using Existing Database
I am setting up a new dev. machine and need to get an old database into the new installation of MySql. There are two databases showing on the database list 'mysql' and 'test'.
Can I drop the old database (contained in a folder) straight in with the existing databases and get any sense from it?

Existing Database
I'm using MySQL 4.1.9-nt & VC++ 6.0 .
how to get all the database names and table names?

I'm using the VC++ syntax,

CRecordset rs(&db); //here db is object of CDatabase class.
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"show databases ");

Information From Existing Db
Dont know much about mySQL, so excuse my potential ignorance here :
Is it possible for a table to write information to another table automatically?
For example, if on "table1" I have a column called "code", can I write the entire contents of a specified row to another table called "abc" if the code "abc" appears in the "code" column of "table1"

Add A Record To An Existing Recordset?
I have a MySQL table with about 30 records, I use a form to insert data into the first 24. (like; Name, Phone, etc...)

The last 6 (Photo1, Photo2, Photo3, Photo4, Photo5, Photo6) are the names of uploaded images (which are uploaded from a seperate form.

How do I (Or what MySQL statement do I use) to insert the 6 names into the recordset?
(Each image name will be saved into a php variable. ($Img1, $Img2, $Img3, $Img4, $Img5, $Img6)

Using Update To Add Value To Existing Numeric Value
I have users inputting hours into a mysql database. Is there a way to have a mysql statement take my input and add it to the existing value in a record? I suppose I could just select that value and use php to add the 2 values but was thinking mysql might have a function to do this for me.


Moving Existing Fields Out
Can someone tell me what would be the best way to move fields out of an existing table into new tables?

Best Way To Get Data From Existing Database
what I'm trying to do is get data from an exhisting oracle database that is not looked after by myself but I have access to. What I want is for my MySql database to mirror the oracle one or update every 5mins or so. I have done a quick search but as I have only made simple databases before I'm not sure what to search for!

Attaching An Existing Database
I would like to attach an existing database (I have the files from another system) to MySQL running on a recovery machine - yeah, the Linux system got screwed, but I managed to recover the files.

So, having the database files (.myi, .myd and .frm), how do I go about incorporating them into the current system? (Like "attach database" in MSSQL)

Foreign Key To Existing Table?
Is it possible to add a foreign key to a table that already exists? Or do I have to drop the table and recreate it?

Accessing Existing DB From Files Off A CD
I inherited a project from another developer who was fired. There is no database documentation. There is both an SQL backup file and the original database files on a CD. I have recreated the db from the backup file and that worked perfectly. However, I don't have full confidence in the accuracy of the backup file. I was wondering if I can somehow access the db from the files on the CD.

I copied the files to a folder on my machine and then changed the MySQL data folder setting to that folder. Unfortunately MySQL wouldn't start. What would I need to do to be able to have MySQL use the files copied from the CD as the data files?

Running An Existing SQL File
I hope this question is not too 'newbie' to answer.. I'm reading a book (PHP and MySQL web development by Luke Welling and Laura Thomson), and am getting an error on an example script.

I'm trying to create tables using an existing SQL file through MySQL. According to the book, it should be done like this:

> mysql -h hostname -u username -D database -p < file.sql
(I've changed the hostname, username, etc.)

However, I get the same error over and over again: ERROR 1064 (42000): ... at line 1. The book didn't even mention where to put the file, I've tried all sorts of things, but can't get it to work..

Copy Of An Existing Table
MySQL: How I make a copy of an existing table in phpMyAdmin or SQL ?

without copy data
with copy and data

Uploading Existing Database
I just got a dedicated server and can't upload some of the larger databases. I have tried compressed (gz) as well as the none zipped and automatic. None works for a 50MB + database.

I am not sure what's wrong, maybe it times out, but then I never get the error. If that's the case then how do I change the timeout settings in MySQL. Also I am using Plesk, not sure if that matters.

Edit Existing Data
I have just started to learn MySQL and this question might be silly. I have a MySQL table with user information. I want to edit this table in a website. I need to load existing data in a HTML form and then edit the fields and update them.
I searched the net but could not find anything .

List Of Non-existing Users
I have 2 tables: tableFrom , tableTo.

I want to have the list of users that ARE in tableFrom, but NOT in tableTo.
The common field is "username".
I want all the rows from tableFrom as long as they don't already exist in tableTo.
An important point is that we use MySQL 4.0.20, so I can't use "NOT EXISTS(...)".
This query needs to be pretty fast since it might be ran many times an hour, even minutes.


Copyright © 2005-08 www.BigResource.com, All rights reserved