プログラミング学習帳

オンラインプログラミング学習サービス「ウェブカツ!!」で習ったことを整理してます。

SQL言語とは?

SQL言語とは、データベースを直接操作することができる言語
PHP言語と一緒に書くことができる

 

 

よく使う操作一覧

 

DBの作成
——————————————————————————————
create datebase データベース名;


create datebase sample2;
——————————————————————————————

 

 

テーブルの作成
——————————————————————————————
create table テーブル名(フィールド名 データ型,フィールド名 データ型);
フィールド名→ カラム名


create table test(
id int primary key auto_increment,
name text not null,
area varchar(50) not null
);

id → フィールド名
int → データ型「数値」
primary key →レコード(行)を判別するためのidを示す
auto_increment →12345と、自動的に番号が繰り上がる

null→ 値が何も入ってない
——————————————————————————————

 

 

テーブルを削除
——————————————————————————————
drop table テーブル名;


drop table test;
——————————————————————————————

 

 

レコード(行)を挿入
——————————————————————————————
insert テーブル名 set フィールド名=レコード,フィールド名=レコード;


insert test set name='ウェブカツ 太郎', area='東京都';
——————————————————————————————

 

 

レコードを更新(値を変える)
——————————————————————————————
update テーブル名 set 更新するレコードのフィールド=レコード値
where フィールド名 = フィールド値;


update test set name='ウェブカツ 花子';
where id=1;

idが「1」のものに対して、「ウェブカツ 花子」に変更
——————————————————————————————

 

 

レコードを検索(全てのカラム)
——————————————————————————————
select * from テーブル名 where 探すフィールド名=その値;


select * from test where name='ウェブカツ 花子';
——————————————————————————————

 

 

レコードを検索(特定のカラム)
——————————————————————————————
select 取得したいカラム from テーブル名 where 探すフィールド名=その値;


select id , name from test where name='ウェブカツ 花子';
——————————————————————————————

 

 

レコードを削除
——————————————————————————
delete from テーブル名 where レコードのフィールド=削除するレコード値;


delete from from test where name='ウェブカツ 花子';
—————————————————————————

 

 

データベースを削除
——————————————————
drop database データベース名;


drop database sample;
————————————————