I'm trying to create new table by joining two tables, using the following query on a MySQL DBMS:
create table t11 as
SELECT * FROM banking
JOIN details ON details.[account number]= banking.[account number]
which is firing a syntax error. Where is the mistake in it?
Delimiting field and table names with brackets is SQL-Server notation. In MySQL you may want to use backticks instead.
CREATE TABLE t11 AS
SELECT *
FROM banking
JOIN details ON details.`account number` = banking.`account number`
Or avoid using them completely, if:
they're composed by one word
there's no correspondence with any MySQL reserved keyword.
Related
Joining multiple times to a temporary table. Getting this error:
Error Code: 1137. Can't reopen table: 'a'
I googled it and found that there is a restriction of some kind on referencing the same temporary table multiple times in a query. Can anyone explain why this restriction exists?
Here is an exmaple of simple query that will cause this error:
CREATE TEMPORARY TABLE foo
SELECT * FROM shopify_us limit 10;
SELECT *
FROM (
SELECT *
FROM shopify_us
LIMIT 10
) boo
LEFT JOIN foo a ON a.customer_id = boo.customer_id
LEFT JOIN foo b on b.customer_id = boo.customer_id
However, if I simply remove the 2nd join, I no longer encounter the error.
The restriction is that you can't reference a temporary table multiple times in the same query. For example, doing a self-join on a temporary table cannot be done. But you can use a temporary table by multiple subsequent queries.
Here's the comment in the MySQL Server code that explains the reason, right before it raises this error:
/*
We're trying to use the same temporary table twice in a query.
Right now we don't support this because a temporary table is always
represented by only one TABLE object in THD, and it can not be
cloned. Emit an error for an unsupported behaviour.
*/
https://github.com/mysql/mysql-server/blob/8.0/sql/sql_base.cc#L7284-L7289
THD refers to the data structure for session-scoped information. Temporary tables are scoped to the session in which they are created. They aren't references to tables that can be shared by multiple sessions.
In other words, it's just a limitation in the code with respect to the data structure that tracks temporary tables.
See also the history of this as a bug report dating back to 2005: https://bugs.mysql.com/bug.php?id=10327
There is a workaround in MySQL 8.0: you can us a common table expression instead, for some kinds of uses for which you would have used a temp table.
When I execute the query as mentioned below, it executes without any issues.
delete from table where entryID in
(
select * from (select max(entryID) from table where locationId = 2) as deleted
);
But, when I try to alter the same query as below, I see "Error Code: 1093. You can't specify target table 'table' for update in FROM clause". Why is it not accepting the below query?
delete from table where entryID in
(
select max(entryID) from table where locationId = 2
);
table is a reserved keyword (note the R next to TABLE), so it cannot be used for an identifer.Use a different name for your table, or encapsulate your table name in backticks:
delete from `table` where entryID in
(
select max(entryID) from `table` where locationId = 2
);
It's a restriction in MySQL. The error message pretty well spells out what the restriction is. (This is documented somewhere in the MySQL Reference Manual.)
It may make more sense to understand the reason that first query doesn't return an error. Why that is a workaround to the restriction. And that's because of the way that MySQL processes it. The query inside the parens is an inline view, which MySQL calls a derived table. What MySQL calls it makes sense, when we understand how MySQL operates.
MySQL first runs the inline view query and materializes the results into a temporary(ish) derived table.
It's very similar to the effect of doing a CREATE TEMPORARY TABLE foo ... and an INSERT INTO foo SELECT ....
After that's all squared away, and we have a derived table, then the outer query runs. The outer query references the derived table. In the example given, the derived table is assigned the alias "deleted". Notice that the FROM clause is referencing the derived table. That is to say, it doesn't reference to the target of the DELETE. So it doesn't violate the restriction.
I have an existing sql database which has 2 columns of data (part and variant), another one of our systems uses sql in a different table in the same database but columns are (part and type), how would i merge the two in a datagridview (part, variant and type) using the part to do the select statement in a way to pull the other two bits of info and include them in the same datagridview. In vb.net
Cheers,
Pete
My guess would be that you would need an outer join to get the data, assuming there is an unique key and it's "part", for example:
Select part, variant, type
from table1,table2
where table1.part = table2.part;
Assuming that each table is unique and there is no relationship to part, nor a key:
Select part, variant
from table1
UNION ALL
select part, type
from table2;
This is ANSI SQL, so you may have to adapt it to SQL Server syntax, but I think the intent is clear.
Bind the resultset to the DataGridView.
Does this answer you question?
R/
Prescott ....
I'm confused with MySQL. I'm querying a table with a primary key field called index.
SELECT * FROM content WHERE index = 7
Pretty simple right? I keep getting a syntax error though, around index = 7, error number 1064 (can't paste the error as it's on an iPhone simulator).
The field index contains a 7. It is the PRIMARY_KEY, and of type int(6)
The database works fine querying other fields and so on.
Are there settings I need to ensure are in place before I can query this field?
Much appreciated.
SELECT * FROM content WHERE `index` = 7
index is a reserved word in MySQL. You have to escape it with backticks. See here
Another suitable workaround (for a column identifier which is a reserved word) is to qualify the column with the table name or a table alias. My preference is to always use a table alias. In this example, we give the table content an alias of c, which we can then reference in the query:
SELECT c.* FROM content c WHERE c.index = 7
In this example, backticks are not required around the column name, because when it is qualified with a table alias, MySQL sees it as an identifier; a reserved word can't be qualified with a table name.
Using a table alias is a pattern we use in more complex statements, which reference multiple tables and/or reference the same table multiple times. Nothing prevents us from using the same pattern with simple statements.
And the backticks around identifiers is always allowed, even if they are required:
SELECT `c`.* FROM `content` `c` WHERE `c`.`index` = 7
Index is a reserved word, use
Select * from content
where `index` = 7
I need to alter a default value for a column in an existing table.
I assumed (and I actually found a few examples on the net "confirming" this) that I could do
ALTER TABLE table ALTER COLUMN column SET DEFAULT 'something' but I get syntax error and according to this http://msdn.microsoft.com/en-us/library/ms190273.aspx DEFAULT is not supported.
Next idea was to drop the default constraint from the database but I found that when the table was created the constraint was not named and SQL generates a random name with every deployment so I can't reference it by name.
I came up with a query from sys tables that returns the name of the constraint:
select o.name from sys.objects o
join sys.columns c on o.object_id = c.default_object_id
join sys.tables t on c.object_id = t.object_id
where t.name='TableName' and c.name='ColumnWithDefValue'
I'm about to write a query by string concatenation and I can't stop thinking about how there must be a better way to do all this. Any suggestions?
TL;DR version: what's the easiest way to alter a default constraint for a column in SQL Server R2?
Thanks
Looks like you have to drop the constraint and then recreate it
http://bypsoft.blogspot.com/2007/10/changing-default-column-values-sql.html