Home > Link, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, Vishal Pawar > BISQL – Laymen to SQL Developer # 29 – SQL #3 – The Create Table Statement, Specifying of Constraints & Default Values of SQL

BISQL – Laymen to SQL Developer # 29 – SQL #3 – The Create Table Statement, Specifying of Constraints & Default Values of SQL

Hi Folks,

This post is part of Series Database Management Systems

Currently running topic for this series is listed as below :

Series of Database Management Systems

>>Chapter 1 : DBMS [Database Management Systems]

>>Chapter 2 : Database Core Concepts and  Applications

>>Chapter 3 : Record Storage and Primary File Organization

>>Chapter 4 : Index Structures of Files

>>Chapter 5 : Entity-Relationship Model

>>Chapter 6 : Relational Algebra

>>Chapter 7 : SQL<You are Here>

Continuing from my previous post on this series.

We are going to Cover the Following Points in this article

  • The Create Table Statement
  • Specifying of Constraints & Default Values of SQL

The Create Table Statement

Now we shall discuss in detail ways to create and alter tables with constraints.

Syntax: CREATE TABLE tablename

(Column_1 datatype, column_2 datatype………….);

You specify the name of the table (should be unique) and one or more attributes and their data types.

EXAMPLE

CREATE TABLE Employee (

SSN number (4) not null NAME varchar (2) (20) not null BDATE data,

Mgrssn number, Primary key (SSN)

Foreign key (mgrssn) reference employee (SSN));

Alter Table Statement

After creating a table, one or more columns can be added to this table, similarly, columns can be dropped [oracle 9i only) and in either case the existing table columns will not be affected, for example, assume that we wish to add a column phone to employee table

Ø EXAMPLE

ALTER TABLE Employee

ADD phone number (7) not null;

Using the same alter command you can modify the data type of a column.

For example, the phone column can be modified from number to

varchar2.

ALTER TABLE Employee

MODIFY phone varchar2 (10);

Oracle 81 does not support dropping a column, but oracle 9i does it. ALTER TABLE employee

DROP COLUMN MODIFY phone; Dropping a table even when it has data is possible

SYNTAX: DROP [TABLE] table;

For Example, to drop the employee table, use the following statement. DROP employee

To rename the table use the RENAME statement as shown below

RENAME Employee to workers. VIEWS (Virtual Table)

View is a derived table, which doesn’t have storage of its own. Views are created by picking certain columns from the base table. The advantages of using views are:

Ø It restricts direct data access form tables I.e. it provides security

Ø Reduces joining of tables each and every time.

Syntax: Create view<view name> as select <column/s> from<table/s>

where<condition>

