0% found this document useful (0 votes)
37 views6 pages

Syntax Reviewer

The document describes the syntax for creating, altering, and manipulating databases and tables in SQL. It includes examples for creating databases and tables, altering table and column names, adding and dropping columns and constraints, inserting, updating, and deleting data, and dropping tables. The key SQL commands covered are CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, TRUNCATE, and JOIN.

Uploaded by

carekc24id
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)
37 views6 pages

Syntax Reviewer

The document describes the syntax for creating, altering, and manipulating databases and tables in SQL. It includes examples for creating databases and tables, altering table and column names, adding and dropping columns and constraints, inserting, updating, and deleting data, and dropping tables. The key SQL commands covered are CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, TRUNCATE, and JOIN.

Uploaded by

carekc24id
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
You are on page 1/ 6

syntax_reviewer

==================================================================================
Creating database:
CREATE DATABASE database_name;

EXAMPLE:
CREATE DATABASE herotry;

----------------------------------------------------------------------------------
Changing name of database:
ALTER DATABASE current_name RENAME TO new_name;

EXAMPLE:
ALTER DATABASE herotry RENAME TO game_database;

----------------------------------------------------------------------------------
Dropping database:
DROP DATABASE database_name;

EXAMPLE:
DROP DATABASE game_database;

==================================================================================

-- Creating table
CREATE TABLE table_name(
column1 data_type constraints,
column2 data_type constraints
);

EXAMPLES
-- table 1
CREATE TABLE player(
player_id INT PRIMARY KEY,
player_name VARCHAR(500) NOT NULL UNIQUE,
email VARCHAR(500) NOT NULL UNIQUE,
password VARCHAR(500) NOT NULL,
Registration_date DATE DEFAULT CURRENT_DATE
);

-- table 2
CREATE TABLE character(
character_id INT PRIMARY KEY,
character_name VARCHAR(500) NOT NULL,
level INT check (level >= 1),
exp INT check (exp >= 0),
player_id INT,
foreign key (player_id) references player(player_id)
);

-- table 3 (one to one)


CREATE TABLE class (
class_id INT PRIMARY KEY,
class_name VARCHAR(500) NOT NULL,
character_id INT UNIQUE,
FOREIGN KEY (character_id) REFERENCES character(character_id)
);
-- table 4
CREATE TABLE skill(
skill_id INT PRIMARY KEY NOT NULL,
skillName VARCHAR(500) NOT NULL,
is_Active BOOL DEFAULT true,
character_id INT,
foreign key (character_id) references character(character_id)
);

-- table 5
CREATE TABLE item(
item_id INT PRIMARY KEY NOT NULL,
item_name VARCHAR(500) NOT NULL,
/*(if you want to put fix categories for items):
item_categ VARCHAR(500) CHECK (item_categ IN('Weapon', 'Armor',
'Consumable'))
*/
item_categ VARCHAR(500)
);

-- table 6 (for many to many)


CREATE TABLE character_item (
character_id INT,
item_id INT,
PRIMARY KEY (character_id, item_id),
FOREIGN KEY (character_id) REFERENCES character(character_id),
FOREIGN KEY (item_id) REFERENCES item(item_id)
);

==================================================================================

-- Altering table

-- Alter table name


ALTER TABLE table_name RENAME TO new_table_name;

EXAMPLES:

-- player to user
ALTER TABLE "player" RENAME TO "user";

-- user to player
ALTER TABLE "user" RENAME TO "player";

----------------------------------------------------------------------------------

-- Alter column name


ALTER TABLE table_name
RENAME COLUMN column_name TO new_column_name;

EXAMPLES:

-- changing column from character table: "character_name" to "name_of_char"


ALTER TABLE character
RENAME COLUMN character_name TO name_of_char;

-- changing column from character table: "name_of_char" to "character_name"


ALTER TABLE character
RENAME COLUMN name_of_char TO character_name;

----------------------------------------------------------------------------------

-- Add column
ALTER TABLE table_name
ADD COLUMN new_column_name data_type;

EXAMPLE:
ALTER TABLE player
ADD COLUMN age INT CHECK(age >= 18);

/*
additional info for dropping constrain and dropping column:
-- DROPPING CONSTRAINT
ALTER TABLE player
DROP CONSTRAINT player_age_check;

--DROPPING COLUMN
ALTER TABLE player
DROP COLUMN age;
*/

==================================================================================

-- Delete Constraints
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

EXAMPLES:

-- drop primary key of skill_id


