I am getting error code 105 using create view - mysql

[![I am getting error code: 1050 (as you can see at from the output in the image) Table already exists, when it doesnt. Can anyone help me with this?
The question reads: Create a view called ‘computer_science_students’ for the SELECT query created under Index step ]1]1

Sounds like you have Schroedinger's table...
you probably have a broken table. Try:
Syntax
The syntax for the DROP VIEW statement in MySQL is:
DROP VIEW [IF EXISTS] view_name;
view_name - The name of the view that you wish to drop.
IF EXISTS - Optional. If you do not specify this clause and the VIEW does not exist, the DROP VIEW statement will return an error.
If you have sufficient permissions, delete the data files (in /mysql/data/db_name)

Related

How do I fix a mysql table that doesn't exist but thinks it does

There's a table in a database of mine, let's call it table_x, that used to get created and destroyed all the time. Now that's not happening, so the table should either exist or not, and actually it should not.
When I try to get a mysqldump of the database I systematically get this error:
Error: Couldn't read status information for table table_x ()
mysqldump: Couldn't execute 'show create table `table_x`': Table 'xxxxx.table_x' doesn't exist (1146)
Like mysqldump for some reason still thinks that the table exists and when it tries to dump it it triggers the error.
SHOW TABLES does NOT show the table.
However in information_schema.TABLES it's present.
I guess that's the problem, that information_schema somehow got out of sync with reality.
How do I "repair" this inconsistency?
I tried deleting the table with
DROP TABLE table_x
but unsurprisingly I get an error that the table doesn't exist.
To avoid such kind of error during drop a table it's a good habit to use IF Exist and IF NOT EXIST as:
DROP TABLE IF EXISTS table_x;
CREATE TABLE IF NOT EXIST table_x;

MYSQL if table exit run something otherwise run another thing [duplicate]

