The document outlines 12 common SQL queries used for database management. These include queries to create databases and tables, select data, insert, update, and delete records, as well as change table design and drop databases and tables. Examples are provided for each query type using a sample person table.
The document outlines 12 common SQL queries used for database management. These include queries to create databases and tables, select data, insert, update, and delete records, as well as change table design and drop databases and tables. Examples are provided for each query type using a sample person table.
1 Create new database CREATE DATABASE database CREATE DATABASE mydb
2 Select a database USE database USE mydb
3 Create new table CREATE TABLE table ( CREATE TABLE person (
attribute type [options], id INT IDENTITY (1,1), attribute type [options], name VARCHAR(20) NOT NULL, attribute type [options], birthday DATE NOT NULL, attribute type [options], mobile VARCHAR(10) UNIQUE, attribute type [options], nationality VARCHAR (20) attribute type [options], DEFAULT ‘Viet Nam’, … email VARCHAR(30) NULL ) ) 4 Set primary key CREATE TABLE table ( CREATE TABLE person ( attribute type PRIMARY KEY) id INT PRIMARY KEY, ) )
CREATE TABLE table ( CREATE TABLE person (
attribute type, id INT, PRIMARY KEY (attribute) PRIMARY KEY (id) ) ) 5 Set foreign key CREATE TABLE table1 ( CREATE TABLE person ( attribute type, PersonId INT PRIMARY KEY, PRIMARY KEY (attribute) ) ) CREATE TABLE company ( CREATE TABLE table2 ( company_id INT PRIMARY KEY, attribute type, person_id INT NOT NULL, FOREIGN KEY (attribute) FOREIGN KEY (person_id) REFERENCES table1 (attribute) REFERENCES person (PersonId) ) ) 6 Change table design ALTER TABLE table ALTER TABLE person ADD attribute type ADD gender VARCHAR(10)
ALTER TABLE table ALTER TABLE person
DROP COLUMN attribute DROP COLUMN email 7 Select data from SELECT * FROM table SELECT * FROM person table SELECT * FROM table WHERE SELECT * FROM person WHERE name = attribute = value ‘Nguyen Hoang Long’
SELECT attribute(s) FROM table SELECT name, birthday FROM person
WHERE attribute = value WHERE nationality = ‘Singapore’ 8 Insert record to table INSERT INTO table (attribute, INSERT INTO person (name, attribute,…) VALUES (value, birthday) VALUES (‘Tuan Hung’, value,…) ’1982-08-06’) 9 Update record in UPDATE table SET attribute = UPDATE person SET name = ‘Tuan table value WHERE attribute = value Minh’ WHERE id = 18 10 Delete record from DELETE FROM table WHERE DELETE FROM person WHERE id = 20 table attribute = value 11 Delete a table DROP TABLE table DROP TABLE person
12 Delete a database DROP DATABASE database DROP DATABASE mydb