Oracle SQL DDL - accidentlywoo/sec GitHub Wiki

[DML] ๊ณ„์ • ๋งŒ๋“ค๊ธฐ

Data Dictionary -> user_tables

sqlplus

system ๊ณ„์ • ์ ‘์†

๊ณ„์ • ์ •๋ณดํ™•์ธ

show user

์„ฑ๊ณต ์‹คํ–‰๊ฒฐ๊ณผ

USER is "SYSTEM"

test ๊ณ„์ • ๋งŒ๋“ค๊ธฐ

CREATE USER test IDENTIFIED BY test;

์„ฑ๊ณต ์‹คํ–‰๊ฒฐ๊ณผ

User created.

์ƒ์„ฑ๋œ 'test'๊ณ„์ •์— ์ ‘์† ์‹œ๋„

conn test/test

์‹คํŒจ ๊ฒฐ๊ณผ

user TEST lacks CREATE SESSION privilege; logon denied

์‚ฌ์šฉ์ž๋“ค์˜ ์—ฐ๊ฒฐ์€ ์„ธ์…˜์ด ํ•„์š”ํ•˜๋‹ค -> Session

system ๊ณ„์ •์œผ๋กœ ๋‹ค์‹œ ์ ‘์†

test๊ณ„์ •์— ๊ถŒํ•œ ์ฃผ๊ธฐ

GRANT CREATE SESSION to test;

์„ฑ๊ณต ๊ฒฐ๊ณผ

Grant succeeded.

test ๊ณ„์ • ์ ‘์† ์‹œ๋„

conn test/test

์„ฑ๊ณต ๊ฒฐ๊ณผ

Connected.

์„ธ์…˜์ด ์ฃผ์–ด์ง€๊ณ , ์ ‘์†์ด ์„ฑ๊ณตํ•œ๋‹ค. ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด ๋ณด์ž.

CREATE TABLE test(a number);

์‹คํŒจ ๊ฒฐ๊ณผ

CREATE TABLE test(a number)
*
ERROR at line 1:
ORA-~~: insufficient privileges;

์ƒ์„ฑ๊ถŒํ•œ์„ ์ค˜์•ผ ํ•œ๋‹ค.

system ๊ณ„์ •์œผ๋กœ ๋‹ค์‹œ ์ ‘์†

GRANT CONNECT, RESOURCE to test;

GRANT๋Š” ROLE์„ ์ค„ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฐœ๋ณ„์ ์ธ ๊ถŒํ•œ์„ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

๋‹ค์‹œ test ๊ณ„์ •์œผ๋กœ ์ ‘์† ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด ๋ณด์ž.

CREATE TABLE test(a number);

์„ฑ๊ณต ๊ฒฐ๊ณผ

Table created.

ํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE ํ…Œ์ด๋ธ”๋ช…(
a CHAR,
a1 CHAR(3),
a2 VARCHAR2(3)
)

CHARํƒ€์ž…์€ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์ด ๊ฐ€๋Šฅ, (CHAR๋Š” MAXIMUM 1์ž๋ฆฌ๋กœ ์ƒ์„ฑ๋จ / NUMBER ๋ฌธ์ž๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ตœ๋Œ“๊ฐ’์œผ๋กœ ์ƒ์„ฑ)

But VARCHAR๋Š” ๋ฌธ์ž๊ธธ์ด๋ฅผ ๊ผญ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.(์ƒ์„ฑ ์—๋Ÿฌ)

  • CHAR์€ ๊ณ ์ • ๋ฌธ์ž๊ธธ์ด
  • VARCHAR (์˜ค๋ผํด์€ VARCHAR2์‚ฌ์šฉ) ๋ฌธ์ž ๊ธธ์ด๋งŒํผ ์ €์žฅ

์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ปฌ๋Ÿผ์˜ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

SELECT LENGTH(a), LENGTH(a1), LENGTH(a2) FROM ํ…Œ์ด๋ธ”๋ช…;
CREATE TABLE ํ…Œ์ด๋ธ”๋ช…(
dt DATEm
dt1 TIMESTAMP
);

-DATE : DATE์—๋„ ๋…„์›”์ผ ์‹œ๋ถ„์ดˆ ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ์ง€๋งŒ, ์ถœ๋ ฅํ• ๋•Œ๋Š” ๋…„์›”์ผ๋งŒ ๋ณด์—ฌ์ค€๋‹ค. -TIMESTAMP : ๋ฐ€๋ฆฌ ์„ธ์ปจ์ฆˆ ๋‹จ์œ„์˜ ์ž์„ธํ•œ ์‹œ๊ฐ„์ •๋ณด๋ฅผ ๋ณด๊ณ ์‹ถ์„ ๋•, TIMESTAMP๋ฅผ ์‚ฌ์šฉํ•˜์ž.

๋‚ ์งœํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋Š” ์ž๋ฆฟ์ˆ˜ ์ง€์ • ์ ˆ๋Œ€ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

