How to display a mysql query - mysql

I want to look at a query used to create a table. The table was created already. I've seen this done in the past in the command console, but I cant find how to do this now. It was something like:
show create; --OR
select create; --none of these work
After running that it will display the query and some details about mysql.

SHOW CREATE TABLE tbl_name is the syntax in mysql. MySql Refernce.

maybe you're talking about show create table

Related

MySQL not allowing to create table

I'm learning about MySQL, I just made my first databank, but when I make a table called "pessoas", as shown in an Youtube tutorial, it showed these two atached error messages, and didn't let me see the table created. I've tried to put back accents surrounding the words, but didn't work. Can someone explain me what's going on?
Your SELECTa are gibberish for mysql that is why they fail
See you second select for example
SELECT
Db as scopeuserhostSelect_priv
AS Selectlnsert_priv
AS lnsertUpdate_priv
AS UpdateDelete_priv
AS DeleteCreate_priv
AS CreateDrop_priv
AS DropGrant_priv
AS GrantReferences_priv
AS Referenceslndex_priv
AS IndexAlter_priv
AS AlterCreate_View_priv
AS Create ViewShow_view_priv
AS Show viewTrigger_priv
AS TriggerDelete_versioning_rows_priv
AS Delete versioning rows
FROM mysql.db
WHERE cadastro2 likedb
A query looks something like this
SELECT `test12`.`id` as id,
`test12`.`usr_id` as usr_id,
`test12`.`category` as category,
`test12`.`comp_id` as comp_id,
`test12`.`position`as position,
`test12`.`description` as description,
`test12`.`country` as country,
`test12`.`state` as state,
`test12`.`city` as city,
`test12`.`start_date` as start_date,
`test12`.`end_date` end_date,
`test12`.`timestamp` as timestamo
FROM `testdb`.`test12`
WHERE `category` LIKE 'tesmeifyou can';
every column is separated by comma, an alias for a column needs an actual column, and LIKE needs to compare a string
Here you find some more information on SELECT
A.A.,
your CREATE TABLE runs successfully on MariaDB 10.4.13.
The actual error messages are related to queries that retrieve information about database users, I guess these queries come from the MySQL Workbench itself.
My best guess is that your database and Workbench have different versions. Personally I am not a big fan of the Workbench (only for query profiling), so maybe try out another client: MySQL commandline, HeidiSQL, phpMyAdmin, ...
Best regards,
Martin

How to SHOW CREATE TABLE in a particular database in MySQL?

It is possible to get CREATE TABLE statement in specific database without using USE before?
Something like SHOW CREATE TABLE my_table IN my_database; (this doesn't works).
Thanks
show create table database_name.table_name;

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;

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.

mysql create table like to query?

Is it possible to use the create table query (create table t2 like t1) to get the query instead the actual table? Kind of like duplicating some management consoles Send To editor feature?
I think you're looking for SHOW CREATE TABLE tablename;
'tablename' can be the table within the current database, if you've selected one, or you can qualify it as in 'dbname.tablename'. The output can be used to create the table again. In fact, I sometimes use it in scripts that need to either use an existing table or create it if it doesn't exist. First I'll get the table creation info as shown above, and then I'll use something like"
CREATE TABLE IF NOT EXISTS tablename .........
Where '..........' is all the goodies that SHOW CREATE TABLE tablename gives me.
If you're interested in a good tutorial on MySQL (the database), as well as SQL (the language), you might have a look at the O'Reilly book, Learning MySQL. It's pretty good.