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!
Related
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.
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.
Having some trouble putting together a table with a unique value. The current setup I have for two tables which for all intents and purposes can be the same as the one below. My problem is that I'm trying to use the auto incremented value as the primary key due to redundancies in the data pulls, but since it's for two tables, I want to concatenate a string to the auto incremented value so my ID column would be:
Boop1, Boop2, Boop3 and Beep1, Beep2, Beep3, instead of 1, 2, 3 for both tables so they are differentiated and thus do not have duplicate values when I put in constraints
CREATE TABLE IF NOT EXISTS `beep`.`boop` (
`ID` INT NOT NULL AUTO_INCREMENT,
`a` VARCHAR(15) NOT NULL,
`b` VARCHAR(255) NOT NULL,
`c` VARCHAR(50) NOT NULL,
`d` VARCHAR(50) NOT NULL,
PRIMARY KEY(`ID`))
ENGINE = InnoDB;
LOAD DATA LOCAL INFILE 'blah.csv'
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES SET DCMID = CONCAT('DCM'+`DCMID`);
The code in boldface is optional and was only there to try concatenating which I already know does not work
I realize that this would not be able to work since my datatype is an INT, so what would I have to do to to keep my autoincrement while differentiating
For reference, I am using a LOAD DATA LOCAL INFILE, and not INSERT (and I don't think bulk insert is available with mySQL workbench). otherwise, i would bulk insert and just utilize last_insert_id
The goal is to plug and play for a datapull I perform so I can archive my data quickly and run queries to grab the data I need in the future. using one insert line per row of data i have would be extremely inefficient
I was utilizing a delimiter function earlier with a trigger, which in theory would have worked by altering the table after the load data infile, but that requires SUPER privileges which I do not have
is what i'm asking for even possible, or should i give up and find a workaround, or try grabbing super priveleges and trying the delimiter trigger
I'm not sure why you would do that, but you could use two different indexes. The first one is the auto-increment, and is populated by MySQL. The second one is your "prefixed" key, and is created by a trigger called after insert, where you update the column based on the first key and the prefix that you want.
I have a task that must be executed by MySql (v5.1.72) stored procedure.
The task consists of several steps:
select rowset from one table by some condition
delete all rows, containing in rowset from step 1 (actually, delete rows by condition from step 1)
return from procedure rows, retrieved on step 1 (that were deleted on step 2)
And this procedure have some additional constraints:
it's expected to be invoked rather frequently, so there's a possible problem with creating temporary tables in request.
all data assigned to procedure call must be removed after procedure returned result and finished working. So, if result data is stored in table, this table must be automatically removed after procedure call.
Is it possible to solve this problem without additional tables?
And if not, how this can be done the best way?
I would create a table that has a extra column name Guid, of type CHAR(38).
Something like this.
CREATE TABLE `test_table` (
`TestId` int(11) NOT NULL,
`Value` varchar(45) DEFAULT NULL,
`Value1` varchar(45) DEFAULT NULL,
`Guid` char(38) NOT NULL,
PRIMARY KEY (`TestId`),
KEY `Guid` (`Guid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The index KEY Guid (Guid) will help in the SP to faster process the data.
If you in your procedure define a golbal variable
DECLARE v_Guid CHAR(38);
Then you can set the value like this
SET v_Guid = SELECT UUID();
This would return a string like this 18d2e4ea-5d8c-11e3-b923-00ff90aef4a9.
Now you can use this value v_Guid for every action that you make. SELECT, INSERT or DELETE.
When you later want to delete the rows. You just execute
DELETE FROM test_table WHERE Guid=v_Guid;
In this manner, the SP can use the same table even if the procedure runs and overlaps eachother. You never DROP the table, just delete the rows with the related GUID.
Have you considered someting like this?
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