Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

sql

The document provides a comprehensive guide on SQL commands for managing databases and tables, including how to create, delete, and modify databases and tables. It covers operations such as inserting, updating, and deleting rows, as well as using joins and constraints like primary and foreign keys. Additionally, it explains the use of autocommit, commit, rollback, and various data types for date and time management.

Uploaded by

suranjan.jana1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

sql

The document provides a comprehensive guide on SQL commands for managing databases and tables, including how to create, delete, and modify databases and tables. It covers operations such as inserting, updating, and deleting rows, as well as using joins and constraints like primary and foreign keys. Additionally, it explains the use of autocommit, commit, rollback, and various data types for date and time management.

Uploaded by

suranjan.jana1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL

database
[1] how to create a database-- “ create database database_name; ”

example – create database customer;

[2] how to delete a database – “ drop database database_name;”

example – drop database customer;

[3] how to use a database – “ use database_name;”

example-- use customer;

[4] how to enable read-only mode in database – alter database database_name read only =1;

example- alter database customer read only =1;

//We can access the database but cannot change it.. like we cannot perform any task in it

[5] how to remove it – alter database database_name read only =0;

example- alter database customer read only =0;

Table:
[1] how to create a table in a database –

create table table_name(

entity type , // each entity of the table separated by a “comma sign ,”

entity type, // entity are generally column names or attributes, and types are the return type.

entity type

);

example –

create table employees(


customer_id INT,

first_name varchar(50),

last_name varchar(50),

hourly_pay DECIMAL(5, 2),

hire_date date

);

[2] how to see a table all entities in a table – “select * from table_name;”

example – select * from employees;

[3] how to drop a table – “ drop table table_name;”

example-- drop table employees;

[4] rename a table – “ rename table old_name to new_name;”

example – rename table employees to workers;

[5] add and change the position of a column example

example – alter table employees

add phone_number varchar(50)

after last_name;

[6] rename a column with an example --

example--- alter table employees

rename column phone_number to email;

[7] modify an entity of a column with example ---

example-- alter table employees

modify column email varchar(50);

[8] drop a specific column ---

example – alter table employees

drop column email ;


Rows/ index:
[1] how to fill the values in a column with an example --

//date format should be - {year-month-date}

//Each row is separated with a comma,

insert into employees

values

(1, "suranjan", "jana", 45.00, "2024-01-25"),

(2, "subhendu", "jana", 60.00, "2024-01-23"),

(3, "subhendu", "jana", 60.00, "2024-01-24");

[2] fill value only the specific column --

insert into employees(customer_id, first_name,last_name)

values

(4, "sunit", "jana");

Select
[1] for select all –

select * from employees;

[2] for a specific column –

select last_name, first_name

from employees;

//the display order is based on the sequence of input

[3] where clause –

select *

from employees
where last_name="jana";

[4] where clause for finding null values--

select *

from employees

where hourly_pay is null;

Update & Delete


update a row --

update employees

set

hourly_pay = 10.58,

hire_date = "2025-01-07"

where customer_id = 4;

delete a row --

delete from employees

where customer_id =4;

Autocommit, Commit, Rollback


set autocommit = off; // it allows you to enter a change manually

commit; //We need to use this statement before any changes

select * from employees;

delete from employees;

rollback; // this statement is used to revert the current transaction

ex-- if we delete the table then it helps to return it back


Date, Time & DateTime
// creation of table

create table test(

my_date date,

my_time time,

my_date_time datetime

);

// insert rows [note – +1 used for tomorrow , -1 used for yesterday with function]

insert into test

values ( current_date(), current_time(), now() );

select * from test;

Unique:
create table products (

product_id int ,

product_name varchar(50) unique,

price decimal(5, 3)

);

// alternative way of adding “unique”

alter table products

add constraint

unique( product_id );
Notnull:
Check:
Default:

primary key:
if we declare a column as the primary key then each value of that column should be

not null and unique.

Each table can only have one primary key

Note: The primary key can be a single column or multiple columns.

alter table transactions

add constraint

primary key ( amount,transaction_id );

// creation of a transaction table ---

create table transactions (

transaction_id int primary key,

amount decimal(5, 2) );

// insert data into table

insert into transactions

values (1001 , 20.6),

(1002 , 20.3),
(1005,52.6);

select * from transactions;

drop table transactions;

auto increament

foreign key:
// creation of a table with primary key

CREATE TABLE customers (

customer_id INT PRIMARY KEY AUTO_INCREMENT,

first_name VARCHAR(50),

last_name VARCHAR(50)

);

// inserting the values in the table

INSERT INTO customers (first_name, last_name)

VALUES ("Fred", "Fish"),

("Larry", "Lobster"),

("Bubble", "Bass");

select* from customers ;

drop table customers;

//Another table with foreign key


CREATE TABLE transactions (

transaction_id INT PRIMARY KEY AUTO_INCREMENT,

amount DECIMAL(5, 2),

customer_id INT,

FOREIGN KEY ( customer_id ) REFERENCES customers (customer_id )

); // primary key of another table

select * from transactions;

drop table transactions ;

alter table transactions

drop foreign key transactions_ibfk_1;

Joins:
CREATE TABLE Customers (

customer_id INT PRIMARY KEY,

first_name VARCHAR(50),

last_name VARCHAR(50),

age INT,

country VARCHAR(50)

);

CREATE TABLE Orders (

order_id INT PRIMARY KEY,

item VARCHAR(50),
amount DECIMAL(10, 2),

customer_id INT,

FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)

);

INSERT INTO Customers (customer_id, first_name, last_name, age, country) VALUES

(1, 'John', 'Doe', 31, 'USA'),

(2, 'Robert', 'Luna', 22, 'USA'),

(3, 'David', 'Robinson', 22, 'UK'),

(4, 'John', 'Reinhardt', 25, 'UK'),

(5, 'Betty', 'Doe', 28, 'UAE');

INSERT INTO Orders (order_id, item, amount, customer_id) VALUES

(1, 'Keyboard', 400, 4),

(2, 'Mouse', 300, 4),

(3, 'Monitor', 12000, 3),

(4, 'Keyboard', 400, 1),

(5, 'Mousepad', 250, 2);

select * from customers;

select * from orders;

-- join Customers and Orders tables with their matching fields customer_id
SELECT * /*Customers.customer_id, Orders.item*/

FROM Customers

INNER JOIN Orders

ON Customers.customer_id = Orders.customer_id;

left , right joins

and or & not

order by:

limit:
select * from table_name

limit 10;
union:
union is used to join two select statement

but no columns should be similar between the two table

You might also like