Basically, what i want to do is to parse a *.sql file and select all CREATE TABLE Statements. Example below:
-- ----------------------------
-- Table structure for aes_interval
-- ----------------------------
DROP TABLE IF EXISTS `aes_interval`;
CREATE TABLE `aes_interval` (
`processcode` bigint(20) NOT NULL,
`overstaying` int(11) NOT NULL,
`floating` int(11) NOT NULL,
PRIMARY KEY (`processcode`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci;
by running a RegEx that would select all in between (...) i will be able to get an output/substringed text like below:
CREATE TABLE `aes_interval` (
`processcode` bigint(20) NOT NULL,
`overstaying` int(11) NOT NULL,
`floating` int(11) NOT NULL,
PRIMARY KEY (`processcode`) USING BTREE
)
i've tried CREATE TABLE\w+\((.|\n)*?\) but it only returns the output below:
CREATE TABLE `aes_interval` (
`processcode` bigint(20)
hopefully, i can pick up the proper regex here.
Assuming the CREATE TABLE statements are terminated by semicolon, and therefore that semicolon does not appear until the end of each statement, then the following regex should work:
(CREATE TABLE.*?;)
There is a caveat that the above would need to run in a tool/language with dot configured to match newline. And also multiline match mode would need to be enabled.
Demo
Edit:
I suspect that lazy dot is not working on Sublime Text. If so, then you can try the following pattern:
(CREATE TABLE[^;]*)
You can try this (CREATE TABLE.*?((\(.*?\)).*?)+\)) in https://regex101.com/
Related
I created an in memory H2 database with JDBC URL
jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
The H2 web console refuses to let me do an auto_increment. I've seen serial for Postgres, but that doesn't work either.
At it's simplest, it hates:
create table test(id bigint auto_increment);
Syntax error in SQL statement "create table test(id bigint [*]auto_increment)"; expected "ARRAY, INVISIBLE, VISIBLE, NOT NULL, NULL, AS, DEFAULT, GENERATED, ON UPDATE, NOT NULL, NULL, DEFAULT ON NULL, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
create table test(id bigint auto_increment) [42001-214] 42001/42001 (Help)
Why do I care:
My code base was failing with NULL not allowed for column "REV". I'm using JPA/Hibernate + Liquibase. In order to try the suggestions at
Hibernate Envers + Liquibase: NULL not allowed for column "REV"
I'm trying to add an auto_increment to my Liquibase changelog file.
You can use the SQL Standard's generation clause GENERATED ALWAYS AS IDENTITY. For example:
create table test (
id bigint generated always as identity,
name varchar(10)
);
See PostgreSQL Example.
It works the same way in H2. For example:
create table test(id bigint generated always as identity, name varchar(10));
insert into test (name) values ('Chicago') ;
select * from test;
Result:
ID NAME
-- -------
1 Chicago
I am trying to create tables in an SQLite database with sqlite3.
The command $ sqlite3 mydb < mytables.sql produce the following error: Incomplete SQL: ??C.
mytables.sql is:
CREATE TABLE SizeCulture (
SizeCultureID INTEGER PRIMARY KEY ASC,
SizeID INTEGER NULL,
CultureID TEXT NULL,
Name TEXT NULL,
Description TEXT NULL,
Abbreviation TEXT NULL,
);
CREATE TABLE Size(
SizeID INTEGER PRIMARY KEY ASC ,
Creation TEXT NOT NULL,
Modification TEXT NOT NULL,
Deleted INTEGER NOT NULL,
);
/****** Object: Table [Ordering].[BarCode] Script Date: 11/09/2011 14:58:19 ******/
CREATE TABLE BarCode(
BarCodeID INTEGER PRIMARY KEY ASC NOT NULL,
BarCodeValue TEXT NOT NULL,
);
This was modified from a script generated by SQL Server, where some tables need to be replicated on an Android device.
The above is just a set of repeating create table statements. From what I understand, SQLite follows standard SQL (like MySQL or postgres).
Though I can't test it at the moment, I think it's the trailing commas that are confusing it (for example, the comma at the end of Abbreviation TEXT NULL,). Try removing all those trailing commas.
Edit: To be clear, I'm talking about all of these commas:
Abbreviation TEXT NULL,
...
Deleted INTEGER NOT NULL,
...
BarCodeValue TEXT NOT NULL,
I had the same problem, but for a different reason (so I'm commenting because Google led me here). Turns out you can also encounter this error if your file has a weird encoding (like UCS-2 instead of UTF8).
Has anyone noticed this?
Whenever a model uses column_aggregation (inheritance), the schema.sql has 2 CREATE TABLE commands, one creates the basic table, and the other (apart from fields) adds an index on the inheritence column
CREATE TABLE Prop (id INT AUTO_INCREMENT, opt_property_type SMALLINT UNSIGNED DEFAULT 251 NOT NULL, property_nature VARCHAR(255), INDEX opt_property_type_idx (opt_property_type), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Prop (id INT AUTO_INCREMENT, opt_property_type SMALLINT UNSIGNED DEFAULT 251 NOT NULL, property_nature VARCHAR(255), INDEX Prop_property_nature_idx (property_nature), INDEX opt_property_type_idx (opt_property_type), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
Note the inclusion of INDEX Prop_property_nature_idx (property_nature) in the second statement
If anyone else is facing this, I will log a bug. Thanks
I just came across this myself. It seems like doctrine:build-sql is buggy.
One of the crazier things I discovered while investigating this is that doctrine:insert-sql doesn't even use schema.sql. It dynamically generates and runs the SQL based on the model definitions.
Looks like this is a known bug and won't be fixed in Doctrine 1:
http://www.doctrine-project.org/jira/browse/DC-123
http://www.doctrine-project.org/jira/browse/DC-536
I want to take the values from this site for the country table in my database.
The problem is that they don't provide the table structure, so I have to create one, but I cannot get it right - my phpMyAdmin keeps displaying an error when I want to inject the data into the table I created below:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMERIC, alpha3, name, officialName) VALUES ('004','AFG','Afghanistan','Afghan' at line 1
--
-- Table structure for table `countrytable`
--
CREATE TABLE IF NOT EXISTS `countrytable` (
`NUMERIC` int(11) NOT NULL,
`alpha3` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`officialName` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I think my table structure is incorrect. How can I fix it? Thanks!
Try all varchar fields to get the data in since all fields are in quotes in the string you have.
NUMERIC is reserved word in mysql
add in back-tick or quote it -> http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
`alpha3` should be a varchar(3) (or larger), not an int(11).
CREATE TABLE PERMISSIONS(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255) NOT NULL, UNIQUE(ID)
)
CREATE TABLE ROLES(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255)
)
I want to run this in MySql. When I try to execute separately each create-query everything works fine but they don't work together. I thought that separator was missed and tried to put semicolon after each query but MySql says that I have syntax mistake near ";" . Where is the mistake?
using the queries in the mysql console with a semi-colon after the each statement works. maybe you use an api (like php's mysql_query) which only supports one query at the time.
It's a semi-colon.
What is the equivalent of 'go' in MySQL?
I don't have a MySql instance running here and it's by no means my cup of tea but I believe you're supposed to separate your queries with ;.
CREATE TABLE PERMISSIONS(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255) NOT NULL, UNIQUE(ID)
) ;
CREATE TABLE ROLES(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255)
)