0% found this document useful (0 votes)
35 views16 pages

SQL DDL: Limbajul de Definitie A Datelor - DDL

The document discusses SQL DDL (Data Definition Language) commands for creating, altering, and dropping databases, tables, and views. It provides examples of using CREATE statements to define databases and tables, ALTER statements to modify tables, DROP statements to delete tables, and CREATE statements to define views. It also covers data types, indexes, foreign keys, and storage engines that can be used in table definitions.

Uploaded by

mistermihai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views16 pages

SQL DDL: Limbajul de Definitie A Datelor - DDL

The document discusses SQL DDL (Data Definition Language) commands for creating, altering, and dropping databases, tables, and views. It provides examples of using CREATE statements to define databases and tables, ALTER statements to modify tables, DROP statements to delete tables, and CREATE statements to define views. It also covers data types, indexes, foreign keys, and storage engines that can be used in table definitions.

Uploaded by

mistermihai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SQL DDL

Limbajul de definitie a datelor DDL


Creare baza de date
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ...

create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name

Exemple:
CREATE DATABASE test CREATE DATABASE IF NOT EXISTS test CREATE DATABASE IF NOT EXISTS test CHARACTER SET = utf8; COLLATE = utf8_bin; CREATE DATABASE IF NOT EXISTS test CHARACTER SET = ascii; SHOW COLLATION; SHOW CHARACTER SET;

Modificare baza de date


ALTER {DATABASE | SCHEMA} [db_name] alter_specification ... ALTER {DATABASE | SCHEMA} db_name UPGRADE DATA DIRECTORY NAME

Stergere baza de date


DROP {DATABASE | SCHEMA} [IF EXISTS] db_name

Exemple:
DROP DATABASE test DROP DATABASE IF EXISTS test

alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name

Exemple:
ALTER DATABASE CHARACTER SET = ascii COLLATE = ascii_bin;

SQL DDL
Creare tabela
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_option] ... [partition_options] CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] [table_option] ... [partition_options]

select_statement
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name { LIKE old_tbl_name | (LIKE old_tbl_name) }

unde:
create_definition: col_name column_definition | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ... | {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ... | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option] ... | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ... | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition | CHECK (expr)

SQL DDL
Exemple:

INSERT INTO t1 SET c2 = 10, c3='2008-02-20', c4='12:30:20', c5=1.2, c6=0.02, c7='cuvant'; INSERT INTO t1 SET c3='2008/02/12', c4='8.66.20', c7='curs';

SQL DDL
si:
column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT 'string'] [reference_definition] [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}] [STORAGE {DISK|MEMORY|DEFAULT}]

si:

data_type:
BIT[(length)] | TINYINT[(length)] [UNSIGNED] [ZEROFILL] | SMALLINT[(length)] [UNSIGNED] [ZEROFILL] | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL] | INT[(length)] [UNSIGNED] [ZEROFILL] | INTEGER[(length)] [UNSIGNED] [ZEROFILL] | BIGINT[(length)] [UNSIGNED] [ZEROFILL] | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL] | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL] | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL] | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL] | DATE | TIME | TIMESTAMP | DATETIME | YEAR | CHAR[(length)] [CHARACTER SET charset_name] [COLLATE collation_name] | VARCHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name] | BINARY[(length)] | VARBINARY(length) | TINYBLOB | BLOB | MEDIUMBLOB | LONGBLOB | TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | spatial_type

