LoginSignup
0
0

More than 5 years have passed since last update.

SQLメモ CREATE ALTER

Last updated at Posted at 2018-10-26

CREATE文

//テーブル作成(mysql)
CREATE TABLE IF NOT EXISTS データベース名.テーブル名(
  cd integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
  name text,
  disp_flg integer NOT NULL DEFAULT 1,
  sort integer,
  new_record_time timestamp default current_timestamp(),
  update_record_time timestamp
);


//テーブル作成(POstgreSQL)
CREATE テーブル名
(
  cd serial NOT NULL,
  date date,
  name text,
  comment text,
  link text,
  target text,
  img1 text,
  img2 text,
  disp_flg integer NOT NULL DEFAULT 1,
  sort integer,
  new_record_time timestamp,
  update_record_time timestamp
  CONSTRAINT table_name_pkey PRIMARY KEY (cd)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE テーブル名
  OWNER TO postgres;
COMMENT ON TABLE テーブル名
  IS 'テーブル名';

//テーブルの複製
CREATE TABLE 新テーブル AS SELECT * FROM 既存テーブル;

alter文

//カラムの追加
alter table テーブル名 add column カラム名;

//特定のカラムの後に追加したい場合
alter table テーブル名 add column カラム名 after 前のカラム名;

//カラムの削除
alter table テーブル名 drop column カラム名;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0