Last active
May 27, 2020 06:47
-
-
Save shiro01/dca786cd57cab6ffe8da5a5534cb5f42 to your computer and use it in GitHub Desktop.
データベースコマンドメモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ユーザ情報取得 | |
> SELECT user, host, password FROM mysql.user; | |
ユーザの権限確認 | |
> SHOW GRANTS for 'root'@'%'; | |
ユーザ作成 hostは%(任意の文字列)で作成される。指定する場合は'ユーザ名'@'ホスト名'で指定する。 | |
> CREATE USER master IDENTIFIED BY 'master'; | |
> CREATE USER 'master'@'localhost' IDENTIFIED BY 'master'; | |
権限追加(localhostはサーバにログインしてmysqlログインするとき使う。<DB>.<table>) | |
> GRANT ALL PRIVILEGES ON *.* TO master@'%' IDENTIFIED BY 'master'; | |
> GRANT ALL PRIVILEGES ON *.* TO master@'localhost' IDENTIFIED BY 'master'; | |
権限削除 | |
REVOKE ALL ON <DB>.<table> FROM <ユーザ>; | |
権限確認 | |
SHOW GRANTS FOR <ユーザ>; | |
ユーザ削除 | |
> DROP USER master | |
読み込み権限ユーザ作成 | |
create user <ユーザ> identified by '<パス>'; | |
grant select on <DB>.<table> TO <ユーザ>; | |
show grants for <ユーザ>; | |
データベース作成 | |
> CREATE DATABASE testdb; | |
データベース情報確認 | |
> SELECT * from INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='testdb'; | |
データベース一覧取得 | |
> SHOW DATABASES; | |
使用するデータベースを指定 | |
> USE testdb; | |
データベースに存在するテーブル一覧取得 | |
> SHOW TABLES; | |
-- DATEについてお試し実行 | |
SELECT | |
CURRENT_DATE() | |
, DATE('20190325') - INTERVAL 9 HOUR | |
, ADDDATE(CURRENT_DATE(), -1) = "2019/06/06" as d1 | |
, ADDDATE(CURRENT_DATE(), -1) < "2019/06/06 00:00:00" as d2 | |
, ADDDATE(CURRENT_DATE(), -1) < "2019/06/06 00:00:01" as d3 | |
, CURRENT_DATE() - INTERVAL 0.001 SECOND_MICROSECOND as d4 | |
, ADDDATE("2019/06/07", -1) as d5 | |
, ADDDATE("2019/06/07", -1) < CURRENT_DATE() as d6 | |
, '2019/06/07' - INTERVAL 0.001 SECOND_MICROSECOND as d6 | |
, NOW() + INTERVAL 9 HOUR as d8_jst | |
, ADDDATE('2019/06/07', -1) as d9 | |
, '2019/06/07' - INTERVAL 0.0000001 SECOND_MICROSECOND as d10 | |
, DATE(ADDDATE('2019/06/06 15:01:00' + INTERVAL 9 HOUR, -1)) - INTERVAL 9 HOUR as d11 | |
, DATE('2019/06/06 15:01:00' + INTERVAL 9 HOUR) - INTERVAL 9 HOUR - INTERVAL 0.001 SECOND_MICROSECOND as d12 | |
, DATE('2019/06/06 00:01:00') - INTERVAL 9 HOUR as d13 | |
, DATE(ADDDATE('2019/06/06 00:01:00', +1)) - INTERVAL 9 HOUR - INTERVAL 0.001 SECOND_MICROSECOND as d14 | |
; | |
ALTER TABLE goods ADD COLUMN `test01` double NULL COMMENT 'テスト01' after `name`; | |
ALTER TABLE goods ADD COLUMN `test02` double COMMENT 'テスト02' after `name`; | |
ALTER TABLE goods ADD COLUMN `test03` DOUBLE NOT NULL DEFAULT 0 COMMENT 'テスト03' after `name`; | |
ALTER TABLE goods MODIFY COLUMN `test03` BIGINT COMMENT 'テスト03'; | |
テーブルのリネーム | |
alter table goods rename to goodstb; | |
alter table goodstb rename to goods; | |
※ インストール直後のpostgresにログインできない場合 | |
下記コマンドを実行し、postgresユーザーのパスワードを初期化した後、再度ログインする。 | |
sudo -u postgres psql -c "alter user postgres password 'postgres';" | |
psql -U postgres -h localhost | |
パスワードを変更 | |
alter user postgres password 'postgres_pass'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment