このPostgreSQLのテーブルは、MySQLのテーブルに変換することができますか?

StackOverflow https://stackoverflow.com/questions/2134621

質問

PostgreSQLのテーブルスキーマを考えます

create table thing (
    id serial primary key,
    key text,
    type int references thing,   
    latest_revision int default 1,
    created timestamp default(current_timestamp at time zone 'utc'),
    last_modified timestamp default(current_timestamp at time zone 'utc')
);
$for name in ['key', 'type', 'latest_revision', 'last_modified', 'created']:
    create index thing_${name}_idx ON thing($name);

があり、私は理解していない2行があり、MySQLのテーブルスキーマに変換することも可能であれば疑問に思って?それは自分自身を参照しているようだとして、次の行は、mysqlは理解するであろうものに変換することができます:

type int references thing,

プラス、最後の行のMySQL同等があります:

$for name in ['key', 'type', 'latest_revision', 'last_modified', 'created']:
    create index thing_${name}_idx ON thing($name);
役に立ちましたか?

解決

referencesラインが外部キーである、あなたはMySQLの中でこのようなものを使用することができます:

CREATE TABLE thing (
   ...
   type int,
   FOREIGN KEY (type) REFERENCES thing (id),
   ...
);

最後の2行は、SQLではありませんが、それはいくつかのスクリプト言語です。それは単に言及した列のインデックスを作成します:

CREATE INDEX thing_key_idx ON thing (key);
CREATE INDEX thing_type_idx ON thing (type);
...

他のヒント

これは pgloaderするから来たと考えているように私につながるPythonのような最後の行のルックス、その、一般的に使用されるPythonプログラム。またはAN-アドホックPythonプログラム。これは、PG、またはpsqlで有効な構文の私の知る限りではありません。

そしてreferences foo、ビットは、テーブル、fooの主キーに対する外部キーです。何の列が主キーにそれをデフォルトに指定されていない場合ます。

詳細は表を作成 href="http://www.postgresql.org/docs/current/static/sql-createtable.html" rel="nofollow noreferrer">上のドキュメントをチェックます。

ですから、あなたはすべて私に言っているものから、これはオリジナルのPostgreSQLのテーブルと同等のMySQLのテーブルスキーマになります

--
-- Table structure for table `thing`
--
CREATE TABLE IF NOT EXISTS `thing` (
  `id` int NOT NULL auto_increment,
  `key` text,
  `type` int,
  `latest_revision` tinyint NOT NULL default '1',
  `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
-- 
-- Constraints for table `thing`
-- 
ALTER TABLE `thing`
  ADD CONSTRAINT `thing_ibfk_1` FOREIGN KEY (`type`) REFERENCES `thing` (`id`);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top