I'm stumped, I don't know how to go about doing this.
Basically I just want to create a table, but if it exists it needs to be dropped and re-created, not truncated, but if it doesn't exist just create it.
Would anyone be able to help?
Just put DROP TABLE IF EXISTS `tablename`; before your CREATE TABLE statement.
That statement drops the table if it exists but will not throw an error if it does not.
Just use DROP TABLE IF EXISTS:
DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` ( ... );
Try searching the MySQL documentation first if you have any other problems.
Well... Huh. For years nobody mentioned one subtle thing.
Despite DROP TABLE IF EXISTS `bla`; CREATE TABLE `bla` ( ... ); seems reasonable, it leads to a situation when old table is already gone and new one has not been yet created: some client may try to access subject table right at this moment.
The better way is to create brand new table and swap it with an old one (table contents are lost):
CREATE TABLE `bla__new` (id int); /* if not ok: terminate, report error */
RENAME TABLE `bla__new` to `bla`; /* if ok: terminate, report success */
RENAME TABLE `bla` to `bla__old`, `bla__new` to `bla`;
DROP TABLE IF EXISTS `bla__old`;
You should check the result of CREATE ... and do not continue in
case of error, because failure means that other thread didn't finish
the same script: either because it crashed in the middle or just
didn't finish yet -- it's a good idea to inspect things by yourself.
Then, you should check the result of first RENAME ... and do not
continue in case of success: whole operation is successfully
completed; even more, running next RENAME ... can (and will) be
unsafe if another thread has already started same sequence (it's
better to cover this case than not to cover, see locking note below).
Second RENAME ... atomically replaces table definition, refer to
MySQL manual
for details.
At last, DROP ... just cleans up the old table,
obviously.
Wrapping all statements with something like SELECT GET_LOCK('__upgrade', -1); ... DO RELEASE_LOCK('__upgrade'); allows to just invoke all statements sequentially without error checking, but I don't think it's a good idea: complexity increases and locking functions in MySQL aren't safe for statement-based replication.
If the table data should survive table definition upgrade... For general case it's far more complex story about comparing table definitions to find out differences and produce proper ALTER ... statement, which is not always possible automatically, e.g. when columns are renamed.
Side note 1:
You can deal with views using the same approach, in this case CREATE/DROP TABLE merely transforms to CREATE/DROP VIEW while RENAME TABLE remains unchanged. In fact you can even turn table into view and vice versa.
CREATE VIEW `foo__new` as ...; /* if not ok: terminate, report error */
RENAME TABLE `foo__new` to `foo`; /* if ok: terminate, report success */
RENAME TABLE `foo` to `foo__old`, `foo__new` to `foo`;
DROP VIEW IF EXISTS `foo__old`;
Side note 2:
MariaDB users should be happy with CREATE OR REPLACE TABLE/VIEW, which already cares about subject problem and it's fine points.
I needed to drop a table and re-create with a data from a view.
I was creating a table out of a view and this is what I did:
DROP TABLE <table_name>;
CREATE TABLE <table_name> AS SELECT * FROM <view>;
The above worked for me using MySQL MariaDb.

Error creating MySQL view in phpMyAdmin

I'm having a hard time trying to create a view in phpMyAdmin. I have a database named myDB and a table named myTable.
In phpMyAdmin I click on the SQL tab, type in :
SHOW CREATE VIEW myView;
I got this error MySQL said:
#1146 - Table 'myTable.myView' doesn't exist
I don't understand this error message at all, of course it doesn't exist, otherwise why would I want to create it in the first place? And why wouldn't mySQL allow me to create it? How do I create a view?
Thanks
SHOW CREATE VIEW
The syntax you are using is not for creating view in SQL.
it is to show the views you have created in SQL
You need to use the following syntax to create a view
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
ALTER ALGORITHM = UNDEFINED DEFINER=**[YOUR_USERNAME]**#localhost VIEW **[YOUR_VIEW_NAME]** AS [YOUR_VIEW_QUERY];
Change the bold text above, example:
ALTER ALGORITHM = UNDEFINED DEFINER=`dadu_keeve`#`localhost` VIEW `view_banner` AS select `mst_banner`.`banner_uid` AS `banner_uid`,`mst_banner`.`banner_img` AS `banner_img`,`mst_banner`.`banner_alt` AS `banner_alt`,`mst_banner`.`banner_caption` AS `banner_caption`,`mst_banner`.`banner_link` AS `banner_link`,`mst_banner`.`banner_sort` AS `banner_sort`,`mst_banner`.`banner_tipe` AS `banner_tipe`,if((`mst_banner`.`banner_tipe` = 0),'BOX','FULL WIDTH') AS `banner_tipe_desc` from `mst_banner` ;

SQL: Drop Column Syntax Error

I want to drop a column from my table. I have tried executing the following syntax:
ALTER TABLE user
DROP COLUMN departmentid;
On execution, it hits me with an error stating that the statement could not be prepared. Can anyone tell me why it's doing this? I get the same error when I try renaming a column.
If you are using MySQL, Try using
DROP departmentid;
Use BackTicks 'column name' in the query.
It might not be possible to DELETE, INSERT or ALTER a table in online query builders. because once you drop tables, others cannot play around with same table. So I dont thing you have privileges to do so. If you are learning install xamp which has installed apache server and MySQL.
Hope this info helps!
I solved the problem by executing below query:
I need to remove a column and all the entries from that column to free my DB size
my initial table structure is as shown below:
CREATE TABLE words(_id,word,synonyms,favorite,history,updDate)
And I wanted the table in below form
CREATE TABLE words(_id,word,favorite,history,updDate)
So I executed below query and it removed "synonyms" column
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(_id,word,favorite,history,updDate);
INSERT INTO t1_backup SELECT _id,word,favorite,history,updDate FROM words;
DROP TABLE words;
CREATE TABLE words(_id,word,favorite,history,updDate);
INSERT INTO words SELECT _id,word,favorite,history,updDate FROM t1_backup;
DROP TABLE t1_backup
COMMIT;

If table exists drop table then create it, if it does not exist just create it

I'm stumped, I don't know how to go about doing this.
Basically I just want to create a table, but if it exists it needs to be dropped and re-created, not truncated, but if it doesn't exist just create it.
Would anyone be able to help?
Just put DROP TABLE IF EXISTS `tablename`; before your CREATE TABLE statement.
That statement drops the table if it exists but will not throw an error if it does not.
Just use DROP TABLE IF EXISTS:
DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` ( ... );
Try searching the MySQL documentation first if you have any other problems.
Well... Huh. For years nobody mentioned one subtle thing.
Despite DROP TABLE IF EXISTS `bla`; CREATE TABLE `bla` ( ... ); seems reasonable, it leads to a situation when old table is already gone and new one has not been yet created: some client may try to access subject table right at this moment.
The better way is to create brand new table and swap it with an old one (table contents are lost):
CREATE TABLE `bla__new` (id int); /* if not ok: terminate, report error */
RENAME TABLE `bla__new` to `bla`; /* if ok: terminate, report success */
RENAME TABLE `bla` to `bla__old`, `bla__new` to `bla`;
DROP TABLE IF EXISTS `bla__old`;
You should check the result of CREATE ... and do not continue in
case of error, because failure means that other thread didn't finish
the same script: either because it crashed in the middle or just
didn't finish yet -- it's a good idea to inspect things by yourself.
Then, you should check the result of first RENAME ... and do not
continue in case of success: whole operation is successfully
completed; even more, running next RENAME ... can (and will) be
unsafe if another thread has already started same sequence (it's
better to cover this case than not to cover, see locking note below).
Second RENAME ... atomically replaces table definition, refer to
MySQL manual
for details.
At last, DROP ... just cleans up the old table,
obviously.
Wrapping all statements with something like SELECT GET_LOCK('__upgrade', -1); ... DO RELEASE_LOCK('__upgrade'); allows to just invoke all statements sequentially without error checking, but I don't think it's a good idea: complexity increases and locking functions in MySQL aren't safe for statement-based replication.
If the table data should survive table definition upgrade... For general case it's far more complex story about comparing table definitions to find out differences and produce proper ALTER ... statement, which is not always possible automatically, e.g. when columns are renamed.
Side note 1:
You can deal with views using the same approach, in this case CREATE/DROP TABLE merely transforms to CREATE/DROP VIEW while RENAME TABLE remains unchanged. In fact you can even turn table into view and vice versa.
CREATE VIEW `foo__new` as ...; /* if not ok: terminate, report error */
RENAME TABLE `foo__new` to `foo`; /* if ok: terminate, report success */
RENAME TABLE `foo` to `foo__old`, `foo__new` to `foo`;
DROP VIEW IF EXISTS `foo__old`;
Side note 2:
MariaDB users should be happy with CREATE OR REPLACE TABLE/VIEW, which already cares about subject problem and it's fine points.
I needed to drop a table and re-create with a data from a view.
I was creating a table out of a view and this is what I did:
DROP TABLE <table_name>;
CREATE TABLE <table_name> AS SELECT * FROM <view>;
The above worked for me using MySQL MariaDb.