DATEํƒ€์ž…์œผ๋กœํ• ๊นŒ? VARCHARํƒ€์ž…์œผ๋กœํ• ๊นŒ?

  • ์ž๋™์œผ๋กœ ๋‚ ์งœ๋ฐœ๊ธ‰ -> DATE / ์ž…๋ ฅ์„ ๋ฐ›์•„์•ผ ๋˜๋Š” ๊ฒฝ์šฐ -> ๋ฌธ์žํƒ€์ž…
INSERT INTO ํ…Œ์ด๋ธ”๋ช… VALUES(SYSDATE, SYSTIMESTAMP);

SELECT * FROM ํ…Œ์ด๋ธ”๋ช…;

์„ฑ๊ณต ๊ฒฐ๊ณผ

DT                DT1
------------------------------------------
20/06/08          20/06/08 10:45:52.109000
SELECT TO_CHAR(dt,'YY/MM/DD HH24:MI:SS'), dt1 FROM ํ…Œ์ด๋ธ”๋ช…;
DT                        DT1
------------------------------------------
20/06/08 10:45:52         20/06/08 10:45:52.109000

์•„์ฃผ ์ •ํ™•ํ•œ ํ‘œํ˜„์€ TIMESTAMP๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ + ์ œ์•ฝ์กฐ๊ฑด

Data Dictionary -> User Constraints

์ œ์•ฝ ์กฐ๊ฑด ์ •์˜

๊ตฌ๋ฌธ

CREATE TABLE [schema] table
    (column datatype [DEFAULT expr]
    [column_constraint].
    ...
    [table_constraint][,...]);

์—ด ๋ ˆ๋ฒจ ์ œ์•ฝ ์กฐ๊ฑด ๊ตฌ๋ฌธ

column [CONSTRAINT constraint_name] constraint_type,

ํ…Œ์ด๋ธ” ๋ ˆ๋ฒจ ์ œ์•ฝ ์กฐ๊ฑด ๊ตฌ๋ฌธ

column,...
   [CONSTRAINT constraint_name] constraint_type
   (column, ...),

-> ํ…Œ์ด๋ธ” ๋ ˆ๋ฒจ ์ œ์•ฝ์กฐ๊ฑด ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค.

์ œ์•ฝ ์กฐ๊ฑด ์œ ํ˜• 1.NOT NULL 2. UNIQUE 3. PRIMARY KEY 4. FORIEGN KEY 5. CHECK

์˜ˆ์‹œ๋ฅผ ๋ณด์ž

CREATE TABLE ์‚ฌ์›(
    ์‚ฌ๋ฒˆ VARCHAR2(10) CONSTRAINT ์‚ฌ์›_์‚ฌ๋ฒˆ_pk PRIMARY KEY,
    ์ด๋ฆ„ VARCHAR2(20) NOT NULL,
    ์ง๋ฌด ID VARCHAR2(6),
    ์ฃผ๋ฏผ๋ฒˆํ˜ธ CHAR(13),
    ๊ธ‰์—ฌ NUMBER(7),
    CONSTRAINT ์‚ฌ์›_์ง๋ฌดID_fk FOREIGN KEY (์ง๋ฌดID) REFERENCES ์ง๋ฌด(์ง๋ฌดID),
    CONSTRAINT ์‚ฌ์›_์ฃผ๋ฏผ๋ฒˆํ˜ธ_uq UNIQUE (์ฃผ๋ฏผ๋ฒˆํ˜ธ),
    CONSTRAINT ์‚ฌ์›_๊ธ‰์—ฌ_ck CHECK (๊ธ‰์—ฌ >=0)
);
INSERT INTO ์ง๋ฌด(์ง๋ฌดID, ์ง๋ฌด๋ช…) VALUES ('A', '์ธ์‚ฌ์—…๋ฌด');
INSERT INTO ์‚ฌ์›(์‚ฌ๋ฒˆ, ์ด๋ฆ„, ์ง๋ฌดID, ๊ธ‰์—ฌ) VALUES ('s1','n1','A', 1000);
DELETE FROM ์ง๋ฌด WHERE ์ง๋ฌดID = 'A'; -- ERROR

๋ถ€๋ชจ ํ…Œ์ด๋ธ”์—์„œ ์ฐธ์กฐ๋˜๊ณ ์žˆ๋Š” ์ž์‹ํ…Œ์ด๋ธ”์ด ์žˆ์„๋•Œ ์—๋Ÿฌ ๋ฐœ์ƒ

์‚ฌ์› ํ…Œ์ด๋ธ” ์ƒ์„ฑ์‹œ

~, CONSTRAINT ์‚ฌ์›_์ง๋ฌดID_fk FOREIGN KEY (์ง๋ฌดID) REFERENCES ์ง๋ฌด(์ง๋ฌดID) ON DELETE CASCADE

์ž์‹ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ๋•Œ, ์ฐธ์กฐํ•˜๋Š” ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๊ฐ€ ์‚ญ์ œ๋˜๋ฉด ๊ฐ™์ด ์‚ญ์ œ๋˜๊ฒ ๋‹ค๋Š” ์ œ์•ฝ์กฐ๊ฑด! ์—…๋ฌด์—์„  ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.