Code SQL Flashcards

1
Q

Create tablespace

A

CREATE TABLESPACE TBS1
DATAFILE ‘C’
SIZE n m
AUTOEXTEND ON
MAXSIZE UNLIMITED;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Delete Tablespace

A

DROP TABLESPACE TBS1INCLUDING CONTENTS AND DATAFILES;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Change default tablespace

A

ALTER USER HR DEFAULT TABLESPACE TBS1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create table in tablespace

A

CREATE TABLE HR.TAB1(id NUMBER(30));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Insert multiple entries into Table with 1 Column

A

INSERT INTO HR.TAB1
SELECT level FROM dual
CONNECT BY level <= 100000;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create table with 2 columns

A

CREATE TABLE HR.TAB2(id NUMBER(30), name VARCHAR2(50));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Insert multiple entries into table with 2 columns

A

INSERT INTO HR.TAB2(id, name)
SELECT level, ‘Name’ ||level – || for string concat
FROM dual
CONNECT BZ level <= 100;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create table with constrains

A

CREATE TABLE HR.TAB1
(user_id NUMBER (30) CONSTRAINT PK_USER_ID PRIMARY KEY,
user_name VARCHAR(300 CHAR) CONSTRAINT NN_USER_NAME NOT NULL,
create_date DATE CONSTRAINT NN_CREATE_DATE NOT NULL,
CONSTRAINT fk_student_id FOREIGN KEY (sid) REFERENCES UNI.student (sid));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

User Anlegen

A

CREATE USER UNI IDENTIFIED BY uni;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

GRANTS

A

GRANT CREATE SEESION TO UNI;
GRANT CREATE TABLE TO UNI;
GRANT SELECT ON HR.HR_TAB4 TO UNI;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Insert Value into Table

A

INSERT INTO table(sid, name, create_date)
VALUES(1,’durra’,SYSTIMESTAMP,TO_DATE(‘2023-06-30));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Package Header

A

– Create a package header
CREATE OR REPLACE PACKAGE circle_area_pkg IS
– Package Specification
FUNCTION calculate_area(radius NUMBER) RETURN NUMBER;
END circle_area_pkg;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create Package Body

A

CREATE OR REPLACE PACKAGE BODY circle_area_pkg IS
– Package Body
FUNCTION calculate_area(radius NUMBER) RETURN NUMBER IS
pi CONSTANT NUMBER := 3.14159;
BEGIN
RETURN pi * radius * radius;
END calculate_area;
END circle_area_pkg;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Call Function in Package

A

– Call the calculate_area function from the package
DECLARE
radius NUMBER := 5;
area NUMBER;
BEGIN
area := circle_area_pkg.calculate_area(radius);
DBMS_OUTPUT.PUT_LINE(‘Area: ‘ || area);
END;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create and edit Sequence

A

– sequence stuff
CREATE SEQUENCE my_seq
START WITH 1
INCREMENT BY 1
NOMAXVALUE;

ALTER SEQUENCE my_seq
INCREMENT BY 2;

SELECT my_seq.NEXTVAL FROM DUAL;
– enable dbms output
SET SERVEROUTPUT ON;

– PL/SQL Block
DECLARE – Declare Variables, allocate memory for cursors and define data types
currval_seq NUMBER;
BEGIN – Execute SQL Code and/or Statements -> After Begin there can be implemented an exception block for exceptions
SELECT my_seq.CURRVAL – can just be accessed after calling NEXTVAL at least once!
INTO currval_seq
FROM DUAL;

DBMS_OUTPUT.PUT_LINE(currval_seq);
DBMS_OUTPUT.PUT_LINE(my_seq.NEXTVAL); END;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Insert Value into Table

A

INSERT INTO table(sid, name, create_date)
VALUES(1,’durra’,SYSTIMESTAMP,TO_DATE(‘2023-06-30));

17
Q

Insert Value into Table

A

INSERT INTO table(sid, name, create_date)
VALUES(1,’durra’,SYSTIMESTAMP,TO_DATE(‘2023-06-30));

18
Q

Merge Table

A

MERGE INTO member_staging x – target table
USING (SELECT member_id, first_name, last_name, rank FROM members) m – source tables/data
ON (x.member_id = m.member_id) – merge condition
WHEN MATCHED THEN – necessary to define
UPDATE SET x.first_name = m.first_name,
x.last_name = m.last_name,
x.rank = m.rank
WHERE x.first_name <> m.first_name OR – <> = !=
x.last_name <> m.last_name OR
x.rank <> m.rank
WHEN NOT MATCHED THEN – necessary to define
INSERT(x.member_id, x.first_name, x.last_name, x.rank)
VALUES(m.member_id, m.first_name, m.last_name, m.rank);