Creating a table as a sql script - mysql

I need to create a table with a file or anything, everything needs to be done as a sql script.
Can someone help me create a table without a csv file,
The name of the table is "videos"
The rows will be:
unique id
title
minutes
URL

When creating table to database via script you need to create a file where you will define structure of table with DDL (Data definition language).
For example
Create file table.sql. Open file and use CREATE TABLE statement for table creation.
CREATE TABLE videos(
id INT NOT NULL AUTO_INCREMENT,
minutes INT NOT NULL,
title VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL,
PRIMARY KEY ( id )
);
Afterwards you can run this script in many different ways. For example in Linux based operating systems you can run script with command mysql in form mysql -u user -p database_name < table.sql, where user is your username and database_name is name of database for which you want to create table.

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!

Dynamically create tables and copy data from MySQL to a SQL Server Database

I am able to create a table manually in SQL Server by checking columns from MySQl table. And then able to move data from MySQL to SQL Server table.
Example: MySQL table = Employee
Describe Employee;
Output
Field, Type, Null, Key, Default, Extra
EmpId int(10) NO PRI 0
Name varchar(100) YES
Age int(10) YES 18
EmailId varchar(100) NO
Using this I am creating same table in SQL Server
Drop table MsSQLdb..Employee;
CREATE TABLE Employee (
EmpId int NOT NULL PRIMARY KEY DEFAULT 0,
Name varchar(100),
Age int de,
Name varchar(100) NOT NULL DEFAULT 18
);
Now using OpenQuery to copy the data from MySQL to SQL Server :
select EmpId,Name,Age,EmailId into MsSQLdb..Employee
from
OPENQUERY(LinkedServer, 'SELECT EmpId,Name,Age,EmailId FROM mySQL_db.Employee')
But, daily my mySQL_db.Employee table gets more columns or less. So daily I need to manually Map this columns in the above queries to redo the task. Is there any dynamic way for this task?
I have around 40-tables and each table have around 30+ columns. so looking for any a dynamic way.
You can go for SELECT * INTO for loading data into tables. More on SELECT
I assume that you are fine for DROP & RECREATE and simple scenario of loading data. You might not get right datatype as datatype is automatically decided by SQL Server based on initial set of rows.
DROP TABLE dbo.Employee;
SELECT * INTO dbo.Employee
from
OPENQUERY(LinkedServer, 'SELECT * FROM mySQL_db.Employee')
Indexes, constraints, and triggers defined in the source table are not
transferred to the new table, nor can they be specified in the
SELECT...INTO statement. If these objects are required, you can create
them after executing the SELECT...INTO statement.

mysql export separate create table and alter

I'm currently trying to find a way to separate the mysql export function (using phpmyadmin and/or shell)
Following situation:
I have two databases which were the same at some state but over time only one got updated. So for example:
- database1 has table "users" with columns "UID" and "username"
- database2 has table "users" with columns "UID" "username" and "status"
Now i want to export database2 and import it to database1. database2 contains data I DONT need anymore. But database1 contains important data. So I need something that gives me the possibility to somehow "merge" these databases without data loss on database1 and without merging data from database2.
The merge should look if the table exists, if not -> create it
The merge should look if the columns exist in the table, if not -> create it
What I have tried so far:
I've first tried it with the inbuilt PHPmyadmin export function (advanced options). This would give me the following result:
CREATE TABLE IF NOT EXISTS `users` (
`UID` int(11) NOT NULL,
`username` varchar(25) NOT NULL,
`status` int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When I import this, of course, nothing special will happen since the table already exists and so the 'status' column does not get created.
On my research I found a possibility to export a database separated from table creation using shell using the command (blacked out database name and user/pass):
mysqldump -t --insert-ignore --skip-opt -u USER -p PASSWORD -h 127.0.0.1 database > database.sql
But this gives me an insert option with data (which I dont need)
INSERT IGNORE INTO `users` VALUES (1,"bla",1);
Is there any different possibility?
Sure I know I could write it by hand to alter the columns into the table but it sure has to be automatic since its not only the table I named in the example.
To be 100% clear what I'm trying to achieve, a pseudo handwritten sql script:
CREATE TABLE IF NOT EXISTS 'users';
For every column -> IF NOT EXISTS column in 'users' -> ALTER TABLE 'users' ADD column def;
You can use MySQLWorkBench as well. Look video instruction

"Proper" place to store description about a MySQL database

Is there a proper place to store a high level description of a database? Something along the lines of "This database is used to store XYZ for use by ABC". It's not necessarily information one would need to query, but something that would useful for someone administering the system (i.e. me in a few months when I'm trying to remember what I was trying to accomplish a few months ago.).
This seems like something that someone would have asked before (or information that is readily findable), but none of my searching came up with anything relevant. Most of what I found was for displaying the structure of the database itself.
Comment metadata is not available for MySQL databases, but you can create a table to store some comments: (I have this table in a generic tools database)
-- Create a table to store db name and fields as need.
create table dbinfo(
id int not null primary key auto_increment,
db_name varchar(64) not null collate utf8_bin,
db_comment varchar(255),
unique (db_name)
) default charset utf8;
To fill/update dbinfo table with current databases:
insert into dbinfo (db_name)
select SCHEMA_NAME
from INFORMATION_SCHEMA.SCHEMATA
where SCHEMA_NAME not in (select db_name from dbinfo);
You will only need to maintain dbinfo table until MySQL enables database comments.

DROP TABLE in 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