Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Creating a database


Published on

Published in: Education, Technology
  • Be the first to comment

Creating a database

  1. 1. Creating a Database Chapter 13
  2. 2. DDL - D ata D efinition L anguage <ul><li>Define or create a new table </li></ul><ul><li>Remove a table </li></ul><ul><li>Change definition/structure of an existing table </li></ul><ul><li>Define a virtual table(view) </li></ul><ul><li>Make index </li></ul><ul><li>Control physical storage of data </li></ul>
  3. 3. DDL consists of <ul><li>Create – Defining or creating database objects </li></ul><ul><li>Drop – Removing an existing database object </li></ul><ul><li>Alter – Changing definition of a database object </li></ul>
  4. 4. Create Table <ul><li>Define a new table </li></ul><ul><li>Prepare to accept data </li></ul><ul><li>Creator becomes owner </li></ul><ul><li>Table must have legal SQL name </li></ul><ul><li>Name not already existing </li></ul>
  5. 5. Create Table (contd.)-Column Definitions <ul><li>Column name(mandatory)- unique for the table but can be repeated in other tables </li></ul><ul><li>Data Type(mandatory)- also require size, if not given –default…(domain can be used) </li></ul><ul><li>Required data- NOT NULL if required </li></ul><ul><li>Default value- optional default value </li></ul><ul><li>Constraints:Primary key,Foreign key,Unique,Check </li></ul>
  6. 6. Create table example create table classmaster ( classid varchar2(4) primary key, classname varchar2(15) not null unique, intake number(3));
  7. 7. Create table example Create table classmaster2 ( classid varchar2(4) , classname varchar2(15) not null , intake number(3), doa date default '01-JUL-10', primary key(classid), unique(classname), check (intake between 0 and 100), Constraint fk_clm_clname foreign key(classname) references classmaster(classname) ) ;
  8. 8. Create table example Create table studentmaster ( regno varchar2(4) primary key, classid varchar2(14) constraint fk_sm_clsmstr references classmaster(classid) on delete set null, sfname varchar2(20) not null, slname varchar2(20) not null, DOB date, DOA date default ’01-JUL-10’, AdmStatus char(1) check (admstatus in ('A','C','P')), freeship char(1) check (freeship in ('Y','N')), check( doa>dob)); );
  9. 9. Creating table from another table <ul><li>Create table emp_copy as select * from emp; </li></ul><ul><li>This command will create a table named emp_copy with the same structure and all records of the table emp. </li></ul><ul><li>To create a table with the same structure without any records: </li></ul><ul><li>Create table emp_str_copy as select * from emp where 1=2; </li></ul><ul><li>To create table with selected columns and rows: </li></ul><ul><li>Create table emp_fewcopy as select empno,ename,sal from emp where sal>1000; </li></ul>
  10. 10. Constraints <ul><li>Details of constraints can be obtained from the table user_constraints </li></ul><ul><li>If constraint name is not declared dbms provides a name on its own </li></ul>
  11. 11. Removing a table <ul><li>DROP TABLE <table_name>; </li></ul>drop table classmaster2; Example:
  12. 12. Changing table definition <ul><li>Add a column definition to the table </li></ul><ul><li>Drop a column </li></ul><ul><li>Change the default value for a column </li></ul><ul><li>Add or drop a primary key for a table </li></ul><ul><li>Add or drop a foreign key for a table </li></ul><ul><li>Add or drop a unique constraint for a table </li></ul><ul><li>Add or drop a check constraint for a table </li></ul><ul><li>Add or drop a not null constraint for a table </li></ul>ALTER TABLE statement can:
  13. 13. Adding a column <ul><li>Alter table studentmaster add contact_no varchar2(20); </li></ul><ul><li>Usually not null not given or if given should be with a default value. </li></ul><ul><li>If data already present, adding a column with not null will violate not null constraint </li></ul>
  14. 14. Dropping a column <ul><li>Alter table classmaster drop intake; </li></ul>This statement drops the column named intake from the table classmaster.
  15. 15. Change the default value for a column <ul><li>alter table girls modify locality default 'BORIVALI'; </li></ul><ul><li>alter table classmaster modify classname default ‘TYBScIT’; </li></ul><ul><li>alter table classmaster modify classname default NULL; </li></ul>
  16. 16. Add or drop primary key for a column <ul><li>alter table girls add primary key (name); </li></ul><ul><li>alter table girls drop primary key ; </li></ul>(Column_name) (Table_name)
  17. 17. Add or drop foreign key for a column <ul><li>alter table emp drop constraint FK_DEPTNO ; </li></ul><ul><li>Table altered </li></ul><ul><li>alter table emp add constraint fk_deptno foreign key(deptno) references dept(deptno); </li></ul><ul><li>Table altered </li></ul>
  18. 18. How to know constraint name <ul><li>select constraint_name,constraint_type from user_constraints where table_name='STUDENTMASTER'; </li></ul><ul><li>CONSTRAINT_NAME C </li></ul><ul><li>------------------------------ - </li></ul><ul><li>SYS_C002715 C </li></ul><ul><li>SYS_C002716 C </li></ul><ul><li>SYS_C002717 C </li></ul><ul><li>SYS_C002718 C </li></ul><ul><li>SYS_C002719 P </li></ul><ul><li>fk_sm_clsmstr R </li></ul>
  19. 19. Add or drop UNIQUE constraint for a column <ul><li>alter table girls add constraint un_name unique(name); </li></ul><ul><li>Table altered </li></ul><ul><li>alter table girls drop constraint un_name ; </li></ul><ul><li>Table altered </li></ul>
  20. 20. Add or drop CHECK constraint for a column <ul><li>alter table girls add constraint chk_loc check(locality in ('BORIVALI',‘KANDIVALI'); </li></ul><ul><li>Table altered </li></ul><ul><li>alter table girls drop constraint chk_loc; </li></ul><ul><li>Table altered </li></ul>
  21. 21. Add or drop NOT NULL constraint for a column <ul><li>alter table girls modify locality not null; </li></ul><ul><li>Table altered </li></ul><ul><li>Get the constraint name from user_constraints </li></ul><ul><li>alter table girls drop constraint SYS_C002763 ; </li></ul><ul><li>Table altered </li></ul>
  22. 22. Creating assertions and domains <ul><li>Create assertion <assertion_name> </li></ul><ul><li>Check (check_condition) </li></ul><ul><li>Create domain <domain_name> </li></ul><ul><li><data type> check (check_condition) </li></ul><ul><li>Drop assertion <assertion_name> </li></ul><ul><li>Drop domain <domain_name> </li></ul><ul><li>To alter the definitions of assertions or domains they must be first dropped and then recreated. </li></ul>
  23. 23. Creating and Dropping Alias/Synonyms <ul><li>A synonym or alias is a name defined by the user that stands for the name of some other table. </li></ul><ul><li>Create synonym employee for emp; </li></ul><ul><li>Synonym created </li></ul><ul><li>Drop synonym employee; </li></ul><ul><li>Synonym dropped </li></ul>
  24. 24. Indexes <ul><li>Provides rapid access to rows of a table based on values of one or more columns </li></ul><ul><li>Stores data values and pointers to the rows where those values occur </li></ul><ul><li>Data values arranged in ascending or descending order </li></ul><ul><li>Presence of index transparent to user </li></ul>
  25. 25. Advantages- <ul><li>Speeds the execution of statements with search conditions referring to indexed columns </li></ul><ul><li>Good for tables with more queries and less frequent change </li></ul>
  26. 26. Disadvantages <ul><li>X Consumes additional disk space </li></ul><ul><li>X must be updated when a row is added or indexed column is updated-additional overhead </li></ul><ul><li>X Not worth when a column contains wide range of values or large number of null values </li></ul><ul><li>DBMS automatically creates index on columns with unique constraint </li></ul>
  27. 27. Example Indexes <ul><li>For creating indexes </li></ul><ul><li>create unique index ind_stm_sfname on studentmaster(sfname); </li></ul><ul><li>create unique index ind_stm_slname_desc on studentmaster(slname desc); </li></ul><ul><li>For removing indexes </li></ul><ul><li>Drop index ind_stm_slname_desc ; </li></ul><ul><li>To view details of indexes - user_indexes </li></ul>
  28. 28. Types of Indexes <ul><li>Normal </li></ul><ul><li>Function based </li></ul><ul><li>B-tree </li></ul><ul><li>Hash </li></ul><ul><li>Bit map </li></ul>
  29. 29. Database structure <ul><li>Single database architecture </li></ul><ul><li>Multi database architecture </li></ul><ul><li>Multi location architecture </li></ul><ul><li>Databases on multiple servers </li></ul>
  30. 30. Single database architecture <ul><li>DBMS supports one system wide database </li></ul><ul><li>Tables in various applications can reference one another </li></ul><ul><li>No choice is to be made as only one database-easy access </li></ul><ul><li>Database grows huge as more and more applications are added </li></ul><ul><li>Managing databases becomes difficult </li></ul><ul><li>Examples- Oracle,DB2 </li></ul>
  31. 31. Multi database architecture <ul><li>Each database assigned unique name </li></ul><ul><li>Each database dedicated to particular application </li></ul><ul><li>Divides data management tasks into smaller , manageable chunks </li></ul><ul><li>Requires no overall coordination </li></ul><ul><li>New application added as a new database,no need to disturb existing db </li></ul><ul><li>Users remember structure of their db easily </li></ul>
  32. 32. Multi database architecture(contd.) <ul><li>Individual db become unconnected </li></ul><ul><li>Table in one db cannot contain a foreign key reference to table in another db </li></ul><ul><li>Cross db queries usually not supported </li></ul><ul><li>Where ever allowed extended notation- db_name.owner.table_name </li></ul><ul><li>Commands used to connect to other db </li></ul><ul><li>Connect db_name </li></ul><ul><li>Use db_name </li></ul>
  33. 33. Multi Location Architecture <ul><li>Supports multiple dbs by using system’s directory structure to organize them </li></ul><ul><li>Each application has own database </li></ul><ul><li>Each db in same directory has unique name </li></ul><ul><li>Dbs may have same name in different directories </li></ul><ul><li>Flexible-suited for applications where user needs his/her own applications in separate dbs </li></ul>
  34. 34. Multi Location Architecture(contd.) <ul><li>Disadvantages same as that of multi db. </li></ul><ul><li>No master db to keep track of all dbs </li></ul><ul><li>Gaining access to db objects difficult as directory name is to be specified </li></ul>
  35. 35. Databases on multiple servers <ul><li>Databases on a network </li></ul><ul><li>Databases associated with named servers on a network </li></ul><ul><li>Within one server db’s with different names </li></ul><ul><li>Mapping of server names to physical server locations done by network software </li></ul><ul><li>Mapping of db names to physical files on a server done by dbms </li></ul>
  36. 36. THANKS