![MAMPのDBをDBeaverを使って操作しよう!③テーブル作成・削除編](https://vynsen.net/wp-content/uploads/2022/04/d8dc4a429fc158b16289148e894a8ec2-7.png)
DBeaverを使ってMySQLのテーブルを作成・削除しよう
MAMPのDBをDBeaverを使って操作しよう!②データベース作成・削除編
前回までDBeaverを使用してDBを作成・削除する方法を見てきました。今回はテーブルの作成と削除をしていきます。
DBeaverを利用したテーブル作成
① Scriptのタブを開いて下記コードを入力
⇒実行箇所にカーソルを合わせて Ctrl+ Enter
![](https://vynsen.net/wp-content/uploads/2022/04/1-11-1024x576.png)
create table my_first_db.table(
t_key int primary key auto_increment,
t_id int unsigned default 0 comment 'ID',
t_value varchar(100) default null comment '値'
);
コードを意味を簡単に解説
![](https://vynsen.net/wp-content/uploads/2022/04/9-1-1024x576.png)
![](https://vynsen.net/wp-content/uploads/2022/04/10-1-1024x576.png)
![](https://vynsen.net/wp-content/uploads/2022/04/11-1024x576.png)
![](https://vynsen.net/wp-content/uploads/2022/04/12-1024x576.png)
② データベースを更新するとテーブルが出来る
![](https://vynsen.net/wp-content/uploads/2022/04/3-12-1024x576.png)
カラムを追加したいとき
![](https://vynsen.net/wp-content/uploads/2022/04/7e6af601a7c2e0752e589d8c03720ccf-1024x576.png)
alter table my_first_db.table
add column t_name varchar(20) not null;
alter table my_first_db.table
⇒alter table のあとにDB名とテーブル名を指定
add column t_name varchar(20) not null;
⇒add column のあとに カラム名、データ型、制約
![](https://vynsen.net/wp-content/uploads/2022/04/4bfda515c4ced617fc887fd9c95b33ec-1024x576.png)
作成時間のカラムの作成
alter table my_first_db.table
add column created_at timestamp not null default current_timestamp;
更新時間のカラムの作成
alter table my_first_db.table
add column updated_at timestamp not null default current_timestamp on update current_timestamp after created_at;
カラムを削除したいとき
alter table my_first_db.table
drop column t_id;
aalter table my_first_db.table
⇒追加と同様にDB名とテーブル名を指定
drop column t_id;
⇒drop column のあとにカラム名
DBeaverを利用したテーブル削除
① 下記コードを実行
drop table my_first_db.table;
⇒実行箇所にカーソルを合わせて
![](https://vynsen.net/wp-content/uploads/2022/04/4-8-1024x576.png)
② テーブルが削除されます
![](https://vynsen.net/wp-content/uploads/2022/04/5-8-1024x576.png)
MAMPのDBをDBeaverを使って操作しよう!④データレコードの追加・削除
ぜひ参考にしてください!また!