MySQL Table does not exist, but it does - mysql

I am new to building databases, i have created a database for a website i am creating, and i keep getting this message "Executing SQL script in server
ERROR: Error 1146: Table 'mydb.franch' doesn't exist", when trying to forward engineer my database. The table does exist but it is not being read. (See image below). Please any help will be appreciated.

I can't add a comment, but in your image the table is "Franch" not "franch". Maybe that is the problem?

I may be wrong but I believe you are trying to access your table as (something like)
select * from 'mydb.franch'
or
select * from `mydb.franch`
In either case it's wrong and should be
select * from `mydb`.`franch`

MYSQL is case sensitive when creating a table, which means that you cannot create a table or variable with lower/upper case, and call it with upper/lower cases.
As such you must call the same name you used to create the table. In this case since the table is called mydb.Franch you would do: select * frommydb.Franch;`

Related

Check for column existence in mySQL

I know this question has been asked before, but for some reason the suggested solutions do not work in my setup.
I am calling a mySQL database from within matlab on windows, and need to check whether a given table has a specific column before my script commences calculating values for that column.
From previous answers, I get that the following should help me determine if col1 exists:
select exists (select * from table1 where col1=val1)
And this works fine if col1 exists. However, when it doesn't I get the following:
>> fetch(conn,'select exists (select * from model12B where col1=.5)')
Error using database/fetch (line 37)
[MySQL][ODBC 5.3(a) Driver][mysqld-5.5.37-log]Unknown column 'col1' in 'where clause'
This looks more like a MySQL error than a matlab error, hence why I'm framing it as a MySQL question. I can of course wrap this in a try-catch block, but that feels wrong. Can anyone suggest a good way to ask for existence without generating an error?
You can use the information_schema to tell you if the column exists in specified database and specified table :
select * from information_schema.columns where table_name='tablename' and table_schema='databasename' and and column_name='columnname'
Or you can use "show columns " command:
show columns from databasename.tablename where like 'columnname';
Of course, you can use the try - catch to test if a column exists of not. I don't think there is any side effect if you analyze the error message carefully.

Is there a way to copy the structure of a table (fields but no data) to a new table in MySQL?

For example, I have a table named movies. It has the fields/columns title VARCHAR(100) and runtime INT(5). It's loaded with 10,000 rows of data.
I want to create another table, let's call it movies_custom, that has all of the same columns, but with none of the data.
Is there a quick SQL statement to do this?
EDIT: Sorry, I noticed the SQL Server tag on this and assumed that was the technology you were using until I saw MySQL in the question title.
You can use the syntax CREATE movies_custom LIKE movies in MySQL
Sure!
In SQL Server you can do this query:
select top 0 * into movies_custom from movies
That creates the table structure without inserting any rows.
Very easy in MySQL:
CREATE newtable LIKE oldtable;
The other answers led me in the right direction, but I had to add the keyword TABLE in order to make it work with MySQL Workbench 6.
CREATE TABLE movies_custom LIKE movies;

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.

Conditionally add a column to a mysql table

I have a project where I want to be able to manage several instances of the same database on several people's localhosts. I want each developer to be able to reset their DB back to the canonical origin no matter what state they get their DB into. To this end I maintain a standard database file. It holds the schema using CREATE TABLE table_name IF NOT EXISTS {
However, I want to be able to add to the starting table structures as needed as this project moves along. To do this, I would love to be able to do something like the following ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name but that does not seem to exist. I did notice a stored procedure floating around the internet that solves this, but I wanted to know if something simpler is able to achieve the goal I have in mind. Thank you for the time and help.
It won't be possible to do it with plain SQL. Stored procedure should work the best: read information_schema and check if the column is present. If not - execute the alter statement.
One option is to execute your ALTER statement without checking anything:
ALTER TABLE table_name ADD column_name VARCHAR(40);
(change VARCHAR(40) to whatever you need)
If the column didn't already exist, then the statement creates it.
If the column already existed, the statement does nothing and returns an error. Just ignore the error and continue.

CREATE TABLE LIKE not working on InfiniDB?

I'm trying to automate a process in which an InfiniDB table is replaced. My idea is to create one table with the same structure as the original, load it with new data, and replace the original with this new one.
The problem is that when you do:
CREATE TABLE temp_table LIKE original_table;
the service replies the good old:
ERROR 138 (HY000): The syntax or the data type(s) is not supported by InfiniDB. Please check the InfiniDB syntax guide for supported syntax or data types.
I was thinking to do a SHOW CREATE TABLE, replace the name, and run the result, but... that's a bit ugly.
Do you know of a better way?
Thanks in advance!!