CREATING AND MODIFYING RELATIONS USING SQL


What is Relational Database

A database relation is a predefined row/column format for storing information in a relational database.

Relations are equivalent to tables.

 

Definition of DBMS terms

a.     SQL is structured Query Language

 

What is SQL?

SQL acronyms of structured Query Language, it’s a computer language use for storing, manipulating and retrieving data stored in relational database. SQL is a language of database, it includes database creation, deletion, fetching rows and modifying rows etc. 

SQL is an ANSI (American National Standards Institute standard but there are many different versions of the SQL language.

SQL is the standard language for Relation Database System. All relational database management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL Server uses SQL as standard database language.

SQL COMMANDS

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE, and DROP. These commands can be classified into groups based on their nature as: DML, DCL, and DQL 

b.    DML: Data Definition Language:

Data are manipulated by using: INSERT, UPDATE and DELETE command.

INSERT: This command is use to create record.

UPDATE: This Command is use in modifying records.

DELETE: Deletes records.

c.      DCL-Data Control Language:

GRANT: gives a privileges to user

REVOKE: Takes back privileges granted from user.

d.    DQL: Data Query Language

Uses SELECT commands: retrieves certain records from one or more tables.

e.      ALTER: Modifies an existing database object, such as a table.

f.      DROP: Deletes an entire table, a view of a table or other object in the database.

 

CREATING OF SQL DBASE

CREATE DATABASE using SQL

The SQL CREATE DATABASE statement is used to create new SQL database.


SYNTAX:

basic syntax of CREATE DATABASE statement is as follows:

CREATE DATABASE DatabaseName;

Always database name should be unique within the RDBMS.

 

Example:

If you want to create new database <School>, then CREATE DATABASE statement would be as follows:

SQL> CREATE DATABASE School;

 

Make sure you have administrative privilege before creating any database. Once a database is created, I you can check it in the list of databases as follows SQL> SHOW DATABASES 

DATA MANIPULATION LANGUAGE

 CREATE TABLE

Creating a basic table involves naming the table and defining its columns and each column's data type.

 

The SQL CREATE TABLE statement is used to create a new table.

Syntax:

Basic syntax of CREATE TABLE statement  is as follows:

CREATE TABLE table_name(

Column 1 datatype,

Column 2 datatype,

Column 3 datatype,

Column N datatype,

PRIMARY KEY (one or more columns));

 

Example: The following is an example which create a CCUSTOMERS table with ID as primary key and NOT NULL are the constrains showing that these fields cannot be NULL while creating records in this table:

SQL> CREATE TABLE CUSTOMER(

ID                         INT                      NOTNULL,

NAME                  VARCHAR(20)   NOTNNULL,

AGE                     INT                      NOTNULL,

ADDRESS           CHAR(25),

SALARY             DECIMAL(18,2),

PRIMARY KEY (ID));

         

DROP OR DELETE DATABASE

The SQL DROP DATABASE statement is used to drop any existing database in SQL schema.

 

SYNTAX

Basic syntax of Drop Database statement is a follows:

DROP DATABASE DatabaseName;

 

UDATE

The SQL Update query is used to modify the existing records in a table you use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.

 

SYNTAX

The basic syntax of UPDATE query with WHERE clause is as follows:

UPDATE table_name

SET column1=value1,clumn2-=value2….,, columnN=valueN

WHERE[condition];

 

Example: following is an example which would update ADDRESSfor a customer whose ID is 6:

SQL> UPDATE CUSTOMERS

SET ADDRESS = ‘Pune’

WHERE ID =6;



Post a Comment

Previous Post Next Post

Contact Form