SlideShare a Scribd company logo
CREATING DATABASE USING SQL COMMANDS
SHOW DATABASEmysql> SHOW DATABASES;Use the SHOW statement to find out what databases currently exist on the serverSQL command
CREATE DATABASEmysql> CREATE DATABASE newdb;Database nameCreate statementThe new database will appear after successfully create the database
USE DATABASEmysql> USE newdbCreating a database does not select it for use; you must do that explicitly.Note that USE, like QUIT, does not require a semicolon. The USE statement is special in another way, too: it must be given on a single line. SQL commandDatabase name
SHOW TABLESmysql> SHOW TABLES;To create one or more tables in the current database, you can use CREATE TABLE statement.SQL commandIt indicates that there is no table in the database.
CREATE TABLEmysql> CREATE TABLE Use a CREATE TABLE statement to specify the layout of your table.In this case, table pelajarwill have 4 attributes.
If you want to find out about the structure of a table, the DESCRIBE command is useful; it displays information about each of a table's columnsDESCRIBESQL commandDatabase name
ALTER TABLE statementALTER TABLE is use to modify an existing columnIt is consists of ADD, MODIFY and DROP columnALTER TABLE statement:ADD column	MODIFY column	DROP columnALTER TABLE tableADD (column datatype [DEFAULT expr][column datatype]….);ALTER TABLE tableMODIFY (column datatype [DEFAULT expr][column datatype]….);ALTER TABLE tableDROP (column datatype [DEFAULT expr][column datatype]….);
ALTER TABLE statementExample
Table before add new column
Execute ALTER TABLE statement to ADD:
Table after execute
the ALTER TABLE
statement to ADDALTER TABLE emp ADD (job VARCHAR(9));
ALTER TABLE statementExample
Table before MODIFY a column
Execute ALTER TABLE statement to MODIFY:
Table after execute
the ALTER TABLE
statement to
MODIFYALTER TABLE emp MODIFY (ename VARCHAR(3));
ALTER TABLE statementExample
Table before DROP column
Execute ALTER TABLE statement to DROP:
Table after execute
the ALTER TABLE
statement to DROP:ALTER TABLE emp DROP COLUMN job;
Basic Structures in Query DesignQUERY WITH SQL
DEFINE THE INSTRUCTIONSELECT   to query data in the databaseINSERT  to insert data into a tableUPDATE  to update data in a tableDELETE  to delete data from a table
SELECT STATEMENTPurpose  to retrieve and display data from one or more database tables.
It is an extremely powerful command capable of performing the equivalentof the relational algebra’s Selection, Projection and Join operations in a single statement.
It is most frequently used SQL command.
Basic select statement:
SELECT identifies what columns
FROM identifies which tableSELECT [DISTINCT] {*, column,…}FROM table;
SELECT STATEMENTSelect all columnsSelect specific columnsOutputSELECT * FROM dept;OutputSELECT Deptno, loc FROM dept;
SELECT STATEMENT   Using WHERE clause.
   It is used to restrict or limiting the rows selected
   The WHERE clause follows the FROM clauseSELECT [DISTINCT] {*, column,…}FROM tableWHERE condition(s);
SELECT STATEMENTUsing where clauseSelect specific columnsOutputSELECT ename, job, deptno FROM deptWHERE job = ‘CLERK’OutputSELECT Deptno, loc FROM deptWHERE loc = ‘New York’;
SELECT STATEMENTUsing BETWEEN operator to display rows based on a range of values OutputSELECT ename, sal FROM empWHERE sal BETWEEN 10000 AND 15000;
UPDATEUPDATE itemSET Quantity =10,UnitPrice =1800.00WHERE ItemNo = ‘123’;In this statement , only one row will be updated since the condition is specified in the WHERE clause, if the WHERE clause is omitted, all rows in the item table will be update.
DELETETo remove one or more rows from a table. For example , to delete the ItemNo = ‘123’.DELETEFROM itemWHERE ItemNo = ‘123’;
The syntax for inserting data into a table one row at a time is as follows:	INSERT INTO "table_name" ("column1", "column2", ...)VALUES ("value1", "value2", ...)INSERT INTO

More Related Content

What's hot (19)

PPTX
Sql basics
Genesis Omo
 
PPTX
Database Systems - SQL - DCL Statements (Chapter 3/4)
Vidyasagar Mundroy
 
PDF
Database Systems - SQL - DDL Statements (Chapter 3/2)
Vidyasagar Mundroy
 
