ikura1's log

備忘録

DBお勉強

卒業研究ではPythonでの使用になります。
Pythonでの最低限の操作方法でもよかったのですが、良い機会なので勉強。
ドットインストールを書き出す。
確認にはなるはず。
あとはこれをPythonでするにはどうすればいいかか。


テーブル:データの区切り
フィールド/カラム:列
レコード:行

.で始まるコマンドは;はいらない

データベース参照
.datebase
テーブル作成
create table tablename(id,name);

テーブル参照
.table

.schema

テーブル削除
drop table tablename;

テーブル名変更
alter table tablename rename newname;

カラム追加
alter table tablename add column newcolumn

データ型の指定
create table tablename (name text,id integer)

NULL 空
INTEGER 整数値
REAL 小数値
TEXT 文字列
BLOB(Binarya Large OBject)
#あとで調べる

必須ではない
確約されない

便利機能

1,自動連番
(id integer primary key autoincrement)

2,nullの場合エラー
(name text not null)

3,重複の場合エラー
(name txt unique)

4,デフォルト値設定
(age integer default 30)

5,チェック機能
(count check(count>0))

インデックスを付ける
create index age on tablename
ユニークなものを付けたい場合
crete unique index


データ挿入
insert into tablename (nemae,email,age)values('test',hoge@gmail.com',20)
特殊文字 'を前に書く


データ抽出
1,全体
select * from tablename

2,特定の
select name from tablename

3,指定した物順番
serect * from tablename order by score

4,逆順
select * from tablename order by score desc

5,表示限度
select * from tablename order by score limit 3

6,しきい値
where score>=30

7,テキストも対応
where name='taguti'

8,含まれているなら、ワイルドカード
where name like 'tagu%'


埋め込み関数

1,カウント
select count(*) from tablename

2,最大値
select max(score) from tablename

3,ランダム
select random()

3-1,ランダム表示
select * from tablename order by random() limit 1

5,データの長さ表示
select name ,length(name) from tablename

6,データ型表示
select name, typeof(name) from tablename

詳しいこと知りたいなら
公式参照!

7,データの種類
select distinct team name from tablename

8,集計
select tablename,sum(score) from tablename group by team


時間の処理
1,現在時間
select current_time

2,現在年月日
select current_date

3,現在
select current_timestamp

select strftime('%Y'current_timestamp)


データ更新

updete tablename set name ='datinstall_taguti' where ='taguti'
updete tablename set name ='datinstall_taguti',score=500 where ='taguti'

データ削除
delete
delete from tablename where score <=100


IDのようなもの
select ROWID,* form tablename


複数のテーブル管理

select table.id,name,team,sum(score) from table,games where table.id=name.id group by table.id


外部からファイルから読み取る
1,区切り文字
デフォルトは|

1-1,変更
.separator ,

2,読み込み
.import hoge.txt tablename


バックアップ 
1,バックアップ
.dump teblename

2,出力変更
.output hoge.dump

3,読み込み
.read hoge.dump