I am trying to create a temporary table and right now I am doing something like this:
CREATE IF NOT EXISTS TABLE tempdb.student AS (SELECT * FROM student LIMIT 0)
then
ALTER TABLE tempdb.student MODIFY id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY
However, I need to find a way to do this without the ALTER statement because both statements are getting ran together and if the table has already been created, I am getting an error because of the multiple primary keys. Any advice? Thanks
You can define columns, indexes, table options, and partitioning before the AS keyword.
CREATE IF NOT EXISTS TABLE tempdb.student (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(10),
enroll_date DATE,
...etc...
)
AS SELECT * FROM student LIMIT 0
This is the only way you can set some column or table options, such as auto-increment.
But it means you have to define all the columns, not just id.
Since you are using LIMIT 0 it means you don't actually want any rows from the source table. I guess you are just trying to duplicate the table structure, but none of the data.
In that case, you might like to use the following instead:
CREATE IF NOT EXISTS TABLE tempdb.student LIKE student;
This duplicates other column and table options, and indexes, and partitions. The new table will have the AUTO_INCREMENT option for its primary key.
You can explicitly check if the table exists (you need to be careful if the code can be executed in parallel):
SELECT
COUNT(*) INTO #exists
FROM
information_schema.tables
WHERE
table_schema = 'tempdb'
AND table_name = 'student'
;
IF #exists = 0 THEN
CREATE TABLE tempdb.student AS (SELECT * FROM student LIMIT 0); -- or use LIKE syntax
ALTER TABLE tempdb.student MODIFY id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY;
END IF;
If your temporary table needed for the session only, I'd use CREATE TEMPORARY TABLE ... LIKE ... and alter as required.
Related
This is a syntax question, I don't know if what I'm thinking is possible. I can't find anything about it.
CREATE TABLE IF NOT EXISTS newTable
SELECT something1, something2, something3 FROM someTable;
Using this syntax to create a new table, can I add a primary key to this? Something like this:
CREATE TABLE IF NOT EXISTS newTable
SELECT id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, something1, something2, something3 FROM someTable;
I have a large and complex dynamic query that returns me te exact results I need. All that's left is to add these results an autoincrement primary key!
Because the query is dynamic, I don't know the number of columns I'll get, so I can't just:
DROP TABLE IF EXISTS newTable;
CREATE TABLE newTable(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
something1 VARCHAR(255),
something2 VARCHAR(255),
something3 VARCHAR(255)
) ENGINE=MEMORY;
Thanks in advance for any given advice!
Looks like you will have to run multiple steps to do this:
Create a table with the primary key
Add the columns to this table using a stored procedure (could be merged with step 1)
To do this you use the information schema. This will look as follows (dummy code, no mysql handy to debug anything):
CREATE PROCEDURE create_table(table_name VARCHAR(200))
BEGIN
declare cursor table_columns as SELECT COLUMN_NAME, DATA_TYPE,
IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table_name'
LOOP
concatenate create table statement
END LOOP;
execute create table statement
END
i understand,below column will be signed int by default.
id INT(6);
Can an auto increment column specified below be signed by default? Mysql starts the value from 1 for an auto increment column.
id INT(6) AUTO_INCREMENT PRIMARY KEY
Yes, you can create an auto increment primary key with a signed int. Try this:
CREATE TABLE mytable( id int(6) AUTO_INCREMENT PRIMARY KEY);
Then the following queries are both valid
INSERT INTO mytable values();
INSERT INTO mytable values(-10);
This will result in the table having a row with -10 and another with 1 as values. But you will run into problems if you try this:
ALTER TABLE mytable AUTO_INCREMENT=-10;
yes, you cannot have auto increment values that are negative numbers.
After a lot of searches... I looked for a solution with a TRIGGER called BEFORE INSERT ! I found this : https://stackoverflow.com/a/43441586/2282880
Here is my variant :
CREATE TRIGGER `invertID`
BEFORE INSERT ON `<table>`
FOR EACH ROW SET NEW.id=CONCAT("-", (
SELECT `auto_increment`
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = '<table>')
)
It worked for me fine.
It was the best way I found to sync in both directions two databases with same schema without same ID's in my tables.
I have tried several ways of setting the auto increment value on the primary key ID on a table with no luck.
Doesn't work for me...
id INT NOT NULL AUTO_INCREMENT = 10000,
Tried this...
UPDATE tablename SET id = id + 10000;
Tried this..
ALTER TABLE tablename AUTO_INCREMENT = 10000;
Tried this..
CREATE TABLE tablename (
...
) ENGINE = MyISAM AUTO_INCREMENT = 10000;
What is the proper way to set this when creating a table using SQL?
On the creation of the table you can use these SQL statement:
CREATE TABLE tablename (
...
) ENGINE = InnoDB AUTO_INCREMENT = 10000;
But, if the table is already created, you can make an ALTER TABLE to let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
ALTER TABLE tablename AUTO_INCREMENT = 10000;
You can find more on using AUTO_INCREMENT.
Good day everyone. Hope someone can helps me in my experiments.
I have a simple Table for my needs and it's even have some data inside.
CREATE TABLE IF NOT exists main.test (
ID INT(11) NOT NULL,
NAME varchar(30) NOT NULL,
DATE_CREATED timestamp NOT NULL,
PRIMARY KEY (ID));
But then I should update this table with adding column FK_.
How can I check if table had already has field FK_?
If such column is not exist do:
ALTER TABLE main.test
ADD COLUMN FK INT(11),
ADD FOREIGN KEY (FK)
REFERENCES test2(ID_test2)
As I use java decision of my problem was using ResultSet.
ResultSet set = statement.executeQuery(query);
set.next();
int result = set.getInt(1); //it always return only one row
set.close();
And this is my sql-query:
SELECT COUNT(COLUMN_NAME) FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'main'
AND TABLE_NAME = 'test'
AND COLUMN_NAME = 'FK";
When I'll get a result I can decide what query I should to use.
I'm trying to insert a row into a table which references keys in other tables. There are instances where the insert/select will fail because the selected value can not be null. I'd like to know which selects fail so that I can create the necessary rows in other tables as needed.
The following example is a bit contrived, but should illustrate the challenge here:
CREATE TABLE TableOne
(
TableOneId INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
Value VARCHAR(64) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE TableTwo
(
TableTwoId INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
Value VARCHAR(64) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE DependentTable
(
DependentId INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
TableOneId INT UNSIGNED NOT NULL,
TableTwoId INT UNSIGNED NOT NULL,
Value FLOAT NOT NULL,
FOREIGN KEY (TableOneId) REFERENCES TableOne(TableOneId),
FOREIGN KEY (TableTwoId) REFERENCES TableOne(TableTwoId)
) ENGINE=InnoDB;
INSERT INTO DependentTable (Value, TableOneId, TableTwoId) SELECT 1.0, TableOne.TableOneId, TableTwo.TableTwoId FROM TableOne,TableTwo WHERE TableOne.Value='TableOneValue' AND TableTwo.Value='TableTwoValue';
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
If either 'TableOneValue' or 'TableTwoValue' doesn't exist, then the select will return null and 0 rows will be inserted - as expected. However, I was hoping to get some other piece of information that would allow me to determine which value didn't exist so I can create it.
The problem, at least in my actual situation, is that blindly creating each value to satisfy the foreign key reference would be overkill/expensive as there are multiple foreign keys and some of those dependent tables will have other required dependencies. I'd like to approach it a little more intelligently and determine which reference(s) didn't exist and only create those instead.
I could try to create the rows in dependent tables one-by-one, wait until a row is actually inserted ( versus getting a duplicate entry ), and then try the original insert again - but it feels like there should be a better/more elegant way to do this...
Thanks in advance for any suggestions/ideas...
What I'd do is insert everything into an intermediate table without the Foreign Keys. Then use that table to query against TableOne and TableTwo, to see which items in TableOneId and TableTwoId do not exist in TableOne and TableTwo. Then adjust, cleanup, etc.
INSERT INTO IntermediateTable(DependentId,
TableOneId,
TableTwoId,
Value) blah blah blah
The following will show you the missing values (repeat for TableTwo):
SELECT *
FROM IntermediateTable i
LEFT JOIN TableOne t1 ON t1.TableOneid = i.TableOneId
WHERE t1.TableOneId IS NULL
When cleanup is done, you can copy everything from the intermediate table into DependentTable with foreign keys defined.
INSERT INTO DependentTable SELECT * FROM IntermediateTable --everything has been cleaned up so that there are now no missing ids in TableOne and TableTwo.
Without this intermediate table, you lack something to use for a query against TableOne and TableTwo for undefined ids.