I am trying to create a mysql queryer in java and I would like to be able to add the primary key(composite) associated with the user query.
For example:
SELECT *
FROM my_table
WHERE logic
How can i add the composite primary key as the first column of my result? I need to keep columns asked by the user too.
Do i need to use:
CONCAT_WS('-',column1,column2,column3) ?
I can't use it if the user asks every columns (using *).
Perhaps something like this will help:
SELECT concat(....), mytable.*
FROM mytable
Although when you add the concat-column add the end, somthing like below without the additional table reference should work fine:
SELECT *, concat(....) FROM mytable
Related
I'm fairly new to developing web apps in Java. I just connected the database, and as seen in the pictures, my ID_patient is auto_increment, but in Netbeans it looks generated.
INSERT INTO sys.patient values('5','elif','nil','er','elif#hotmail.com','11111111111','1234a','istanbul')
The new record inserted wants this value, while i want it to take
INSERT INTO sys.patient values('elif','nil','er','elif#hotmail.com','11111111111','1234a','istanbul')
and auto-increment and give the id as 1,2,3,4...etc.
how can I fix this?
thank you
in netbeans
in mysql
If you are expecting to create an insert query where you don't want to provide an id and the database should generate it automatically based on table defination then you need to create insert query where you have to mention columns names.
Example
INSERT INTO <TABLENAME>(COLUMN1, COLUMN2, COLUMN3, COLUMN4)
VALUES
(VALUE1,VALUE2, VALUE 3,VALUE 4);
So in ur case it should be
INSERT INTO PATIENT(FirstName,MiddleName,LastName,E_mail)
values
('myname','mymiddlename','mylastname','myemailid');
Sequence of column names and their values are very important. They should match exactly. If you don't provide a value for one of column and if its is auto increment, then DB will add an value to it else it will add null value.
So I have a MySQL database schema where there is a USERS table which contains the ID as a primary key for that table, I also have a USER_PASSWORDS table which references the USERS table where the USER_ID will act as a foreign key in this table.
The issue that I am facing is that I am writing an application where the user will be able to sign up and specify a username and password. But I would like to insert the user into the database with one query.
I was thinking I had to insert the username first into the USERS table and see what ID has been given to that username and then insert the hash of the password that the user has entered into the USER_PASSWORDS table and specifying the ID that was queried.
I dont like this approach because it means that I have to:
INSERT into the database
QUERY the database
INSERT into the database again
Is there a better way of doing this?
Thanks
You can't insert into two tables with one insert statement, and you would have to query the users table anyway to get the ID value to insert as a foreign key for the user_passwords table.
Really the only way to do what you want is the solution you've already identified:
Insert into the Users table
Query to get the ID of the User you just inserted
Insert into the USER_PASSWORDS table with the ID you obtained for the User.
You could wrap all this up in a stored procedure that takes user data and password as parameters, which would be the "better" way of doing it.
As you didnt really tell for what system / programming language you need this and you did not provide any code example either, I can only give you some theory what you could do:
Its impossible to insert data into two different MySQL tables with one queries but you can reduce your script atleast by the SELECT query:
1.) There is a function in most mysql apis (Depending on what programming language and MySQL Library you are using) that says "getLastInsertId()", "lastInsertId()" or similar.
This will return the ID that was inserted by the auto-increment of the table after the insert is completed.
Just check the docs of your MySQL-api it will have such a function.
2.)
The second possibility is using a UUID - a very large (commonly 128-bit) long string which is generated totally random. There are more so many possible combinations it will happen more probably that you win in the lottery 10 times in a row then you generate two times the same UUID that is already in your table.
So you just generate the UUID and insert it as a key in both tables and you are done.
Just use google to find out-of-the-box libraries to generate UUID's you dont need to build the alogrithm on your own.
An UUID could looks like this:
4a34fe87-f577-4ea9-9557-1bc8f779a68c
One solution: since the hash is unique you can use the hash as a primary key in the USERS table. Then you know what the primary key (id) is at the time of the insert and can reuse when INSERTING in the USER_PASSWORDS table.
That way you can avoid the id query at least.
Im using google app engine data-store built in eclipse using my model for the table. The id is just the date and time from android.
I can query by a row like this and it does work!
select from Quotes as Quotes ORDER BY votes DESC
I want to get my results back by my entities id however this query does not work
select from Quotes as Quotes ORDER BY Id DESC
Here is my table. How can I query by my id/Name and trust me ive tried
select from Quotes as Quotes ORDER BY ID/Name DESC
edit: you probably notice i have a dummyid. I do not want to use that row because I made it in a very hacky way and requires extra loading on the users side.
Oh, dear. I see the problem, now. You have a column named ID/Name. It's usually wise to keep identifiers limited to alphanumeric characters.
Can you rename the column? That would be the best step forward.
If that's not an option, you can wrap it in backticks so that it's treated as an identifier:
SELECT * FROM Quotes ORDER BY `ID/Name` DESC;
See SQL Fiddle, which almost certainly won't match your schema but should get the point across.
That Id/Name is the key field, imagine it is similar to primary key. to refer to that field in query, use
__key__
Example: select * from EntityTable where __key__ = Key('EntityTable', ....)
In your example, using date/time as key name is not really helpful, maybe you can find another info to be used as key.
I have a project in java where i want that certain data from one table (that is in Sql management studio) is selected and that is inserted in other table. So that i can access data on a jsp page from second table. How to do this?
One method would be to iterate through the table while writing the values into an array. Once the data has been stored into the array you can re-iterate through the array but this time inserting the values into a new table.
This may not be the most efficient method, I am sure someone else will chime in if so.
Another method which does not require Java would be to use the Select As statement in SQL, see example.
CREATE TABLE suppliers
AS (SELECT *
FROM companies
WHERE id > 1000);
Or if you already have a table created you can do the following,
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE city = 'Newark';
If you use SQL, you can use SELECT INTO statements to achieve this easily:
SELECT Column1,Column2
INTO SecondTable
FROM FirstTable
WHERE Column3='Whatever'
This will copy the data from FirstTable into SecondTable.
See This Link for more examples
If I have a table like:
CREATE TABLE FRED
(
recordId number(18) primary key,
firstName varchar2(50)
);
Is there an easy way to clone it's structure (not it's data) into another table of a given name. Basically I want to create table with exactly the same structure, but a different name, so that I can perform some functionality on it. I want to do this in code obviously. Java preferably, but most other languages should be similar.
If you're looking a way to find the exact DDL to recreate the table, including the storage clause, you can use
select dbms_metadata.get_ddl('TABLE', 'TABLE_NAME', 'SCHEMA_NAME') from dual
as described here.
CREATE TABLE tablename AS SELECT * FROM orginaltable WHERE 1=2;
Edit: The WHERE clause prohibits any rows from qualifying.
SELECT INTO TARGET_TABLE FROM SOURCE_TABLE;
OR
CREATE TABLE TARGET_TABLE_NAME AS SELECT * FROM SOURCE_TABLE;
if you want to copy only the structure then add the Where Clause WHERE 1=2.
I hope it will be helpful.