Linguagem SQL<br />Data DefinitionLanguage (DDL)<br />
Linguagem SQL<br />StructQueryLanguageLinguagem de consulta estruturada para base de dados relacionais<br />É baseada esse...
Linguagem SQL<br />Pode-se dividir a linguagem em três grupos distintos<br />DDL – Data DefinitionLanguage(CREATE, ALTER, ...
DDL – CREATE  DATABASE<br />Cria base de dados, Tabelas, Indices, Views.<br />CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] d...
DDL –CREATE  TABLE<br />CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] <br />ht...
DDL – CREATE  INDEX<br />CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type] ON tbl_name (index_col_name,...) [...
DDL – CREATE  VIEW<br />CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = { user | CURRENT_USER...
DDL – Comando DROP<br />DROP {DATABASE | SCHEMA} [IF EXISTS] db_name<br />DROP INDEX index_name ON tbl_name<br />DROP [TEM...
DDL – ALTER  DATABASE<br />ALTER {DATABASE | SCHEMA} [db_name] alter_specification ... ALTER {DATABASE | SCHEMA} db_name U...
DDL – ALTER TABLE<br />ALTER [IGNORE] TABLE nome_tblespecificação_alter [, especificação_alter ...] <br />Adicionando camp...
DDL – ALTER TABLE (Cont.)<br />Adicionando indexADD INDEX [nome_indice] (index_nome_col,...) <br />Modificando campo (Incl...
DDL – ALTER TABLE (Cont.)<br />Modificando atributos do campoMODIFY [COLUMN] definição_create [FIRST | AFTER nome_coluna] ...
DDL – ALTER TABLE (Cont.)<br />Renomeando tabelaRENAME [TO] nome_nova_tbl<br />Alterando  o conunto de caracteres CHARACTE...
DDL – ALTER VIEW<br />ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = { user | CURRENT_USER }] [SQL SECURIT...
DDL -RENAME<br />RENAME {DATABASE | SCHEMA} db_name TO new_db_name; <br />RENAME TABLE tbl_name TO new_tbl_name [, tbl_nam...
DDL – Atributos campos (create)<br />NOT NULL | NULL<br />AUTO_INCREMENT<br />DEFAULT valor<br />KEY<br />PRIMARY KEY<br /...
DDL – Integridade referencial<br />REFERENCES tbl_name (index_col_name,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE] [O...
DDL – Tipo de dados (+ usados)<br />INT[(length)] [UNSIGNED] [ZEROFILL] <br />INTEGER[(length)] [UNSIGNED] [ZEROFILL] |<br...
Upcoming SlideShare
Loading in …5
×

Linguagem sql

1,394 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,394
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Linguagem sql

  1. 1. Linguagem SQL<br />Data DefinitionLanguage (DDL)<br />
  2. 2. Linguagem SQL<br />StructQueryLanguageLinguagem de consulta estruturada para base de dados relacionais<br />É baseada essencialmente na álgebra relacional (Teoria dos conjuntos)<br />A linguagem SQL têm como entrada e saída tabelas<br />
  3. 3. Linguagem SQL<br />Pode-se dividir a linguagem em três grupos distintos<br />DDL – Data DefinitionLanguage(CREATE, ALTER, DROP )<br />DML – Data ManipulationLanguage(INSERT, UPDATE, DELETE, SELECT)<br />DCL – Data ControlLanguage(GRANT, REVOKE)<br />DTL – Data TransationLanguage(BEGIN WORK, COMMIT, ROLLBACK)<br />
  4. 4. DDL – CREATE DATABASE<br />Cria base de dados, Tabelas, Indices, Views.<br />CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name<br />http://dev.mysql.com/doc/refman/5.0/en/create-database.html<br />
  5. 5. DDL –CREATE TABLE<br />CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] <br />http://dev.mysql.com/doc/refman/5.0/en/create-table.htm<br />l<br />
  6. 6. DDL – CREATE INDEX<br />CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type] ON tbl_name (index_col_name,...) [index_type] index_col_name: col_name [(length)] [ASC | DESC] index_type: USING {BTREE | HASH} <br />http://dev.mysql.com/doc/refman/5.0/en/create-index.html<br />
  7. 7. DDL – CREATE VIEW<br />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] <br />http://dev.mysql.com/doc/refman/5.0/en/create-view.html<br />
  8. 8. DDL – Comando DROP<br />DROP {DATABASE | SCHEMA} [IF EXISTS] db_name<br />DROP INDEX index_name ON tbl_name<br />DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE] <br />DROP VIEW [IF EXISTS] view_name [, view_name] ... [RESTRICT | CASCADE] <br />http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-data-definition.html<br />
  9. 9. DDL – ALTER DATABASE<br />ALTER {DATABASE | SCHEMA} [db_name] alter_specification ... ALTER {DATABASE | SCHEMA} db_name UPGRADE DATA DIRECTORY NAME alter_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name<br />http://dev.mysql.com/doc/refman/5.1/en/alter-database.html<br />
  10. 10. DDL – ALTER TABLE<br />ALTER [IGNORE] TABLE nome_tblespecificação_alter [, especificação_alter ...] <br />Adicionando camposADD [COLUMN] definição_create [FIRST | AFTER nome_coluna ] <br />
  11. 11. DDL – ALTER TABLE (Cont.)<br />Adicionando indexADD INDEX [nome_indice] (index_nome_col,...) <br />Modificando campo (Inclui nome)CHANGE [COLUMN] nome_col_antigodefinição_create [FIRST | AFTER nome_coluna] <br />
  12. 12. DDL – ALTER TABLE (Cont.)<br />Modificando atributos do campoMODIFY [COLUMN] definição_create [FIRST | AFTER nome_coluna] <br />Apagando campoDROP [COLUMN] nome_col<br />Apagando chave primária ou indiceDROP PRIMARY KEY ouDROP INDEX nome_indice<br />
  13. 13. DDL – ALTER TABLE (Cont.)<br />Renomeando tabelaRENAME [TO] nome_nova_tbl<br />Alterando o conunto de caracteres CHARACTER SET character_set_name [COLLATE collation_name] | table_options<br />http://dev.mysql.com/doc/refman/4.1/pt/alter-table.html<br />
  14. 14. DDL – ALTER VIEW<br />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] <br />http://dev.mysql.com/doc/refman/5.1/en/alter-view.html<br />
  15. 15. DDL -RENAME<br />RENAME {DATABASE | SCHEMA} db_name TO new_db_name; <br />RENAME TABLE tbl_name TO new_tbl_name [, tbl_name2 TO new_tbl_name2] ... <br />
  16. 16. DDL – Atributos campos (create)<br />NOT NULL | NULL<br />AUTO_INCREMENT<br />DEFAULT valor<br />KEY<br />PRIMARY KEY<br />COMMENTS ‘comentário até 60 char’<br />CHECK (expr) sexo in (‘F’,’M’)numero >= 0<br />
  17. 17. DDL – Integridade referencial<br />REFERENCES tbl_name (index_col_name,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE] [ON DELETE reference_option] [ON UPDATE reference_option] <br />reference_option: RESTRICT | CASCADE | SET NULL | NO ACTION <br />
  18. 18. DDL – Tipo de dados (+ usados)<br />INT[(length)] [UNSIGNED] [ZEROFILL] <br />INTEGER[(length)] [UNSIGNED] [ZEROFILL] |<br /> REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] <br />DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL] | <br />DATE <br />TIME <br />DATETIME<br />CHAR[(length)] [<br />VARCHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name] <br />TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] <br />ENUM(value1,value2,value3,...)<br />

×