Revsyitchit
Revsyitchit
Table Operations
- Create a table : CREATE TABLE (id INT, data Varchar(100));
- Drop a table : DROP TABLE tablename;
- List all tables : SHOW TABLES;
- Add a column : ALTER TABLE tablename ADD column_name
datatype;
- Drop a column : ALTER TABLE tablename DROP column_name;
- Modify a column type : ALTER TABLE tablename MODIFY column_name
new_datatype;
- Add a primary key : ALTER TABLE tablename ADD PRIMARY KEY
(column_name);
- Drop a primary key : ALTER TABLE tablename DROP PRIMARY KEY;
- Add unique constraint : ALTER TABLE tablename ADD UNIQUE
(column_name);
- Add a foreign key : ALTER TABLE tablename ADD CONSTRAINT
fk_name FOREIGN KEY (column_name) REFERENCES
other_table(column_name);
- Drop a foreign key : ALTER TABLE tablename DROP FOREIGN KEY
fk_name;
2. Data manipulations
- Insert a row : INSERT INTO tablename (column1, column2)
VALUES (value1,value2);
- Update rows : INSERT INTO tablename SET column1 = value1
WHERE condition;
- Delete rows : DELETE FROM tablename WHERE condition;
- Select data : SELECT * FROM tablename;
- Select data with condition : SELECT * FROM tablename WHERE
condition;
- Select and order data : SELECT * FROM tablename ORDER BY column
ASC|DESC;
- Select distinct rows : SELECT DISTINCT column FROM tablename;
- Count rows : SELECT COUNT (*) FROM tablename;
- Sum a column : SELECT SUM (column) FROM tablename;
- Average a column : SELECT AVG (column) FROM tablename;
3. ON DELETE CASCADE = Deletes all rows in the child table that reference the
deleted row in the parent table /* use in foreign key */
4. ON UPDATE CASCADE = Updates all corresponding foreign key values in the
child table to match the new primary key value in the parent table /* use in
foreign key */