0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL データベース、テーブル、カラムの追加から削除まで

Posted at

こちらの記事では、私が学んだSQLについて記載しております。
自身の忘れないためのメモとしております。
間違っている部分もあるかもしれませんので、ご了承ください。

データベースの追加/削除

・記述方法
creat database データベース名;

***命名ルール***
最初の名前は半角アルファベット!!!!

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
***データベースの削除***
・記述方法
drop database データベース名;

テーブルの追加/削除

・記述方法
create table 追加したいテーブル名(id int not null auto_increment primary key, tilte varchar(255) not null);

↑↑上記の文の意味↑↑
・列名 id
 -int・・・整数型
 -not null・・・nullを許可しない
 -auto_increment・・・idを自動的にふる
 -primary key・・・主キーの設定

・列名 title
 -varchar(255)・・・最大255文字の可変長文字列型
 -not null・・・nullを許可しない

-----------------------------------------------------------------------
***テーブルの削除***
・記述方法
drop table テーブル名;
テーブルを削除してしまうと簡単に復旧しないので注意!!!!!

テーブル構造の変更

***テーブルの列の追加***
・記述方法
alter table テーブル名 add 追加したい列名(カラム名) int after id;

例 テーブルusresに、ageのカラム名を追加したい
alter table users add age int after id;

-----------------------------------------------------------------------
***テーブルの列名を変更***
・記述方法
alter table テーブル名 change 変更前のカラム名 変更後のカラム名 int;

-----------------------------------------------------------------------
***テーブルの列名の削除***
・記述方法
alter table テーブル名 drop 列名;

補足

show databases;

登録しているデータベース全てを確認できる。

show tables;

データベース内のテーブルを確認できる。

show columns from テーブル名;

テーブルの構造を確認できる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?