PPTX
SQL - DML and DDL Commands
Shrija Madhu
 
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
PPT
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
PPTX
SQL - Structured query language introduction
Smriti Jain
 
PPTX
Oracle: Basic SQL
DataminingTools Inc
 
PPT
SQL DDL
Vikas Gupta
 
PPTX
Oracle SQL DML Statements
A Data Guru
 
PPTX
Advanced SQL Webinar
Ram Kedem
 
PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
PPTX
Oracle: DDL
DataminingTools Inc
 
PPTX
SQL Commands
Sachidananda M H
 
PPT
Sql DML
Vikas Gupta
 
PPTX
8. sql
khoahuy82
 
PPTX
SQL Basics
Hammad Rasheed
 
Sql basics
Genesis Omo
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Vidyasagar Mundroy
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Vidyasagar Mundroy
 
SQL - DML and DDL Commands
Shrija Madhu
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
SQL - Structured query language introduction
Smriti Jain
 
Oracle: Basic SQL
DataminingTools Inc
 
SQL DDL
Vikas Gupta
 
Oracle SQL DML Statements
A Data Guru
 
Advanced SQL Webinar
Ram Kedem
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
Oracle: DDL
DataminingTools Inc
 
SQL Commands
Sachidananda M H
 
Sql DML
Vikas Gupta
 
8. sql
khoahuy82
 
SQL Basics
Hammad Rasheed
 

Viewers also liked (14)

PPT
Executing Sql Commands
phanleson
 
PPTX
Top 10 insurance administrator interview questions and answers
paul21231z
 
PPS
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
PPSX
Execute sql query or sql command sql server using command prompt
Ikhwan Krisnadi
 
PDF
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Denodo
 
DOC
Sql queries
narendrababuc
 
DOCX
DBMS lab manual
maha tce
 
DOC
Sql queires
MohitKumar1985
 
DOC
Dbms lab questions
Parthipan Parthi
 
DOC
Best sql plsql material
pitchaiah yechuri
 
PPT
Sql ppt
Anuja Lad
 
DOC
A must Sql notes for beginners
Ram Sagar Mourya
 
PPT
ASP.NET Tutorial - Presentation 1
Kumar S
 
DOC
Sql queries with answers
vijaybusu
 
Executing Sql Commands
phanleson
 
Top 10 insurance administrator interview questions and answers
paul21231z
 
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
Execute sql query or sql command sql server using command prompt
Ikhwan Krisnadi
 
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Denodo
 
Sql queries
narendrababuc
 
DBMS lab manual
maha tce
 
Sql queires
MohitKumar1985
 
Dbms lab questions
Parthipan Parthi
 
Best sql plsql material
pitchaiah yechuri
 
Sql ppt
Anuja Lad
 
A must Sql notes for beginners
Ram Sagar Mourya
 
ASP.NET Tutorial - Presentation 1
Kumar S
 
Sql queries with answers
vijaybusu
 
Ad

Similar to Creating database using sql commands (20)

PPTX
Sql practise for beginners
ISsoft
 
PPTX
Oraclesql
Priya Goyal
 
PPT
MY SQL
sundar
 
PPT
Mysql 120831075600-phpapp01
sagaroceanic11
 
PPTX
Sql basics
Aman Lalpuria
 
PPTX
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
PDF
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
PPTX
Database Overview
Livares Technologies Pvt Ltd
 
PPTX
SQL Assessment Command Statements
Shaun Wilson
 
PPTX
Avinash database
avibmas
 
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
PPTX
SQL.pptx for the begineers and good know
PavithSingh
 
PPT
DBMS-SQL-Commands-BBA-4-Sem-1-PanjabUniversity.ppt
NishaGupta92113
 
PPT
SQL Presentation-1 (structured query language)
renodet116
 
PPT
SQL Presentation-1 yehjebjj yeuu helo the worls.ppt
nanisaketh
 
PPT
Db1 lecture4
Sherif Gad
 
PPTX
Its about a sql topic for basic structured query language
IMsKanchanaI
 
PPTX
SQL PPT.pptx
PriyaPandey767008
 
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
Sql practise for beginners
ISsoft
 
Oraclesql
Priya Goyal
 
MY SQL
sundar
 
Mysql 120831075600-phpapp01
sagaroceanic11
 
Sql basics
Aman Lalpuria
 
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
Database Overview
Livares Technologies Pvt Ltd
 
SQL Assessment Command Statements
Shaun Wilson
 
