MySQLの練習
MySQLの練習
MySQLはhomebrewで入れてます。
Mac へ MySQL を Homebrew でインストールする手順 - Qiita
インストールしたら、とりあえず起動
mysql.server start
停止
mysql.server stop
練習なのでルート権限で入る
mysql -uroot
出る
exit
DBの作成 (goodsというDB)
create database if not exists goods;
goodsというDBを使う
use database goods;
goodsというDBにsales table作成
create table sales (name varchar(10), price int, count int);
goods DBの中にあるテーブルを見る
show tables from goods;
テーブル定義の確認
show full columns from sales;
describe sales;
show fields from sales;
テーブル定義の変更 sales tableのname の定義を変更
alter table sales modify name varchar(20);
カラム追加(最初の列に)
alter table sales add id int first;
カラム追加(nameの後)
alter table sales add country_code char(4) after name;
auto_incrementの設定
alter table sales modify id int auto_increment;
pkの設定
alter table sales add primary key (id);
fkの設定
alter table sales add foreign key(country_code) references countries(country_code);
not null制約の追加
alter table sales modify column name varchar(20) not null;
連続insert
insert into goods.sales (name,country_code, price, count) values ('Strowberry','A001', 280, 3),('Grape','E002',500,2),('Grapefruits','A003',200,4);