Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL-Data Definition 4/21/2019.

Similar presentations


Presentation on theme: "SQL-Data Definition 4/21/2019."— Presentation transcript:

1 SQL-Data Definition 4/21/2019

2 Objectives To understand the idea of  SQL schemas and  data independence. To develop a means of expressing schemas. 4/21/2019

3 Topics of Discussion SQL can be used to express database schemas in a standard machine readable format. It is primarily intended for relational database systems This session looks at how Logical schemas are constructed using SQL 4/21/2019

4 Creating SQL Schemas In a relational database entities are created as tables with specific properties (relations). Attributes are columns of the tables. Data definition in SQL is standardised. These standards are not implemented exactly in each database product. 4/21/2019

5 Scripts Typically these schemas are stored as scripts either in .SQL files (text) or in the DBMS environment (data dictionary). The script is executed and the persistent tables come into existence. 4/21/2019

6 Example SQL-92 Create table magazine (machine_id varchar(5), max_power float4 not null, controller varchar(10), Magazine_id varchar(5), constraint  magazinekey primary key(magazine_id)); create table mag_tool (magazine_no varchar(5), tool_id varchar(5) , constraint mag_toolkey primary key(magazine_no, tool_id), Constraint holds foreign key (magazine_no) references magazine(magazine_id));   There is no requirement that primary key-foreign key columns have the same name-just the same domain. Magazine Machine_id Max_power controller Magazine_id Magazine_no Tool_id Mag_tool 1..1 holds 0..* 4/21/2019

7 a primary key constraint
Constraints e.g. a primary key constraint create table employee (name char(20), department varchar(20), NI_number integer, constraint emp_key primary key (NI_number)); 4/21/2019

8 A Foreign Key Constraint
Create table pays (hours_worked integer, pay_rate integer, NI_no integer, Week_no integer, Constraint pays_key primary key (NI_no,week_no), constraint NI_number_pays foreign key (NI_no) references employee(NI_number)); There is no requirement that primary and foreign keys have the same name. Referential Integrity is automatically maintained. employee name department NI_number hours_worked pay_rate NI_no Week_no pays 1..1 NI_number_pays 0..* 4/21/2019

9 (prevents duplicate values in non primary key columns)
A Unique Constraint (prevents duplicate values in non primary key columns) Constraint manager_unique unique (Manager_number) Alternate keys would use UNIQUE clause. 4/21/2019

10 A Check Constraint Limits the set of valid values for a column. It is possible to have multiple check constraints for a column. create table employee (name char(20), department varchar(20), NI_number integer, constraint emp_key primary key (NI_number), constraint valid_dept check (department in ('Sales', 'Field Service', 'Logistics', 'MIS'))); N.B. It is not possible to use SQL sub-queries in Check constraints in SQL*Server. 4/21/2019

11 Mask constraint valid_product check (product_id like 'ab%');
where valid_product is a name for the constraint and product_id is a column name. % matches any character string so a valid string is 'ab' followed by any character. constraint valid_product check ((product_id like 'ab%' ) or (product_id like 'zz%')); % matches any character string so valid string is 'ab' followed by any character OR 'zz' followed by any valid string. _ matches any single character. 4/21/2019

12 Ranges constraint customer_numbers check (customer_number between ‘1111’ and ’9999’) 4/21/2019

13 Default Constraint Replaces a missing value on insert
create table employee (name char(20), department varchar(20), NI_number integer, constraint emp_key primary key (NI_number), default department_default 'Sales' for department); Column must not have not null constraint System functions can be used with default to provide system information e.g. current user, date, time etc.. See Transact*SQL Help 'niladic functions'. 4/21/2019

14 Alter Table Used to change pre-existing tables.e.g.
alter table movie_title add column total_sales_revenue decimal(9,2); alter table movie_title alter column total_sales_revenue decimal(10,2); alter table movie_title drop column total_sales_revenue; alter table movie_title add constraint pkey foreign key (lead_actor) references lead_actor_table; 4/21/2019

15 Outcome At the end of the process of creating an SQL schema a script should exist This will be a text file or a component in the data dictionary It is portable between database systems, so the database structure can be moved from system to system 4/21/2019

16 Summary The database structure is independent of database system, operating system and hardware It can be implemented on a client or a server machine The database structure can be changed easily 4/21/2019

17 Steps Take a complete data model
Create a text file containing all the SQL commands which form the schema. One table per entity Store the file (script) with subscript .SQL Load and execute the commands (script) using the DBMS SQL tool (QueryAnalyser or Access2000) 4/21/2019


Download ppt "SQL-Data Definition 4/21/2019."

Similar presentations


Ads by Google