create table test(a Int, b text, c text);
show tables; -- 비 SQLite로 SQLiteStudio에서는 실행 불가
select * from sqlite_master wherer type = 'table'; -- SQLite에서 테이블 목록을 보고자 할떄 사용
a, b, c 속성을 가진 테이블 test를 생성해주었다. 그 후 현재 데이터베이스에 있는 모든 테이블의 목록을 출력.
insert into test values (1, 'This', 'Right here!');
insert into test(b, c) values ('That', 'Over there!');
행을 추가하는 방법. 두번쨰 줄은 원하는 속성의 값만을 지정하고자 할때 사용한다. 이 경우 a의 값은 null로 저장된다.
update test set c = 'Extra funny' where a=2;
a가 2인경우 c를 extra funny로 바꿔준다.
delete from test where a=2;
delete from test;
a가 2인 행을 지워라.
모든 행을 지워라.(테이블은 남아있게 된다)
drop table test;
데이터 베이스에서 test테이블을 삭제한다.
0과 ''(공백)과는 다른 것이다.
select * from test where a = null; -- 작동안함
select * from test where a is null;
위와 같이 a=null이라고 입력하게되면 작동하지 않는다. 이는 is null이라고 작성함으로써 해결할 수 있다.( 부정은 is not null)
id integer NOT NULL,
name varchar(255) UNIQUE NOT NULL,
address varchar(255),
city varchar(255),
state char(2),
zip char(10) );
char, varchar모두 text형태이지만, char는 길이가 고정적이나 varchar는 길이 변동 가능하다.(최대 길이를 지정)
NOT NULL 이라고 지정된 컬럼은 NULL이 불가능하다. (NULL이 되면 오류 발생) NOT NULL DEFAULT 0 는 NULL입력시 0으로 입력되면서 오류 발생을 막는다.
UNIQUE NOT NULL은 고유의 값을 갖도록 한다.(NULL이 아닌)
create table test (
id integer primary key autoincrement,
a varchar(255),
b varchar(255) );
insert into test(a, b) values ('one', 'two'); -- id 자동 삽입
primary는 고유한 값을 가진 index컬럼이라고 지정해주는 것이다. autoincreament는 지정이 없을시 1씩 증가하면서 값을 지정해준다.
create table test(
id integer primary key autoincrement,
a varchar(255),
b varchar(255) );
alter table test add column c varchar(100);
c라는 컬럼을 추가해주었다. 추가수정은 가능하나, 삭제 수정은 불가하다.
create tabel test2 as select id, a, b, from test;
drop table test;
alter table test2 rename to test;
테이블의 컬럼 전체를 삭제할 수는 없으므로, 임시의 테이블을 생성하여 모든 값들을 복사한후, 원래 테이블을 지운후, 임시 테이블의 이름을 원래의 테이블의 이름으로 바꿔준다. 테이블이 클 경우 이는 매우 많은 비용이 따르게 되므로 좋지 않은 방법이다.
(SQLite기준이다)
create table myTB(
aColumn boolean not null check(Acolumn in (0, 1)) );
insert into myTB values(0); -- OK
insert into myTB values(1); -- OK
insert into myTB values(2); -- NOT OK
불린을 표현하는 방법. 0과 1의 값만을 허가한다.