Avinash database
avibmas
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
SQL.pptx for the begineers and good know
PavithSingh
 
DBMS-SQL-Commands-BBA-4-Sem-1-PanjabUniversity.ppt
NishaGupta92113
 
SQL Presentation-1 (structured query language)
renodet116
 
SQL Presentation-1 yehjebjj yeuu helo the worls.ppt
nanisaketh
 
Db1 lecture4
Sherif Gad
 
Its about a sql topic for basic structured query language
IMsKanchanaI
 
SQL PPT.pptx
PriyaPandey767008
 
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
Ad

Creating database using sql commands

  • 2. SHOW DATABASEmysql> SHOW DATABASES;Use the SHOW statement to find out what databases currently exist on the serverSQL command
  • 3. CREATE DATABASEmysql> CREATE DATABASE newdb;Database nameCreate statementThe new database will appear after successfully create the database
  • 4. USE DATABASEmysql> USE newdbCreating a database does not select it for use; you must do that explicitly.Note that USE, like QUIT, does not require a semicolon. The USE statement is special in another way, too: it must be given on a single line. SQL commandDatabase name
  • 5. SHOW TABLESmysql> SHOW TABLES;To create one or more tables in the current database, you can use CREATE TABLE statement.SQL commandIt indicates that there is no table in the database.
  • 6. CREATE TABLEmysql> CREATE TABLE Use a CREATE TABLE statement to specify the layout of your table.In this case, table pelajarwill have 4 attributes.
  • 7. If you want to find out about the structure of a table, the DESCRIBE command is useful; it displays information about each of a table's columnsDESCRIBESQL commandDatabase name
  • 8. ALTER TABLE statementALTER TABLE is use to modify an existing columnIt is consists of ADD, MODIFY and DROP columnALTER TABLE statement:ADD column MODIFY column DROP columnALTER TABLE tableADD (column datatype [DEFAULT expr][column datatype]….);ALTER TABLE tableMODIFY (column datatype [DEFAULT expr][column datatype]….);ALTER TABLE tableDROP (column datatype [DEFAULT expr][column datatype]….);
  • 10. Table before add new column
  • 11. Execute ALTER TABLE statement to ADD:
  • 14. statement to ADDALTER TABLE emp ADD (job VARCHAR(9));
  • 17. Execute ALTER TABLE statement to MODIFY:
  • 21. MODIFYALTER TABLE emp MODIFY (ename VARCHAR(3));
  • 24. Execute ALTER TABLE statement to DROP:
  • 27. statement to DROP:ALTER TABLE emp DROP COLUMN job;
  • 28. Basic Structures in Query DesignQUERY WITH SQL
  • 29. DEFINE THE INSTRUCTIONSELECT  to query data in the databaseINSERT  to insert data into a tableUPDATE  to update data in a tableDELETE  to delete data from a table
  • 30. SELECT STATEMENTPurpose  to retrieve and display data from one or more database tables.
  • 31. It is an extremely powerful command capable of performing the equivalentof the relational algebra’s Selection, Projection and Join operations in a single statement.
  • 32. It is most frequently used SQL command.
  • 35. FROM identifies which tableSELECT [DISTINCT] {*, column,…}FROM table;
  • 36. SELECT STATEMENTSelect all columnsSelect specific columnsOutputSELECT * FROM dept;OutputSELECT Deptno, loc FROM dept;
  • 37. SELECT STATEMENT Using WHERE clause.
  • 38. It is used to restrict or limiting the rows selected
  • 39. The WHERE clause follows the FROM clauseSELECT [DISTINCT] {*, column,…}FROM tableWHERE condition(s);
  • 40. SELECT STATEMENTUsing where clauseSelect specific columnsOutputSELECT ename, job, deptno FROM deptWHERE job = ‘CLERK’OutputSELECT Deptno, loc FROM deptWHERE loc = ‘New York’;
  • 41. SELECT STATEMENTUsing BETWEEN operator to display rows based on a range of values OutputSELECT ename, sal FROM empWHERE sal BETWEEN 10000 AND 15000;
  • 42. UPDATEUPDATE itemSET Quantity =10,UnitPrice =1800.00WHERE ItemNo = ‘123’;In this statement , only one row will be updated since the condition is specified in the WHERE clause, if the WHERE clause is omitted, all rows in the item table will be update.
  • 43. DELETETo remove one or more rows from a table. For example , to delete the ItemNo = ‘123’.DELETEFROM itemWHERE ItemNo = ‘123’;
  • 44. The syntax for inserting data into a table one row at a time is as follows: INSERT INTO "table_name" ("column1", "column2", ...)VALUES ("value1", "value2", ...)INSERT INTO
  • 45. Assuming that we have a table that has the following structure :Table Store_InformationResult :INSERT INTO Store_Information (store_name, Sales, Date)VALUES (‘Ipoh', 900, 'Jan-10-1999')
  • 46. The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, we now use a SELECT statement to specify the data that we want to insert into the table. The syntax is as follows: INSERT INTO "table1" ("column1", "column2", ...)SELECT "column3", "column4", ...FROM "table2"
  • 47. Before using INSERT INTO commandTable Sales_InformationTable Store_InformationTable Sales_InformationTable Store_Information
  • 48. After using INSERT INTO commandINSERT INTO Store_Information (store_name, Sales, Date)SELECT store_name, Sales, DateFROM Sales_InformationWHERE Year(Date) = 1998New information that been added to the tableTable Sales_InformationTable Store_Information
  • 49. CREATE VIEWViews can be considered as virtual tables. Generally speaking, a table has a set of definition, and it physically stores the data. A view also has a set of definitions, which is build on top of table(s) or other view(s), and it does not physically store the data. The syntax for creating a view is as follows: CREATE VIEW "VIEW_NAME" AS "SQL Statement"
  • 50. We have the following tableTABLE CustomerWe want to create a view called V_Customer that contains only the First_Name, Last_Name, and Country columns from this table, we would type in,CREATE VIEW V_CustomerAS SELECT First_Name, Last_Name, CountryFROM Customer
  • 51. Now we have a view called V_Customer with the following structure: View V_Customer(First_Name char(50),Last_Name char(50),Country char(25))
  • 52. We can also use a view to apply joins to two tables. In this case, users only see one view rather than two tables, and the SQL statement users need to issue becomes much simpler. Let's say we have the following two tablesTable Store_InformationTable Geography
  • 53. We want to build a view that has sales by region informationCREATE VIEW V_REGION_SALESAS SELECT A1.region_name REGION, SUM(A2.Sales) SALESFROM Geography A1, Store_Information A2WHERE A1.store_name = A2.store_nameGROUP BY A1.region_nameThis gives us a view, V_REGION_SALES, that has been defined to store sales by region records.
  • 54. SELECT * FROM V_REGION_SALES
  • 55. Exercises:According to the table given, write SQL query for each of the following questions.Add a column called “TelNum” to this table.Change the column name for “Reg_Num” to “RegistrationNum”.Modify the data type of “Year_Born” to date.Delete the column “TelNum”.
  • 56. TABLE CONSTRAINTS IN SQLNOT NULL
  • 59. FOREIGN KEYTABLE CONSTRAINTYou can place constraints to limit the type of data that can go into a table. Such constraints can be specified when the table when the table is first created via the CREATE TABLE statement, or after the table is already created via the ALTER TABLE statement.
  • 60. 1) NOT NULLBy default, a column can hold NULL. If you not want to allow NULL value in a column, you will want to place a constraint on this column specifying that NULL is now not an allowable value.
  • 61. Columns "SID" and "Last_Name" cannot include NULL, while "First_Name" can include NULL.Result :
  • 62. 2) UNIQUEThe UNIQUE constraint ensures that all values in a column are distinct.“SID” column in customer table is modify so that "SID" column cannot include duplicate values, while such constraint does not hold for columns "Last_Name" and "First_Name".
  • 63. Result :Indicates that “SID” column is unique
  • 64. 3) PRIMARY KEYA primary key is used to uniquely identify each row in a table. It can either be part of the actual record itself , or it can be an artificial field. A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key. Primary keys can be specified either when the table is created (using CREATE TABLE) or by changing the existing table structure (using ALTER TABLE).
  • 65. Example :CREATE TABLE Customer (SID integer, Last_Name varchar(30), First_Name varchar(30), PRIMARY KEY (SID)); ALTER TABLE Customer ADD PRIMARY KEY (SID); Indicate that “SID” column has been choose to be the primary key.
  • 67. 4) FOREIGN KEYA foreign key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.
  • 69. CREATE TABLE ORDERS (Order_ID integer, Order_Date date, Customer_SID integer, Amount double, Primary Key (Order_ID), Foreign Key (Customer_SID) references CUSTOMER(SID));  ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);Indicates the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the CUSTOMER table