sql syntax error with VERY basic command? why? - mysql

I am trying just create a table using MySql Workbench. Here's the sql command:
CREATE TABLE `bmxData`.`new_table` ();
yet I get this error while executing this :-
Executing:
CREATE TABLE bmxData.new_table ();
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
SQL Statement:
CREATE TABLE bmxData.new_table ()
Any idea why? I have no idea what I'm doing wrong...

You need to add at least one column:
CREATE TABLE `bmxData`.`new_table`(col INT);
Some RDBMS allow to define table without explicit user column like PostgreSQL:
CREATE TABLE tab();
DBFiddle Demo

There is some discussion in relational theory of the meaning of tables with zero columns. Also described as relations of zero degree.
Relational theory researcher C. J. Date refers to TABLE_DUM and TABLE_DEE. Both have no columns, but the difference is that TABLE_DUM has no columns and no rows, whereas TABLE_DEE has one row (even though that row has no columns).
See this excerpt from "Database in Depth: Relational Theory for Practitioners": https://flylib.com/books/en/2.214.1.38/1/
The following query will therefore return no rows:
SELECT * FROM TABLE_DUM CROSS JOIN MyTable;
Whereas this query will return the same rows and columns as MyTable:
SELECT * FROM TABLE_DEE CROSS JOIN MyTable;
So there's some precedent and significance for a table with no columns. You can think of it by analogy as the role of 0 and 1 in multiplication:
0 x any number is 0
1 x any number is the same number
HOWEVER, standard SQL doesn't allow it. SQL doesn't exactly implement all of the concept of relational theory faithfully. In this case, standard SQL defines a table as having at least one column. The designers of SQL decided that a table with no columns isn't so interesting as to justify support in the SQL language.
If PostgreSQL or some other implementation of SQL decides to allow a table with no columns, they are doing it as an extension to standard SQL.

Related

My SQL Syntax error for multiple commands in one query, working for each command running separately

I'm trying to run the following MySQL command:
USE database_name;
DROP TEMPORARY TABLE IF EXISTS only_with_balance;
DROP TEMPORARY TABLE IF EXISTS keys_to_match;
CREATE TEMPORARY TABLE only_with_balance as (
SELECT
*
FROM
transactions t
WHERE
t.balance is not NULL
and (t.transaction_status_id = 4 or t.transaction_status_id = 5)
and (t.date between "2022-05-01" and "2022-08-24" )
);
But I'm getting a syntax error while trying to run the all the commands at once.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP TEMPORARY TABLE IF EXISTS only_with_balance;
DROP TEMPORARY TABLE IF EXIST' at line 2
When I run each command separately, the result is the expected.
Can someone help me here?
What am I forgetting?
In MySQL, by default the query interface only allows one SQL statement per call.
There's an option to enable multi-query per call, but it must be set at connect time. Some MySQL connectors do this by default, or allow it as an option, but some do not. You didn't say if you're writing code or if you're submitting this set of queries through a client (though you tag the question 'dbeaver' you don't say anything else about that). So I can't guess what interface you're using for these queries.
Anyway, there's no advantage to using multi-query. The default mode is one SQL statement per call. That's what I do.
Using the default mode of a single SQL statement per call has some advantages:
Supports prepared statements and bound parameters (you can't run multiple statements in a single prepare call, even if you enable multi-query).
Simplifies processing errors and warnings.
Simplifies processing result sets.

Create Table in SQL where table name is from digits

When I try do next
mysql> CREATE TABLE '20181020';
sql return an error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax to use
near ''20181020'' at line 1
How can I solve it?
You needt to wrap identifier with backticks:
CREATE TABLE `20181020`(id INT);
I propose not to do so and use proper meaningful naming. Using date as name of table suggest that it could be table-per-date antipattern.
Related article: SELECT * FROM sales + #yymm
your can also use double quote for this type of table name
CREATE TABLE "20181020" (id INT);
insert into "20181020" values(1)
But this type of naming is not standard practice
The other answers cover the solution, which is to use backticks.
Let me add that using non-standard identifiers is a bad idea. If you start naming columns as number as well, who wants to figure out the differences between 20181020.1, 20181020.1, and 20181020.1. Is it so hard to use a prefix: t_20181020?
But you don't want to do that either. Let me explain. Presumably, if you have one table called 20181020, then tomorrow you will want another table 20181021. This is a really bad database design.
You should be putting all the data into a single table, with a date column specifying the date. You can then run queries over one table to see changes over time -- for instance.
If the tables are large and you want the ability to manipulate each day separately, then you use table partitioning to store each day's worth separately. This is handy, because partitions can be archived, restored, and dropped independently of other partitions.

Asigning 2 integer values to a single item in a table in MySQL?

I have this line in an SQL table:
Income NUMBER(12,2),
And I want to recreate the table in mysql, but it's throwing me this error:
error 1064 (42000): You have an error in your SQL syntax; check the manual to use near '2))' at line 4
This is how I'm creating the table:
CREATE TABLE Doctors(
DoctorId INTEGER(5) PRIMARY KEY,
DoctorName VARCHAR(20),
Income INTEGER(12,2));
How can I do this in mysql?
*I have been able to replicate the table in sqlite without errors.
If you are trying to do what your question title states (i.e. store two integer values into the same column), you may be looking to use an array. Databases like PostgreSQL support arrays (see PostgreSQL array documentation), but MySQL doesn't do so natively (see MySQL Documentation). However, the MySQL documentation suggests using a language extension to serialise/deserialise the data (see PHP example).
Otherwise, it may be that you are simply looking for a DECIMAL type:
Income DECIMAL(12,2)
See MySQL Documentation for fixed-point types

MySQL dots in table names

I have an SQL database called copra4server, with a table called db_version.
I enter:
USE copra4server;
SELECT version FROM db_version;
And I get ERROR 1146 (42S02): Table 'copra4server.db_version' doesn't exist
Why is it trying to get a table named dbname.tablename and what does this dot notation mean?
The error is very clear. Your table db_version is not present in the copra4server database.
When you are saying USE copra4server; then it means that you want to use your copra4server database
And when you select from the table db_version the table is not present hence results in the error.
I think you are a bit confused about "databases" versus "database servers".
MySQL is a database server. You can connect to it and see databases. Note the plural here. One server, many databases.
One of the databases on your server is called copra4server. When you say:
USE copra4server
You are saying "my current database is now copra4server". So, any table you reference will be assumed to be in that database. Your query:
SELECT version FROM db_version;
Is really -- under the hood -- saying
SELECT version FROM copra4server..db_version;
And, the table does not exist, causing the error message.
By the way, if you are looking for the database version, the proper syntax is:
SELECT version()
That's how MySQL refers to tables (db.table) so it is clear that it is talking about the table in that db.
Check your spelling if you're sure the table exists.

MySQL Drop table doesn't work for prefix

I am trying to drop tables with wp_ prefix but its giving error below
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE TABLE LIKE 'wp_%'' at line 1
Here is my query
"DROP TABLE WHERE TABLE LIKE '{$wp}%'"
What is wrong in this query? Please help
As far as I know, you can't selectively delete tables. You have to specifically delete each table, since deletion can't use a filter. You could probably use the metadata to get the names of all of your tables, and then find out in your code which ones start with wp_. Then, you would just loop through your list of tables to delete and then delete them with drop table [table-name];.
To get the list of table names from metadata, use select table_name from information_schema.tables;.