ALTER TABLE skill
DROP CONSTRAINT skill_pkey;

-- drop foreign key of character_id


ALTER TABLE skill
DROP CONSTRAINT skill_character_id_fkey;

==================================================================================

-- Add Constraint

-- Adding primary key


ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);

EXAMPLE:
Adding primary key to skill_id
ALTER TABLE skill
ADD CONSTRAINT skill_pkey PRIMARY KEY (skill_id);

----------------------------------------------------------------------------------

-- Adding foreign key


ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name)
REFERENCES reference_table(reference_column);
EXAMPLE:
-- Adding foreign key to character_id from skill table ta's ang reference ay
character_id from character table
ALTER TABLE skill
ADD CONSTRAINT skill_character_id_fkey
FOREIGN KEY (character_id)
REFERENCES character(character_id);

----------------------------------------------------------------------------------

-- additional info sa add constraint: (not included sa db na 'to)

-- Add a Unique Constraint


ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column_name);

EXAMPLE:
ALTER TABLE Students
ADD CONSTRAINT UQ_StudentEmail UNIQUE (Email);

----------------------------------------------------------------------------------

-- Add Check Constraint (not included sa db na 'to)


ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK (condition);

EXAMPLE:
ALTER TABLE Orders
ADD CONSTRAINT CheckOrderTotal CHECK (Total >= 0);

==================================================================================

-- Setting and dropping not null (NOT NULL IS ALSO A CONSTRAINT)

-- removing NOT NULL in a column

ALTER TABLE table_name


ALTER COLUMN column_name DROP NOT NULL;

-- set NOT NULL in a column

ALTER TABLE table_name


ALTER COLUMN column_name SET NOT NULL;

==================================================================================

-- Inserting data

INSERT INTO table_name (column1, column2, ...)


VALUES (value1, value2, ...);

EXAMPLES:

-- Sample data for the player table


INSERT INTO player (player_id, player_name, email, password)
VALUES
(1, 'Daryll', '[email protected]', 'pogi123'),
(2, 'Von', '[email protected]', 'von123456789'),
(3, 'Kurt', '[email protected]', 'kurtpogi');

-- Sample data for the character table


INSERT INTO character (character_id, character_name, level, exp, player_id)
VALUES
(1, 'Goshon', 5, 1000, 1),
(2, 'Zelung', 3, 750, 2),
(3, 'Seselion', 4, 800, 3);

-- Sample data for the class table


INSERT INTO class (class_id, class_name, character_id)
VALUES
(1, 'Assassin', 1), -- Assassin class for character with character_id 1
(2, 'Fighter', 2), -- Fighter class for character with character_id 2
(3, 'Mage', 3); -- Mage class for character with character_id 3

-- Sample data for the skill table


INSERT INTO skill (skill_id, skillName, character_id, is_Active)
VALUES
(1, 'Sword Spike', 1, true),
(2, 'Spear Strike', 2, true),
(3, 'Bat Impact', 3, true);

-- Sample data for the item table


INSERT INTO item (item_id, item_name, item_categ)
VALUES
(1, 'Blade of Despair', 'Physical Item'),
(2, 'Cloak of Destiny', 'Magic Item'),
(3, 'Health Potion', 'Consumable');

-- Sample data for the hero_items table (many-to-many relationship)


INSERT INTO character_item (character_id, item_id)
VALUES
(1, 2), -- Gusion has Cloak of Destiny
(2, 1), -- Zilong has Blade of Despair
(3, 3), -- Nana has Health Potion
(2, 3); -- Zilong also has Health Potion

==================================================================================

UPDATE Data Example:

UPDATE skill
SET is_Active = false
WHERE skillName = 'Spear Strike';

==================================================================================

DELETE DATA

Delete all data from a table:


DELETE FROM table_name;

or you may also use:


TRUNCATE TABLE table_name;

----------------------------------------------------------------------------------

Delete data based on a condition:


DELETE FROM player
WHERE player_name = 'Daryll';

----------------------------------------------------------------------------------

Delete data from a table based on a JOIN condition:


DELETE FROM character_item
WHERE character_id = (
SELECT character_id
FROM character
WHERE character_name = 'Goshon'
);

==================================================================================

-- Dito ko na inilagay para mas madali and matatry

Drop column:

ALTER TABLE table_name


DROP COLUMN column_name;

----------------------------------------------------------------------------------

Drop table:
DROP TABLE table_name;

or you may use this to avoid errors:


DROP TABLE IF EXISTS employees;

You might also like