DROP TABLE in MySQL - mysql

I have created a MySQL table with the following code from a tutorial, I just put it into the MySQL console:
CREATE TABLE `test`.`name` (
`nameid` INT NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(45) NULL ,
`lastname` VARCHAR(45) NULL ,
PRIMARY KEY (`nameid`)
);
INSERT INTO `test`.`name`
(`firstname`,`lastname`)
VALUES
("TheBig","Monster"),
("Guy","Smiley"),
("Big","Bird"),
("Oscar","Grouch"),
("Alastair","Cookie");
With the python script given in the tutorial I am able to print each item.
But when it comes to erase the table, I have tried all possible combinations of:
DROP TABLE `test`
DROP TABLE test
DROP TABLE `test`.`name`
etc, but none will erase it. Moreover, If I use the initial table creation script again, it will add the entries to the table, so I know its a stored table within MySQL but cannot access it, delete it, or list it with:
SHOW DATABASES
or
SHOW TABLES
It is my first afternoon with MySQL, but don't know how to go further if the commands in the MySQL help are not working!
So the summary question is:
How to delete / access the table created with the script?

drop table test.name;
this should work

Related

Cannot make liquibase execute more than 1 script in one file

I have a spring-boot application and sqlite db.
If I write two scripts in one file, only first script make change to db. The table creates but no data inserts, though in log I see that both scripts were executed.
And if I write each script to separate file, then it is OK, table creates and data inserts.
How to make it execute more then one script in one single file?
CREATE TABLE IF NOT EXISTS acl_class
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
class character varying UNIQUE NOT NULL
);
INSERT INTO acl_class
(class)
VALUES
('models.User');
The same problem is with mysql db, but here my app falls with runtime error "Caused by: liquibase.exception.DatabaseException: You have an error in your SQL syntax;" But syntax is ok, and separately tables are creating without any problem:
CREATE TABLE IF NOT EXISTS acl_class
(
id INT AUTO_INCREMENT PRIMARY KEY,
class VARCHAR(255) UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS acl_sid
(
id INT AUTO_INCREMENT PRIMARY KEY,
principal boolean NOT NULL,
sid VARCHAR(255) UNIQUE NOT NULL
);
Thanks to SteveDonie the problem was solved easily: I just added to the begining of my file with multiply scripts this two lines:
--liquibase formatted sql
--changeset myname:create-multiple-tables splitStatements:true endDelimiter:;
and and it worked!

MYSQL copy fields from one table's structure and add them to another tables

I've added some additional fields for my table in db. Now I need to have this additional fields in other few tables. So the question is - can I somehow copy those fields from source table and add them to another tables? Both mysql console and phpmyadmin variants woulbe be nice. Thanks!
A phpmyadmin variant would be to export the table's structure only (Export->Custom->Choose "Structure"). After that, you will get something like this in the exported SQL file:
CREATE TABLE `table` (
`id` int(10) NOT NULL,
`name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
At that point, you can go ahead and remove the last line of the query and the parenthesis after the CREATE TABLE. Then, go ahead and substitute the [other_table] name and change the query to match the following:
ALTER TABLE `[other_table]`
ADD `id` int(10) NOT NULL,
ADD `name` varchar(50) DEFAULT NULL;
Notice how all I did was change CREATE to ALTER and add ADD before each field.
NOTE: This is not very useful on such a trivial example, but when dealing with large amounts of columns, it could prove somewhat useful.

Table doesn't exist on CREATE TABLE?

I'm trying to import a SQL dump to another server. It fails on the first line. I'm first creating the exp_actions table and then inserting a bunch of data into it, but I get this really weird error.
SQL query:
--
-- Database: `ee_cmssite`
--
-- --------------------------------------------------------
--
-- Table structure for table `exp_actions`
--
CREATE TABLE `exp_actions` (
`action_id` INT( 4 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`class` VARCHAR( 50 ) NOT NULL DEFAULT '',
`method` VARCHAR( 50 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `action_id` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =21;
MySQL said:
#1146 - Table 'site_ee.exp_actions' doesn't exist
Why doesn't it exist? I just instructed it to be created. I'm completely baffled. I've tried with and without IF_NOT_EXISTS
If anyone else comes across this seemingly bizarre error - see https://stackoverflow.com/a/11696069 for the solution.
I had the same symptoms and the cause was the same - moving to a new machine I took the old short-cut of simply copying the databases from the mysql/data directory that I needed directly into the new machine, however some were newer InnoDb types. This causes the Create Table throws table doesn't exist error. I had to drop the database and recreate it, then import from an sql dump.
According to the SQL script, the table exists in another database:
--
-- Database: ee_cmssite
-- --------------------------------------------------------
-- Table structure for table exp_actions
try to use ee_cmssite database instead.
While the error is not clear, I think this is related to the missing USE at the beginning of the file. mysqldump doesn't add a USE statement when you dump a single db. So you should add:
USE `ee_cmssite`;

Inserting a value into one table based on insert from another table

I have this table for users that stores their usernames and other data, thats done like this (stripped down):
CREATE TABLE `prod_users` (
`p_user_id` INT(11) NOT NULL AUTO_INCREMENT,
`p_user_name` VARCHAR(200) NOT NULL DEFAULT ''
`p_comp_name` VARCHAR(300) NOT NULL DEFAULT '',
PRIMARY KEY (`p_user_id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
Each time a user signs up, he'll provide a company name as well.
There's another table called prod_profiles, which stores profile details like phone nos. fax nos. etc.
CREATE TABLE `prod_profiles` (
`pf_gen_id` INT(11) NOT NULL AUTO_INCREMENT,
`pf_user_id` INT(11) NOT NULL DEFAULT '0',
`pf_user_name` VARCHAR(200) NOT NULL DEFAULT ''
`pf_comp_name` VARCHAR(300) NOT NULL DEFAULT '',
PRIMARY KEY (`pf_gen_id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
When a new user signs up and his details are added to prod_users, is it possible to automatically add his new user_id, user_name and comp_name details to prod_profile using MySql itself? Since each user will have a new p_user_id and we wont know it, it'll be difficult using php. Can this be achieved inside MySql itself without any problems?
It isn't difficult using PHP, since you have the LAST_INSERT_ID() available for use, be it via mysql_insert_id() or mysqli::$insert_id, PDO::lastInsertId() or whatever your API provides. As long as you call the two INSERT statements in immediate succession on the same script (it is connection dependent), MySQL will supply the correct p_user_id.
However, you can use an AFTER INSERT trigger to force MySQL to create the new row automatically:
CREATE TRIGGER build_profile AFTER INSERT ON prod_users
FOR EACH ROW
BEGIN
INSERT INTO prod_profiles
(pf_user_id, pf_user_name, pf_comp_name)
VALUES (NEW.p_user_id, NEW.p_user_name, NEW.p_comp_name)
END
Review the MySQL CREATE TRIGGER syntax reference for full details and options.
You can use the next mysql function: LAST_INSERT_ID(); which returns the last auto increased id.
Therefore , add a user and then add a prod_profile , while pf_user_id value will be the returned value of last_insert_id().
INSERT INTO `prod_users`(`p_user_name`,`p_comp_name`) VALUES('Dan' , 'Stackover')
INSERT INTO `prod_profiles`(`pf_user_id`,`pf_user_name`,`pf_comp_name`) VALUES(LAST_INSERT_ID(),'Dan','Stackover')
Please notice: I have to say , that storing the username and company_name twice for the same user in two different tables is a reall waste...
Consider re-thinking about your DB structre and logic.

MYSQL relational database

http://www.go4expert.com/forums/showthread.php?t=13386
im following the article above and i am getting error at the first quoted code
#1049 - Unknown database 'library'
CREATE TABLE `library`.`books` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 150 ) NOT NULL ,
`author_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `author_id` )
) ENGINE = INNODB
the other one generates with no problem but this one, why?
Your table name is prepended with the database name "library". Probably your DB is named differently. If you're executing within the DB that you're using just remove the "library." prefix.
CREATE TABLE `books` (
...
Remove the "library" part from the CREATE TABLE statement. The other block in that article works because it doesn't reference "library".
Remove the library. - just use what ever database you're using, ie
CREATE TABLE books (...
And while you're at it, remove all those unnecessary backticks - they are only required when using reserved words (which you should avoid like the plague anyway)
The table authors will be created in your default/current database, here the database library is to be used, but it hasn't been created. You should make a library database.
CREATE DATABASE library
Using a differently named database, or removing library from the create table statement may cause problems further down the tutorial.