0% found this document useful (0 votes)
4 views2 pages

SQL Commands for Car Database Management

The document outlines SQL commands for creating and managing a 'cars' table, including creation, insertion, alteration, and deletion of records. It demonstrates how to add and rename columns, select distinct values, apply filters, order results, limit output, and safely update or delete records. Additionally, it includes examples of SQL queries for various operations on the 'cars' table.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

SQL Commands for Car Database Management

The document outlines SQL commands for creating and managing a 'cars' table, including creation, insertion, alteration, and deletion of records. It demonstrates how to add and rename columns, select distinct values, apply filters, order results, limit output, and safely update or delete records. Additionally, it includes examples of SQL queries for various operations on the 'cars' table.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Creation and Insertion

CREATE TABLE cars (


id SERIAL PRIMARY KEY,
name VARCHAR(50),
model VARCHAR(50),
year INT
);

INSERT INTO cars (name, model, year)


VALUES
('Ford', 'Mustang', 1969),
('Toyota', 'Corolla', 2020),
('Honda', 'Civic', 2018);

// Alter

// Add
alter table cars
add color varchar(10)

// color
alter table cars
rename column color to color_name

// drop
=
alter table cars
drop column color_name

// Distinct + Count
select distinct color from cars;
select count(distinct color) fromt cars;

// where
select name, model from cars
where name = 'Ferrari'

// Order by
select * from cars
order by name;

// Limit
select * from cars
limit 2; // give me the two records

// Limit + offset
select * from cars
limit 2 offset 20; // guve me two records starting from 21

// Always use these ** Safely


// Updating cars [update/set]
update cars
set model='GT69', color='Yellow'
where name = 'Ford'
returning *; // return affected rows

// delete
delete from cars
where name ='Ford' and year = 1971;

You might also like