0% found this document useful (0 votes)
55 views

The SQL Create Database Statement

The document discusses various SQL statements for creating, manipulating, and deleting database objects and data. It includes statements for creating databases and tables, inserting, updating, and deleting data, and joining tables. Examples are provided for creating tables with columns, primary keys, foreign keys and composite keys. Updates are demonstrated using WHERE clauses and SET to modify column values.

Uploaded by

Ahmed Sartaj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

The SQL Create Database Statement

The document discusses various SQL statements for creating, manipulating, and deleting database objects and data. It includes statements for creating databases and tables, inserting, updating, and deleting data, and joining tables. Examples are provided for creating tables with columns, primary keys, foreign keys and composite keys. Updates are demonstrated using WHERE clauses and SET to modify column values.

Uploaded by

Ahmed Sartaj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

The SQL CREATE DATABASE Statement

CREATE DATABASE databasename;

CREATE DATABASE testDB;

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

CREATE TABLE suppliers


( supplier_id int NOT NULL,
supplier_name char(50) NOT NULL,
contact_name char(50),
CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

Composite key
create table my_table (
column_a integer not null,
column_b integer not null,
column_c varchar(50),
primary key (column_a, column_b)
);

INSERT INTO Example


INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

UPDATE TABLE
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

UPDATE customers
SET first_name = 'Judy'
WHERE customer_id = 8000;

UPDATE suppliers
SET supplier_id = 150,
supplier_name = 'Apple',
city = 'Cupertino'
WHERE supplier_name = 'Google';

UPDATE summary_data
SET current_category = (SELECT category_id
FROM products
WHERE products.product_id = summary_data.product_id)
WHERE EXISTS (SELECT category_id
FROM products
WHERE products.product_id = summary_data.product_id);

Delete
DELETE FROM suppliers
WHERE supplier_name = 'Microsoft';

DELETE FROM products


WHERE category_id = 50
AND product_name <> 'Pear';

DELETE FROM EMP_DETAIL


FROM EMP_DETAIL INNER JOIN
EMPLOYEE ON EMP_DETAIL.E_ID = EMPLOYEE.EMP_ID
WHERE (EMPLOYEE.EMP_ID = 3)
SQL Tutorial
SQL HOME SQL Intro SQL Syntax SQL Select SQL Select Distinct SQL Where SQL And, Or,
Not SQL Order By SQL Insert Into SQL Null Values SQL Update SQL Delete SQL Select Top
SQL Min and Max SQL Count, Avg, Sum SQL Like SQL Wildcards SQL In SQL Between SQL
Aliases SQL Joins SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL Self Join
SQL Union SQL Group By SQL Having SQL Exists SQL Any, All SQL Select Into SQL Insert
Into Select SQL Null Functions SQL Comments

SQL Database
SQL Create DB SQL Drop DB SQL Create Table SQL Drop Table SQL Alter Table SQL
Constraints SQL Not Null SQL Unique SQL Primary Key SQL Foreign Key SQL Check SQL
Default SQL Index SQL Auto Increment SQL Dates SQL Views SQL Injection SQL Hosting

You might also like