MariaDB講習

種別 キー サンプル
ダウンロード download MariaDBダウンロードへ移動
コマンド mysql -u ユーザID -p
Enter password:パスワード
MariaDB [(none)]> show databases;
MariaDB [(none)]> use データベース名;
MariaDB [(none)]> show tables;
MariaDB [(none)]> source ファイル名.sql;
ユーザ作成 root権限が必要
mysql -u root -p
Enter password:パスワード
MariaDB [(none)]> grant all on データベース名.* to ユーザID@'localhost' identified by 'パスワード';
MariaDB [(none)]> grant all privileges on *.* to ユーザID@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
データベース作成 MariaDB [(none)]> create database if not exists データベース名 default character set utf8;
パスワード変更 MariaDB [(none)]> update user set password=PASSWORD("新パスワード") where User='ユーザID';
MySQLのデータ型 整数型(TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT)
浮動小数点数型(FLOAT, DOUBLE)
固定小数点数型(DECIMAL, NUMERIC)
BIT型
日付型と時刻型(DATE, TIME, DATETIME, TIMESTAMP, YEAR)
CHAR型とVARCHAR型
BINARY型とVARBINARY型
BLOB型とTEXT型
ENUM型
SET型
テーブル作成 CREATE TABLE IF NOT EXISTS PostalCodeSample
(
 prefCode      VARCHAR(2) NOT NULL COMMENT '都道府県コード 01-47'
,cityCode      VARCHAR(3) NOT NULL COMMENT '市区町村コード'
,postNumber    VARCHAR(7) NOT NULL COMMENT '郵便番号'
,prefName      VARCHAR(256)        COMMENT '都道府県名'
,cityName      VARCHAR(256)        COMMENT '市区町村名'
,townName      VARCHAR(256)        COMMENT '地域名'
,latitude      DOUBLE              COMMENT '緯度'
,creDate       DATE                COMMENT '登録日時'
,CONSTRAINT PRIMARY KEY(prefCode, cityCode, postNumber)
)
COMMENT='郵便番号テーブル'
ENGINE=InnoDB
AUTO_INCREMENT=6
DEFAULT CHARSET=utf8
/
インデクス:MAX 767バイト
テーブル削除 DROP TABLE PostalCodeSample;
データ登録 insert into PostalCodeSample (prefCode,cityCode,postNumber,prefName,cityName,townName)
 values ('13','101','1000001','東京都','千代田区','千代田',0.0,now());
insert into PostalCodeSample (prefCode,cityCode,postNumber,prefName,cityName,townName)
 values ('13','101','1000004','東京都','千代田区','大手町',0.0,now());
データ取得 select * from PostalCodeSample where prefCode = '13' and cityCode = '101' order by postNumber;
select * from PostalCodeSample where prefCode = '13' and cityCode = '101' and postNumber = '1000001' order by postNumber;
select * from PostalCodeSample where prefCode = '13' and cityCode = '101' and postNumber like '100%' order by postNumber;
データ更新 update PostalCodeSample set townName = '内幸町' where postNumber = '1000001';
データ削除 delete from PostalCodeSample where prefCode = '13' and cityCode = '101' and postNumber = '1000001';
データ取得(日時) select curdate();
select current_date();
select current_date;
select curtime();
select current_time();
select current_time;
select now();
select current_timestamp();
select current_timestamp;
月末日 select last_day(now());
翌月 select date_add(curdate(),INTERVAL 1 MONTH);
select date_add(now(),interval 1 month);
翌日 select date_add(curdate(),interval 1 day);
翌翌日 select date_add(curdate(),interval 2 day);
前月 select date_sub(now(),interval 1 month);
前日 select date_sub(now(),interval 1 day);
フォーマット select date_format(curdate(),'%Y/%m/%d');
select date_format(curdate(),'%Y年%m月%d日');
select date_format(now(),'%Y/%m/%d %H:%i:%S');
翌年 select now() + interval 1 year;
翌月 select now() + interval 1 month;
翌日 select now() + interval 1 day;
時間加算 select now() + interval 1 hour;
分加算 select now() + interval 1 minute;
秒加算 select now() + interval 1 second;