You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT*FROM users;
SELECT first_name, last_name FROM users;
Where Clause
SELECT*FROM users WHERE location='Massachusetts';
Conditions
field1 = value1 --equal
field1 <> value1 --not equal
field1 IS NULL
field1 IS NOT NULL
field1 IS IN (value1, value2)
field1 IS NOT IN (value1, value2)
condition1 AND condition2
condition1 OR condition2
Limit Output
SELECT*FROM users WHERE location='Massachusetts'LIMIT10;
Delete Row
DELETEFROM users WHERE id =6;
Update Row
UPDATE users SET email ='freddy@gmail.com'WHERE id =2;
Add New Column
ALTERTABLE users ADD age VARCHAR(3);
Modify Column
ALTERTABLE users MODIFY COLUMN age INT(3);
Sort / Order By
SELECT*FROM users ORDER BY last_name ASC;
SELECT*FROM users ORDER BY last_name DESC;
Add Columns
SELECT CONCAT(first_name, '', last_name) AS'Name', dept FROM users;
Select Unique Rows
SELECT DISTINCT location FROM users;
Select from Range / BETWEEN
SELECT*FROM users
WHERE age BETWEEN 20AND25;
Searching / Like
SELECT*FROM users WHERE first_name LIKE'p%';
SELECT*FROM users WHERE first_name LIKE'pol%';
SELECT*FROM users WHERE first_name LIKE'%k';
SELECT*FROM users WHERE first_name LIKE'%ol%';
Not Like
SELECT*FROM users WHERE first_name NOT LIKE'd%';
IN
SELECT*FROM users WHERE first_name IN ('Polak', 'Shawon');
Create New Table With Foreign Key
CREATETABLEposts(
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
title VARCHAR(100),
body TEXT,
publish_date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
Add Data to Posts Table
INSERT INTO posts(user_id, title, body)
VALUES (1, 'Post One', 'This is post one'),
(3, 'Post Two', 'This is post two'),
(1, 'Post Three', 'This is post three'),
(2, 'Post Four', 'This is post four'),
(5, 'Post Five', 'This is post five'),
(4, 'Post Six', 'This is post six'),
(2, 'Post Seven', 'This is post seven'),
(1, 'Post Eight', 'This is post eight'),
(3, 'Post Nine', 'This is post none'),
(4, 'Post Ten', 'This is post ten');
SELECTCOUNT(id) FROM users;
SELECTMAX(age) FROM users;
SELECTMIN(age) FROM users;
SELECTSUM(age) FROM users;
SELECT UCASE(first_name), LCASE(last_name) FROM users;
Group By
SELECT age, COUNT(age) FROM users GROUP BY age;
SELECT age, COUNT(age) FROM users WHERE age >20GROUP BY age;
SELECT age, COUNT(age) FROM users GROUP BY age HAVINGcount(age) >=2;
mysql -u Username -p dbNameYouWant <databasename_backup.sql;
Data Types
TINYINT (1o: -128 to +127)
SMALLINT (2o: +-65000)
MEDIUMINT (3o: +-16000000)
INT (4o: +-2000000000)
BIGINT (8o: +-9.10^18)
FLOAT(M,D)
DOUBLE(M,D)
FLOAT(D=0->53)
TIME (HH:MM)
YEAR (AAAA)
DATE (AAAA-MM-JJ)
DATETIME (AAAA-MM-JJ HH:MM; années 1000->9999)
TIMESTAMP (like DATETIME, but 1970->2038, compatible with Unix)
VARCHAR (single-line; explicit size)
TEXT (multi-lines; max size=65535)
BLOB (binary; max size=65535)
ENUM ('value1', 'value2', ...) -- (default NULL, or '' if NOT NULL)
Reset root Password
sudo systemctl stop mysql
mysqld_safe --skip-grant-tables
mysql # another tab
mysql> UPDATE mysql.user SET password=PASSWORD('new_pass') WHERE user='root';