SlideShare a Scribd company logo
1 of 44
CREATING DATABASE USING SQL COMMANDS
SHOW DATABASE mysql> SHOW DATABASES; Use the SHOW statement to find out what databases currently exist on the server SQL command
CREATE DATABASE mysql> CREATE DATABASE newdb; Database name Create statement The new database will appear after successfully create the database
USE DATABASE mysql> USE newdb Creating 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 command Database name
SHOW TABLES mysql> SHOW TABLES; To create one or more tables in the current database, you can use CREATE TABLE statement. SQL command It indicates that there is no table in the database.
CREATE TABLE mysql> 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 columns DESCRIBE SQL command Database name
ALTER TABLE statement ALTER TABLE is use to modify an existing column It is consists of ADD, MODIFY and DROP column ALTER TABLE statement: ADD column 	MODIFY column 	DROP column ALTER TABLE table ADD (column datatype [DEFAULT expr][column datatype]….); ALTER TABLE table MODIFY (column datatype [DEFAULT expr][column datatype]….); ALTER TABLE table DROP (column datatype [DEFAULT expr][column datatype]….);
ALTER TABLE statement ,[object Object]
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 statement ,[object Object]
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 statement ,[object Object]
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 Design QUERY WITH SQL
DEFINE THE INSTRUCTION SELECT   to query data in the database INSERT  to insert data into a table UPDATE  to update data in a table DELETE  to delete data from a table
SELECT STATEMENT ,[object Object]
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 STATEMENT Select all columns Select specific columns Output SELECT * FROM dept; Output SELECT Deptno, loc FROM dept;
SELECT STATEMENT ,[object Object]
   It is used to restrict or limiting the rows selected
   The WHERE clause follows the FROM clauseSELECT [DISTINCT] {*, column,…} FROM table WHERE condition(s);
SELECT STATEMENT Using where clause Select specific columns Output SELECT ename, job, deptno  FROM dept WHERE job = ‘CLERK’ Output SELECT Deptno, loc  FROM dept WHERE loc = ‘New York’;
SELECT STATEMENT Using BETWEEN operator to display rows based on a range of values  Output SELECT ename, sal  FROM emp WHERE sal BETWEEN 10000 AND 15000;
UPDATE UPDATE item SET Quantity =10,UnitPrice =1800.00 WHERE 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.
DELETE To remove one or more rows from a table. For example , to delete the ItemNo = ‘123’. DELETE FROM item WHERE 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)

Sql basics
Sql  basicsSql  basics
Sql basics
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
DML Commands
DML CommandsDML Commands
DML Commands
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Sql DML
Sql DMLSql DML
Sql DML
 
8. sql
8. sql8. sql
8. sql
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 

Viewers also liked

Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commandsphanleson
 
Top 10 insurance administrator interview questions and answers
Top 10 insurance administrator interview questions and answersTop 10 insurance administrator interview questions and answers
Top 10 insurance administrator interview questions and answerspaul21231z
 
Execute MySQL query using command prompt
Execute MySQL query using command promptExecute MySQL query using command prompt
Execute MySQL query using command promptIkhwan Krisnadi
 
Execute sql query or sql command sql server using command prompt
Execute sql query or sql command sql server using command promptExecute sql query or sql command sql server using command prompt
Execute sql query or sql command sql server using command promptIkhwan Krisnadi
 
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...Denodo
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manualmaha tce
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginnersRam Sagar Mourya
 
ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1Kumar S
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 

Viewers also liked (15)

Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Sql9e ppt ch03
Sql9e ppt ch03 Sql9e ppt ch03
Sql9e ppt ch03
 
Top 10 insurance administrator interview questions and answers
Top 10 insurance administrator interview questions and answersTop 10 insurance administrator interview questions and answers
Top 10 insurance administrator interview questions and answers
 
Execute MySQL query using command prompt
Execute MySQL query using command promptExecute MySQL query using command prompt
Execute MySQL query using command prompt
 
Execute sql query or sql command sql server using command prompt
Execute sql query or sql command sql server using command promptExecute sql query or sql command sql server using command prompt
Execute sql query or sql command sql server using command prompt
 
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
Designing an Agile Fast Data Architecture for Big Data Ecosystem using Logica...
 
Sql queries
Sql queriesSql queries
Sql queries
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manual
 
Sql queires
Sql queiresSql queires
Sql queires
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 

Similar to Creating database using sql commands (20)

