0% found this document useful (0 votes)
4 views

mysql

Uploaded by

24f1000294
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

mysql

Uploaded by

24f1000294
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

SELECT - extracts data from a database

UPDATE - updates data in a database


DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least
2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at
least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"

#=================================================

CREATE TABLE Student(Rollno INT(3), Name VARCHAR(20),Gender VARCHAR(1), Marks


INT(3), DOB DATE, Mobile_no BIGINT(11), Stream VARCHAR(10));

INSERT INTO Student VALUES(1, "Raj Kumar", "M", 93, "20001117", 9586774748,
"Science"), (3, "Ankit Sharma", "M", 76, "20000202",8567490078, "Science"), (4,
"Radhika Gupta", "F", 78, "19991203", 9818675444, "Humanities"), (5, "Payal Goel",
"F", 82, "19980421", 9845639990, "Vocational"), (6, "Diksha Sharma", "F", 80,
"19991217", 9897666650, "Humanities"), (7, "Gurpreet Kaur", "F", 65, "20000104",
7560567890, "Science"), (9, "Shreya Anand", "F", 70, "19991008", 8876543988,
"Vocational"), (10, "Prateek Mittal", "M", 75, "20001225", 9999967543, "Science");

select * from student;

SELECT * FROM STUDENT WHERE Marks>80;

SELECT Rollno, Marks FROM STUDENT;

SELECT Rollno, marks FROM STUDENT WHERE marks>80;

SELECT * FROM STUDENT WHERE MARKS>80 AND GENDER="M";

SELECT * FROM STUDENT WHERE MARKS>90 OR MARKS<70;

SELECT Name, Marks+4 FROM STUDENT;

SELECT * FROM STUDENT WHERE Name like "p%";

SELECT * FROM STUDENT WHERE MARKS BETWEEN 80 AND 90;

SELECT * FROM STUDENT WHERE STREAM IN ("Humanities", "Vocational");

UPDATE Student SET MARKS = MARKS+5 WHERE MARKS<=70;

SELECT * FROM STUDENT ORDER BY Marks;


SELECT * FROM STUDENT ORDER BY Marks DESC;

SELECT * FROM Student GROUP BY Stream HAVING Marks>80;

SELECT DISTINCT Stream FROM Student;

SELECT MAX(Marks), MIN(Marks) FROM Student;

SELECT SUM(Marks), AVG(Marks) FROM Student;

SELECT Stream, COUNT(*) FROM Student GROUP BY Stream;

ALTER TABLE Student ADD PRIMARY KEY (Rollno);

#==========================================================
UPDATE table_name SET column1 = value1;
UPDATE table_name SET column_1 = value_1, WHERE condition
SHOW DATABASES;
SHOW TABLES;
DESCRIBE table_name;
CREATE TABLE table_name(Column_name1 datatype, Column_name2 datatype……);
INSERT INTO table_name VALUES (value1, value2, value3, …);
To add a new column=>
ALTER TABLE table ADD column_name datatype;
To remove a column=>
ALTER TABLE table_name DROP column_name;
To add an index to a table on a column with a specified name=>
ALTER TABLE table ADD INDEX [name](column, …);
To add a primary key to a table
ALTER TABLE table_name ADD PRIMARY KEY (column_name,…);
To remove the primary key of a table
ALTER TABLE table_name DROP PRIMARY KEY;
To drop a table
DROP TABLE table_name;
To drop a database
DROP DATABASE database_name;
To rename a table
RENAME TABLE old_table_name TO new_table_name;
To rename a database
RENAME DATABASE old_database_name TO new_database_name;
To rename a column
ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
To modify the datatype of a column
ALTER TABLE table_name MODIFY column_name datatype;
To add a column after another column
ALTER TABLE table_name ADD column_name datatype AFTER column_name;
To add a column before another column
ALTER TABLE table_name ADD column_name datatype BEFORE column_name;
To add a column at the beginning of the table
ALTER TABLE table_name ADD column_name datatype FIRST;
To add a column at the end of the table
ALTER TABLE table_name ADD column_name datatype LAST;
To delete all the records from a table
DELETE FROM table_name;
To delete all the records from a table and reset the auto increment value
TRUNCATE TABLE table_name;
To delete a record from a table
DELETE FROM table_name WHERE condition;
To show the columns of a table
SHOW COLUMNS FROM table_name;
To show the indexes of a table
SHOW INDEX FROM table_name;
To show the status of a table
SHOW TABLE STATUS FROM database_name LIKE 'table_name';
To show the status of a database
SHOW DATABASE STATUS FROM database_name LIKE 'table_name';
To create an index with the specified name on a table:
CREATE INDEX index_name ON table_name (column,…);
TO create a unique index with the specified name on a table:
CREATE UNIQUE INDEX index_name ON table_name (column,…);
To drop an index from a table:
DROP INDEX index_name ON table_name;

#==========================================================

SELECT column1, column2, … FROM Table_name;

Remove duplicate rows from a query’s result:


SELECT
DISTINCT (column)
FROM
Table_name;

We can filter records using WHERE clause. It returns only those records which
fulfill a specified set of condition.
SELECT column_list
FROM table_name
WHERE condition;

You can change the output of the column name using column alias:
SELECT
column1 AS alias_name,
expression AS alias,
...
FROM
Table_name;

Counting the number of rows in a table:


SELECT COUNT(*)
FROM table_name;

SELECT
select_list
FROM
table_name
ORDER BY
column1 ASC [DESC],
column2 ASC [DESC];

Group rows using the GROUP BY clause.


SELECT select_list
FROM table_name
GROUP BY column_1, column_2, …;

To query data from multiple tables using inner join, use the following command:
SELECT select_list
FROM table1
INNER JOIN table2 ON condition;

SELECT select_list
FROM table1
LEFT JOIN table2 ON condition;

SELECT select_list
FROM table1
RIGHT JOIN table2 ON condition;

SELECT select_list
FROM table1
CROSS JOIN table2;

The syntax for LIKE clause


SELECT select_list
FROM table_name
WHERE column LIKE ‘%pattern%’ (or ‘_ABC’);
E.g, ‘S%’ will fetch all values that start with S.
‘_AB’ will fetch all values that have A and B at second and third places
respectively.

You might also like