MySQL to Postgres syntax - mysql

I am working on a maven project and for that I am using Postgres as database. Unfortunately the database I know more is MySQL. I have the basic details of SQL file which I want to include in the database. If possible, can someone with knowledge of postgres help me to convert the syntax. One more question, for maven project, do I need to include the .sql file within the project(if yes, where). Kindly let me know.
person.sql.
CREATE TABLE `Person` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
`country` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Thank you.

CREATE TABLE Person
(
id bigserial NOT NULL primary key,
name varchar(20) NOT NULL DEFAULT '',
country varchar(20) DEFAULT NULL
);
More details in the manual: http://www.postgresql.org/docs/current/static/sql-createtable.html

Related

can't add foreign key for table that has files

I'm building a project with angular and php, I added to my database table "file" that I can send files to him and retrieve all files information. now I'm trying to add a foreign key("Customer_id") from "Customers" table to connect the customer to specific file.
But when I try to add a relation it says:
error relational features are disabled
Can any one please help?can it be that the problem because the table has files?
This is my table :
CREATE TABLE `file` (
`id` Int Unsigned Not Null Auto_Increment,
`name` VarChar(255) Not Null Default 'Untitled.txt',
`mime` VarChar(50) Not Null Default 'text/plain',
`size` BigInt Unsigned Not Null Default 0,
`data` MediumBlob Not Null,
`created` DateTime Not Null,
PRIMARY KEY (`id`)
)
Verify that the engines used in both of the tables are innoDB.
As you mentioned in chat, your files table was MyISAM, hence the Foreign Constraints were disabled!!.

Error when importing schema.sql

I'm new to sql, currently I am using phpMyAdmin via XAMP, I think that uses mysql so correct me if I'm wrong by saying that. Anywhos, I'm trying to import a schema.sql data file into my database I created called "test" but I got an error upon importing it:
It says
Import has been successfully finished, 1 queries executed.
(schema.sql)
But then it also gives me an error message:
CREATE TABLE `population` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`population` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
MySQL said: Documentation
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
and :
Notice in .\import.php#704 Undefined variable: import_text Backtrace
I'm not sure what the issue is. The database I created is completely and has nothing in it.
Column id is the auto-column in question; auto-columns need to be defined as a key, for example as a unique key or a primary key. In your case, a primary key is a good idea because - well, it's your id-column.
CREATE TABLE `population` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`population` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Create MySQL Tables with Ansible

I'm using ansible to manage a small mail server using ubuntu. I wanted to use ansible to create a database which I can do and also create users for the database(s) which I can do also. But I'm not sure how to create tables using ansible. I'm trying to create the following three MySQL tables using ansible:
1)
CREATE TABLE `virtual_domains` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2)
CREATE TABLE `virtual_users` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`password` varchar(106) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3)
CREATE TABLE `virtual_aliases` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`source` varchar(100) NOT NULL,
`destination` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I have searched and searched and even ask in #ansible and have stated that I can use the mysql_db module to complete the above task but I can't find any examples that will give me some type of direction on how to achieve the above in ansible.
Any and all help would be GREATLY appreciated!
With the mysql_db module you can import a MySQL file. So you can simply copy all 3 create statements into one singly text file and import it like this:
- mysql_db: name=my_db state=import target=/tmp/dump.sql.bz2
That example is taken from the above linked docs page, there are more tasks including one which shows you how to copy the file to the host before.
hosts: mysql
tasks:
name: create a table in mysql_databases
command: mysql -u root -p[root1234] testdb --skip-column-names --execute "create table test_table (Name varchar(255), EmpID INT NOT NULL)"

How do i create in a table in phpmyadmin?

Whenever I go to sql in phpmyadmin to create a table, I put this code in:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date NOT NULL,
`activated` enum('0','1') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
it says:#1046 - No database selected
but when I put:
CREATE DATABASE data;
USE data;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date NOT NULL,
`activated` enum('0','1') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
it says that it could not create the database?
Step 1: Choose an existing Database or create a new Database.
Step 2: Choose the SQL tab and add your code to create a table.
One server can contain multiple databases. If you login into PHPMyAdmin, you choose a server to login to. After you've logged in, you have to select your database to be able to make changes to it.
So you don't need to create a database. You just need to select it. You can do that with the USE <databasename> statement, or though the PHPMyAdmin interface. In your second snippet, you already used have the USE statement. You just need to remove the CREATE DATABASE statement.
Same goes for creating the table, by the way. PHPMyAdmin provides an interface to add a table field by field without having to write the SQL for it. Although it can't hurt to know the syntax, of course.
Since you have got your sql statement, then just go to
phpMyAdmin > login (if there is need for it) > select database you want to use,
find it in Right Hand Side , example 'test'. tehn simply just find the
tab named sql and click it, after that just paste your statements

Querying two tables... in MySQL

CREATE TABLE IF NOT EXISTS `document`
(
`intId` int(11) NOT NULL auto_increment,
`chDocumentTitle` varchar(32) default NULL,
`dtLastUpdate` datetime default NULL,
`chUser` varchar(32) default NULL,
`chLink` varchar(256) default NULL,
`Keyword` varchar(256) default NULL,
`intParentid` int(11) NOT NULL,
PRIMARY KEY (`intId`),
KEY `dtLastUpdate` (`dtLastUpdate`,`chUser`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
CREATE TABLE IF NOT EXISTS `category`
(
`intId` int(11) NOT NULL auto_increment,
`chName` varchar(32) NOT NULL,
`Isactive` tinyint(1) NOT NULL default '0',
`chnestUnder` int(5) NOT NULL default '0',
PRIMARY KEY (`intId`),
KEY `chName` (`chName`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
Now I am looking for a query which will do the following...
Want to list out the documents of the categories... in hierarchical order.
Category One
Documents of Category One
Sub Category - [ If any ]
Documents of Sub Category
Based on this I need to generate XML.
This page has a very good explanation and plenty of helpful examples on how to work with hierarchical data in MySQL. In your situation it's definitely worth the read:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
...
Also make sure to follow the link to
There's also a reference to this page, with tips on how to work with hierarchical data in your database with a bit of help from PHP.