SQL DDL
Observatii:
Folosind cuvantul cheie TEMPORARY, la crearea unei tabele, tabela va exista numai in timpul sesiunii/conexiunii curente la baza de date; Cuvantul cheie IF NOT EXISTS impiedica aparitia unei erori, in cazul in care tabela declarata exista deja; pe de alta parte, nu se verifica daca tabela existenta are aceeasi structura cu cea indicata de CREATE TABLE; Daca nu se specifica atributul NULL sau NOT NULL, coloana e tratata ca si cum s-ar fi specificat atributul NULL; Atributul AUTO_INCREMENT nu se poate atribui decat unei singure coloane intr-o tabela; acest atribut nu se aplica decat tipurilor intregi sau reale (float, double). Tipurile de tip caracter (char, varchar, text) pot avea atribuite CHARACTER SET setul de caractere atribuit acelei coloane; Clauza DEFAULT permite setarea unei valori default pentru o coloana; de exemplu, pentru un tip data, se poate folosi o functie de tip NOW() sau CURRENT_TIME; KEY e in mod normal un sinonim pentru INDEX; PRIMARY KEY poate fi simplu KEY atunci cand este folosit in definirea unei coloane; PRIMARY KEY este un index pentru care toate coloanele care intra in definirea lui trebuie sa fie NOT NULL: daca nu sunt astfel, sunt definite implicit (si tacit). Se poate creea o tabela din alta utilizand clauza SELECT la sfarsitul comenzii CREATE TABLE; Folosind clauza LIKE, se poate creea o tabela goala folosind structura tabelei originale invocate dupa clauza LIKE; 5

SQL DDL
Storage engines (motoare de stocare):

Exemplu: CREATE TABLE t (i INT) ENGINE = engine_name;


Motor MyISAM InnoDB MEMORY Limita stocare 256TB 64TB RAM Tranzactii NU DA NU B-tree index DA DA DA Hash-index NU DA DA Granularitate blocare Tabela Inregistrare Tabela

De ce sa utilizam totusi engine-uri netranzactionale ? -mult mai rapide; -mai putina memorie necesara (RAM si HD);

SQL DDL
Exemple:

INSERT INTO d2.persoana SET nume='Preda', prenume='Gabriel', adresa='Bucuresti S6'; INSERT INTO d2.persoana SET nume='Preda', prenume='Cristian', adresa='Bucuresti S4'; INSERT INTO d2.persoana SET nume='Preda', prenume='Caterina', adresa='Bucuresti S1'; INSERT INTO d2.persoana SET nume='Popescu', prenume='Gabriel', adresa='Bucuresti S4'; INSERT INTO d2.persoana SET nume='Cristian', prenume='Preda', adresa='Brasov';

SQL DDL
Exemple:

INSERT INTO traducere set token_nume='OPEN_FILE', engleza='Open', romana='Deschide', chineza=''; INSERT INTO traducere set token_nume='SAVE_FILE', engleza='Save', romana='Salveaz', chineza='';

Creare tabela folosind SELECT (se copiaza si datele):

SQL DDL
Creare tabela folosind SELECT (se copiaza si datele):

Creare tabela folosind LIKE (Se copiaza numai structura tabelei, nu se pastreaza datele):

SQL DDL
Modificare tabela
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ...

alter_specification: table_option ... | ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ] | ADD [COLUMN] (col_name column_definition,...) | ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ... | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ... | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option] ... | ADD FULLTEXT [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ... | ADD SPATIAL [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ... | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} | CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name] | MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name] | DROP [COLUMN] col_name | DROP PRIMARY KEY | DROP {INDEX|KEY} index_name | DROP FOREIGN KEY fk_symbol | DISABLE KEYS | ENABLE KEYS | RENAME [TO] new_tbl_name | ORDER BY col_name [, col_name] ... | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name] | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name] []

10

SQL DDL
Exemple: Stergerea unei coloane

Adaugarea unei coloane

11

SQL DDL
Exemple: Modificarea unei coloane

12

SQL DDL
Exemple: Modificarea unei coloane

Redenumirea tabelei

13

SQL DDL
Exemple: Adaugarea unei chei

Stergerea unei chei

14

SQL DDL
Stergerea unei tabele
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE]

Exemple:
DROP TABLE IF EXISTS traducere; DROP TABLE IF EXISTS translation;

15

SQL DDL
Crearea unui view
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = { user | CURRENT_USER }] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION]
Mai simplu: CREATE VIEW view_name AS select_statement Exemplu:
CREATE VIEW romana_engleza AS SELECT token_nume, engleza, romana from traducere;

Modificarea unui view


ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = { user | CURRENT_USER }] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION]

Stergerea unui view


DROP VIEW [IF EXISTS] view_name [, view_name] ... [RESTRICT | CASCADE]
16

You might also like