Oraclesql
OraclesqlOraclesql
Oraclesql
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 
Babitha2 Mysql
Babitha2 MysqlBabitha2 Mysql
Babitha2 Mysql
 
Sql
SqlSql
Sql
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Query
QueryQuery
Query
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries
 
Les10
Les10Les10
Les10
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Sql commands
Sql commandsSql commands
Sql commands
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
SQL
SQLSQL
SQL
 
Sql
SqlSql
Sql
 
SQL Query
SQL QuerySQL Query
SQL Query
 
1 introduction to my sql
1 introduction to my sql1 introduction to my sql
1 introduction to my sql
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Hira
HiraHira
Hira
 
My sql Syntax
My sql SyntaxMy sql Syntax
My sql Syntax
 
Oracle: Commands
Oracle: CommandsOracle: Commands
Oracle: Commands
 

Creating database using sql commands

  • 1. CREATING DATABASE USING SQL COMMANDS
  • 2. SHOW DATABASE mysql> SHOW DATABASES; Use the SHOW statement to find out what databases currently exist on the server SQL command
  • 3. CREATE DATABASE mysql> CREATE DATABASE newdb; Database name Create statement The new database will appear after successfully create the database
  • 4. USE DATABASE mysql> USE newdb Creating 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 command Database name
  • 5. SHOW TABLES mysql> SHOW TABLES; To create one or more tables in the current database, you can use CREATE TABLE statement. SQL command It indicates that there is no table in the database.
  • 6. CREATE TABLE mysql> 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 columns DESCRIBE SQL command Database name
  • 8. ALTER TABLE statement ALTER TABLE is use to modify an existing column It is consists of ADD, MODIFY and DROP column ALTER TABLE statement: ADD column MODIFY column DROP column ALTER TABLE table ADD (column datatype [DEFAULT expr][column datatype]….); ALTER TABLE table MODIFY (column datatype [DEFAULT expr][column datatype]….); ALTER TABLE table DROP (column datatype [DEFAULT expr][column datatype]….);
  • 9.
  • 10. Table before add new column
  • 11. Execute ALTER TABLE statement to ADD:
  • 14. statement to ADDALTER TABLE emp ADD (job VARCHAR(9));
  • 15.
  • 17. Execute ALTER TABLE statement to MODIFY:
  • 21. MODIFYALTER TABLE emp MODIFY (ename VARCHAR(3));
  • 22.
  • 24. Execute ALTER TABLE statement to DROP:
  • 27. statement to DROP:ALTER TABLE emp DROP COLUMN job;
  • 28. Basic Structures in Query Design QUERY WITH SQL
  • 29. DEFINE THE INSTRUCTION SELECT  to query data in the database INSERT  to insert data into a table UPDATE  to update data in a table DELETE  to delete data from a table
  • 30.
  • 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 STATEMENT Select all columns Select specific columns Output SELECT * FROM dept; Output SELECT Deptno, loc FROM dept;
  • 37.
  • 38. It is used to restrict or limiting the rows selected
  • 39. The WHERE clause follows the FROM clauseSELECT [DISTINCT] {*, column,…} FROM table WHERE condition(s);
  • 40. SELECT STATEMENT Using where clause Select specific columns Output SELECT ename, job, deptno FROM dept WHERE job = ‘CLERK’ Output SELECT Deptno, loc FROM dept WHERE loc = ‘New York’;
  • 41. SELECT STATEMENT Using BETWEEN operator to display rows based on a range of values Output SELECT ename, sal FROM emp WHERE sal BETWEEN 10000 AND 15000;
  • 42. UPDATE UPDATE item SET Quantity =10,UnitPrice =1800.00 WHERE 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. DELETE To remove one or more rows from a table. For example , to delete the ItemNo = ‘123’. DELETE FROM item WHERE 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_Information Result : 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 command Table Sales_Information Table Store_Information Table Sales_Information Table Store_Information
  • 48. After using INSERT INTO command INSERT INTO Store_Information (store_name, Sales, Date)SELECT store_name, Sales, DateFROM Sales_InformationWHERE Year(Date) = 1998 New information that been added to the table Table Sales_Information Table Store_Information
  • 49. CREATE VIEW Views 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 table TABLE Customer We 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 tables Table Store_Information Table Geography
  • 53. We want to build a view that has sales by region information CREATE 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_name This 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.
  • 59.
  • 60. 1) NOT NULL By 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) UNIQUE The 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 KEY A 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 KEY A 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.
  • 68. Table customer Table order Example :
  • 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