This question already has answers here:
Generate next Id as per the max id in database using Java
(2 answers)
Closed 8 years ago.
I am working on a web application. It will have multiple users. I am using mysql as database.
In my application i am fetching the latest id (from the database, using max(id)) , and then generating the next id for the new registration. This approach is incorrect, as the id might change between the time i update an id
How should i proceed?
LAST_INSERT_ID() or AUTO_INCREMENT will be your friends
LAST_INSERT_ID() :
INSERT INTO table (id, field) VALUES (LAST_INSERT_ID() + 1, "Hello")
AUTO_INCREMENT :
INSERT INTO table (field) VALUES ("Hello")
And the id is auto incrementing
Some documentation here :
https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
https://dev.mysql.com/doc/refman/5.0/fr/example-auto-increment.html
Related
This question already has answers here:
Prevent duplicate values in database - mysql
(2 answers)
Closed 4 months ago.
Long story short,
I'm trying to create a table, where you can enter a company name. However you cannot enter a company name which contains a string value of a previous entered company name. Is this possible to do with a check constraint?
For example:
My company "CrazyJello" is inserted. Now the following companies that are being entered to MySQL cannot have the string "Crazy" in them.
CREATE table company(
name VARCHAR (100)
CHECK (name != ?????)
No, in sql there is the unique index, but it compares the whole content of the field https://www.mysqltutorial.org/mysql-unique/
This question already has an answer here:
MySQL - insert data from another table merged with constants
(1 answer)
Closed 5 months ago.
For example i have a table name Info have 3 columns ID,COST,CITY
INSERT INTO Info
SELECT *
FROM Info;
WHERE Cost = 100
This will Create exact same copy of 1st row.
I want that new entry need to be 1,250,pune
How can I do when i have 100 columns and need to change only 5 columns thats the real deal?
You can simply add the new value into the select:
INSERT INTO Info (Cost, city)
SELECT 250,city
FROM Info
WHERE Cost = 100
But the ID can not be copied, because an id should be unique
If your ID isn't an auto_increment you can add the id to the queries:
INSERT INTO Info (ID, Cost, city)
SELECT ID,250,city
FROM Info
WHERE Cost = 100
This question already has answers here:
Delete sql rows where IDs do not have a match from another table
(4 answers)
Closed 1 year ago.
I have a MySQL database where i want to clean up a table but i do not know how to compare against other table entrys and hope someone can help.
One table in the database is called "stats". in the table "Stats" there is a row called "Page_title" which holds an ID number. The same ID number should exist in the table "url,id" otherwise that entry from "stats" should be removed, how can this be done?
Please see this image for reference.
You can do this with a sub select:
delete from stats where page_title not in (select id from shorturl)
This question already has answers here:
MySQL, passing a AUTO_INCREMENT to another table
(2 answers)
Insert ID from one table to another in MySQL php
(1 answer)
Closed 5 years ago.
I have two tables booking and client. Booking table will have client's id. Now I want to ask for booking detail first and then user detail next. The problem for me is when I insert booking detail first then insert user detail next I have to insert user's id in booking table. how is it possible?
note:i have to do in core php.
This question already has answers here:
SQL query return data from multiple tables
(6 answers)
Closed 5 years ago.
Table1
Id, name, addresses
101,raja ,chennai
Table2
Id,group,name
101,a,siva
102,b,selva
I want retrieve data from two tables like
Table2.group=a and two table Id must equal then take address from table1 display
Id, name, Address, group
What have you tried so far? Have you taken a look at some of the resources here?
Since this is the type of pretty straightforward SQL query you're likely to do many times over, it's worth you reading the tutorials and learning it yourself. I'll give you a few pointers to begin:
Your two tables are connected by the Id field (most likely, this is the primary key for one table and the foreign key for another). You'll need to JOIN these two tables using that field.
You'll need to specify a WHERE clause to filter for your condition that Table2.group = a.
The fields you want to display can all be specified using SELECT. If you are selecting data FROM Table 1 and joining to Table 2, you won't need to specify the table name before the field name.