Example: Create view V1 as Select ssm.ma,e.sa;aru frp, e,[; Desc V1;

You can create views by referring to more than 1 table. NOTE:

1. If there are NOT NULL columns which are missing in view, you cannot insert the records.

2. If a view is created by referring to more than 1 table we cannot do DML

operation except select.

3. View updation (Ins.Del.Update) is possible only if it is created by a single table.

Insert into V1 values (11,’LSURESH’, 50000) Update v1 set ename=’Akash’ where empno=101;

Create view v2 as select empno, ename,dept.deptno.deptname from

emp.dept;

Indexing:

> Indexing provides a faster access (for to columns that are indexed)

> Indexes can also be used to ensure that no duplicate values are entered into a column.

Eg: Primary key of a table

SYNTAX:

CREATE INDEX index_name

ON table (column1, column2……);

Example:

Create index ind 1 on emp(empno);

1. Query 1

Retrieve the name and address of all employees who work for the research department.

1.

SELECT

FNAME, LNAME, ADDRESS

 

FROM

EMPLOYEE, DEPARTMENT

WHERE DNAME = ‘research’ AND D number=DNO

Query Q1 is similar to a SELECT – PROJECT – JOIN sequence of relational algebra operations.

Such queries are often called select-project-join queries. In the WHERE clause of Q1, the conditional DNAME = ‘Research’ is the selection condition and corresponds to a SELECT operation in Relational algebra.

Other Important Examples: Company database example

This example uses the following tables and underlines columns that are the

primary keys

Employee (

SSN char (9), Name varchar2 (10), Bdate Date, Address varchar 2 (30), Sex chart (1), Salary Number (10, 2) SuperSSN char (9), Dno Number (2)) Department (

Dnumber Number (2), Dname Varchar2 (10), MgrSSj char (9), Mgrstartdate

Date) Project (

Pnumber Number (2), Pname varchar2 (10). Plocation varchar2 (15). Dnum

Number (2)) Depedent(

ESSN CHAR (9), Dependent name Varchar2 (15), sex char, Bdate Date, Relationship varchar2 (10))

Dept_locations(

Dnumber Number (2), Dlocation varchar2 (15)) Works_on(

ESSN char (9), Pno Number (2), Hours Number (3, 1))

Query 1:

Retrieve the name and address of all employees who work for the research department.

Q 1:

SELECT

FNAME, LNAME, ADDRESS

 

FROM

EMPLOYEE, DEPARTMENT

WHERE DNAME = ‘research’ AND Dnumber=DNO

QUERY 2:

Retrieve the birth date and address of the employee whose name is ‘John

B. Smith’.

Q 2: SELECT BDATE, ADDRESS FROM EMPLOYEE

WHERE FNAME = ‘John ‘ AND D minit=’B’ and

LNAME=’Smith’

This query involves only the ‘EMPLOYEE’ relation listed in the FROM

clause.

QUERY 3:

For every project located in ‘stafford’, list the project number, the controlling department number, and the department manager’s last name, address and birth date.

Q 3:

SELECT

PNUMBER, DNUM, LNAME, ADDRESS, BDATE

 

FROM

PROJECT, DEPARTMENT, EMPLOYEE

 

WHERE

DNUM=DNUMBER AND MGRSSN=SSN AND

   

PLOCATION=’Stafford’

The join condition DNUM=DNUMBER relates a project to its controlling department, whereas, the join condition MGRSSN=SSN relates the controlling department to the Employee, who manages that department.

Specifying of Constraints & Default Values of SQL

There are many restrictions on the values in a database. The different constraints can be specified on relational database in the form of constraints.

The different constraints are given below:

· Inherent model based or implicit constraints: theses constraints give idea about the known requirement of the application or logical server.

· Not Null: this command makes the table not to take null values it is used in create table command.

For example:

Create table Employee

(

Ename character (50) NOT NULL,

…..

)

· Unique constraint: Candidate keys are specified by the unique constraint. The unique constraints can be specified alongside the column data type definition in create table command or it can be specified at the end of create command.

For example:

Create table Employee

(

Eno integer UNIQUE

…..

);

Create table Employee

(

Eno integer,

…..

UNIQUE Eno

);

· Check constraint: Recognizable SQL syntax is used to write check constraints. It consist of 2 components, a constraint name and check condition.

The constraint name is an identifier and is used to reference the constraint.

The check condition gives definition to the actual constraint logic. The basic predicates are used to define the check condition (>, <, =,

<>, <=, >=).

The general format is CHECK (Check condition) For example:

Create table Employee

(Empno integer

CONSTRAINT check_Empno

CHECK ( Empno BETWEEN 100 and 25000);

Emp_address varchar (70), Emp_type char (8)

CHECK (Emp_type in (‘TEMP’, ‘FULL TIME’, ‘CONTRACT’)),

Emp_dept char (3) NOT NULL with Default. Salary decimal (7, 2) NOT NULL CONSTRAINT check_salary

CHECK (salary < 500000.00), Commission decimal (7, 2), Bonus decimal (7, 2)

);

· The referential integrity constraint: This constraint is specified between two relations and is used to consistency among tuples in the two relations.

Default Values: The default value is included in any new tuple if an explicit value is not provided for that attribute.

The SQL Commands with Reference to Substring:

The feature that enables parts of strings to be accessed is the SQL

substring.

For example:

The function SUBSTRING (‘080-2666-7780’, 9, 4) Returns ‘7780’.

This command returns 4 characters starting in position 9. The syntax for SQL substring is

SELECT SUBSTRING (< Column_name>, position, length) FROM <table_name>

For example: Consider the following contents of the table. Table: REGION

region_nbr

region_name

100

East region

200

West region

300

North region

400

South region

SELECT region_name, SUBSTRING (region_name, 2, 3) as substring_name FROM dbo.region ORDER BY 1

Result of the above query is as shown below

region_name

substring_name

East region

est

West region

est

North region

ort

South region

out

The SQL Commands with Reference to Arithmetic:

The feature that allows arithmetic in queries, the basic arithmetic operators for addition (+), subtraction (-), multiplication (*), & division (/) can be applied to numeric values or attributes with numeric domains.

Hope you will like Series of Database Management Systems series !

If you have not yet subscribe this Blog , Please subscribe it from “follow me” tab !

So that you will be updated @ real time and all updated knowledge in your mail daily for free without any RSS subscription OR news reading !!

Happy Learning and Sharing !!

For More information related to BI World visit our all Mentalist networks Blog

SQL Server Mentalist … SQL Learning Blog

Business Intelligence Mentalist … Business Intelligence World

Microsoft Mentalist … MVC,ASP.NET, WCF & LinQ

MSBI Mentalist … MS BI and SQL Server

NMUG Bloggers …Navi Mumbai User Group Blog

Architectural Shack … Architectural implementation and design patterns

DBA Mentalist …Advance SQL Server Blog

MVC Mentalist … MVC Learning Blog

Link Mentalist … Daily Best link @ your email

Infographics Mentalist … Image worth explaining thousand Words

Hadoop Mentalist … Blog on Big Data

BI Tools Analysis … BI Tools

Connect With me on

| Facebook |Twitter | LinkedIn| Google+ | Word Press | RSS | About Me |

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: