I'm trying to AUTO_INCREMENT a row.
When creating a row for the table, is INT better than TEXT? I use varchar for the other rows, but I presume using AUTO_INCREMENT with varchar would be completely pointless.
The row is called 'ID', and is suppose to AUTO_INCREMENT every time data is inserted into other rows. Since its suppose to AUTO_INCREMENT, rather than the other rows which allows the user to input data, would it be better to modify 'serverid' below, or disable input on the ID field of my HTML form?
Here is my code:
// Inserting input values into its respected row
$serverip=$_POST['post_serverid'];
$serverid=$_POST['post_serverip'];
$serverid=$_POST['post_serverport'];
$serverid=$_POST['post_servertitle'];
$serverid=$_POST['post_serverdesc'];
$serverid=$_POST['post_serverwebsite'];
$query = mysqli_query($con,"INSERT INTO servers (serverid, serverip, serverport,
servertitle, serverdesc, serverwebsite) VALUES
('$serverid','$serverip','$serverport','$servertitle','$serverdesc','$serverwebsite')");
When defined for an AUTO INCREMENT, there is no meaning in inputting values manually.
And at times there is also a chance that you unknowingly input a value that already present in the table. To avoid such possibilities, you better omit the auto incremented field from the insert statement.
And if you still are interested to use the field in the insert statement, you can safely use a NULL or a 0(zero) into it. So that the engine will replace it with the new value from the auto increment and use it for insertion.
If you are inputting the values from a web form, you can remove the auto increment field from the form.
You shouldn't have to increment the ID from your html/php anywhere. You should set that in the database itself, as an INT with a PRIMARY index and AUTO_INCREMENT checked. Then every entry will be assigned a unique id which you can query later.
Related
How can I insert more than one row for the same value
for example, each user has to submit 2 forms so the username is the same in each form but the information is different
I tried to use UPDATE but it removes the ole information and replaces it with the new one while I want to keep both
is there a way to do that?
insert into your_table (username, col2)
values ('user1', 1),
('user1', 2)
Have two tables, 'USERS' and 'FORMSUBMISSIONS'
When a user submits a form for the first time, a new entry is created in the USERS table, which is unique for each user, and would contain information connected to the user.
And whenever a form is submitted (including the first time), an entry is written to the FORMSUBMISSIONS table with the details of that submission, and a foreign key back to USERS.
That's a cleaner data model for this situation. It will also help future queries on the data. If you are limited to a single table for some reason, then successive inserts will work as above, as long as there is no unique key on the USER field.
you can add duplicate data just your primary key can't be duplicated because it causes primary key constraint. so what you can do is have an extra column let's say "ID" make it your primary key. While submitting the row keep on adding ID column's value by one, rest of the data could be same.
It depends on whether your USERNAME column allows duplicates.
If it's the primary key of the table, your table schema doesn't support what you want to do, because PK should be UNIQUE.
If your USERNAME column allows duplicates, you can use INSERT:
declare #username varchar(max) = 'your_username' --declare a variable to use the same username
insert into table_name (username, form_data)
values(#username, 'form_data_1')
,(#username, 'form_data_2')
It also depends on how you're executing the SQL statement. I would definately go and create stored procedure to do this insert.
you can use bulk insert query for that. as suggested by #huergen but make sure that your username or any field that might be in form data does not have UNIQUE key index. you can also add another field that works like PRIMARY key in that table.so many ways to do but it depends upon your requirement.
Use below insert format to get your desired result:
insert into Table_name(Field1, Field2)
SELECT 'user_1', 1 UNION ALL
SELECT 'user_1', 2
I was just creating a new table using MySQL Query Browser, and noticed there's a tick under Auto Increment Column. How does that work?
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Everytime a NEW user registers on my site, I want their Customer ID (integer only) to auto increment, so I don't have to try and randomly generate a unique number.
Can this be done simply?
Thank you!
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Yes, that's the way auto_increment works.
The value will be incremented for each new row
The value is unique, duplicates are not possible
If a row is deleted, the auto_increment column of that row will not be re-assigned.
The auto_increment value of the last inserted row can be accessed using the mySQL function LAST_INSERT_ID() but it must be called right after the insert query, in the same database connection
mySQL Reference
1 more,
You can insert your own value also (ie your random value).
Yes. Auto_Increment columns work like they say on the tin. Tips
when INSERT - ing, use NULL or omit the column
Use LAST_INSERT_ID() (or API equivalents) to obtain the last generated value.
for security and business logic reasons, it's usually better form to not directly use a key value for a customer identifier. Consider using Hashed / randomised surrogate customer keys instead.
Ta
Yes, that's the exact purpose of AUTO_INCREMENT. It looks at whatever is the current increment value for that table, and stores that value plus 1 for the new row that comes in, automatically. You can omit that field from your INSERT statements and MySQL will handle it for you for every new row that comes in, giving each row its own unique ID.
When you enable Auto Increment an ID will always get automatically added whenever a new record is made.. Example:
If you have 1 record with ID 1 in your table and you add a new record, the ID will automatically be 2.
So, assuming I have a table, with 20 columns, and I want to insert data to them, and the first column is just a primary index, which will automatically have a numerical value if I don't assign one to it. Primary index has auto increment on.
So my question is, do I always have to do it like this (which is pretty annoying and slow)
INSERT INTO table (col1,col2,col3,col4,col5,col6) VALUES (col1value,col2value,col3value,col4,value,col5value,col6value)
or, could I just specify the values (assuming that I'm filling every column except the primary index) like this
INSERT INTO table VALUES (col1values,col2values,col3values,col4values,col5values,col6values)
or will it break the table?
No, you can't use your second solution, unless you provide as many values as field count. You can still provide primary key value with NULL. MySQL will then replace it by an auto-incremented value (assuming your column is PRIMARY KEY + AUTO_INCREMENT).
Nevertheless, the first solution is perfectly fine.
tldr; You need to define the columns you are inserting into.
Mysql assumes that when you don't put in the column names like below
INSERT INTO table (column_name1, column_name2, column_name3)
that you are going to define all the columns in your value definition later. Without defining the column names, it wouldn't know which row to skip for the primary key.
An example of this would be if you had the primary key as column_name2 in the above example.
The first one is correct.
In the second one you can specify the primary key attribute as null or using INSERT INTO table VALUES('',col2values,col3values,col4values,col5values,col6values) assuming that the first field is primary key.
I have a mysql table with descriptionId as a primary key and it is auto incremented. it also has a "content" and a "price" columns and few more.
I also have a form consisting of multiple input boxes with the current database values of my price and content columns in my description table. after submitting the form i'd like to update the table with the new values and if any of the input boxes is deleted, the record must be deleted from the table.
I have also managed to define three arrays to hold the values of all my tables' columns. These Arrays are as followed: $descrId,$content,$price
when i submit my form, the php file loops through theses arrays and executes the following query:(I have validate these arrays so they work just fine)
INSERT INTO
description(descriptionId,content,price,orderNo,salesPerson,dateTime,updated)
VALUES('{$descrId[$k]}','{$content[$k]}','{$price[$k]}','{$orderId}','{$sale}',NOW(),1 )
ON DUPLICATE KEY UPDATE content=VALUES(content),price=VALUES(price),updated=1, dateTime=NOW()
However, this query keeps duplicating the values anytime i press submit.
I appreciate your time....
As I read your question ON DUPLICATE KEY will never occur, because your primary key is an auto incremented value, which will be +1 each time your insert something in the table, so it will never have a duplicated value - that's the idea more or less behind AUTO INCREMENT.
So the answer is to pick another column, i.e. orderNo or dateTime, make it UNIQUE and then try again with the query.
Update
Alternatively you can combine two or more columns and define them as a (unique) key.
If that's also not applicable in your case, then use some hashing function/algorithm when inserting the data and store that hash along the other values in the table.
I have an issue in mysql that i have a field id which is auto increment and some other fields. where the id field should not be autoincremented while enterting the null values and should be autoincremented while entering values insert values in the same row while giving not null values .
It sounds like you need to generate the value for the id field yourself, in your own code, rather than having the database generate it.
If you create an identity field in the database, the database will create the field automatically. Generally this occurs when the record is saved, whether there are null fields present or not. If you need more control than this, you have to generate the id values yourself.
If you need to know what the next id number is, you can get it with SELECT MAX(id_field);
Robert Harvey's answer tackles the problem but I would also suggest looking at what you are saving and see if there is another approach. Perhaps the null values should be saved in another table? Perhaps the column you have as auto-increment isn't the primary key.
This may not be the direction but in this case it's worth stepping back and re-examining.