- 创建数据库
CREATE DATABASE database_name;
- 使用mysqladmin创建数据库
mysqladmin -u root -p create database_name;
- Note: 使用普通用户需要特定的权限来创建数据库或着删除数据库
- 删除数据库
drop database database_name;
- 使用mysqladmin删除数据库
mysqladmin -u root -p drop database_name;
- 选中数据库
use database_name;
- 创建数据库表
CREATE TABLE table_name (column_name column_type);
CREATE TABLE IF NOT EXISTS `user` (
`id` INT UNSIGNED PRIMARY_KEY AUTO_INCREMENT,
`desc` VARCHAR(255) NOT NULL,
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 删除数据库表
drop table table_name;
drop table user;
- 插入数据
INSERT INTO table_name (field1, field2, field3...) VALUES(value1, value2, value3...);
INSERT INTO user (username password)
VALUES('TONY', '123'),
('TOM', '1234');
- 查询数据
SELECT columns_name ... FROM table_name...
SELECT username FROM user;
- where字句
SELECT field1, ... FROM table_name, ... WHERE condition1 [AND [OR]] condition2....;
SELECT username FROM user where id = 1;
- UPDATE字句
UPDATE table_name SET field=new-value, filed1=new-value;
UPDATE user SET username='runserver';
- DELETE语句
DELETE FROM table_name [WHERE Clause];
DELETE FROM user WHERE username='runserver';
- LIKE字句
SELECT field1,... FROM table_name WHERE field LIKE condition [AND [OR]] field2 = 'do';
SELECT * FROM user WHERE username LIKE '%ver';
- UNION操作符
SELECT field....
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT field....
FROM tables
[WHERE condition];
SELECT country FROM websites
UNION
SELECT country FROM apps
ORDER BY country;
SELECT country FROM websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
SELECT country, name FROM websites
WHERE country='cn'
UNION ALL
SELECT country, app_name FROM apps
WHERE country = 'cn'
ORDER BY country;
- 排序
SELECT field... FROM talbe1, table2...
ORDER BY field1, [field2...] [ASC [DESC]]
SELECT * FROM user ORDER BY id asc;
- GROUP BY语句
SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
SELECT name, COUNT(*) FROM user GROUP BY id;
SELECT name SUM(score) as score_number
FROM user GROUP BY name WITH ROLLUP;
SELECT coalesce(name, 'total'), SUM(socre) as sum FROM user
GROUP BY name WITH ROLLUP;
Note:WITH ROLLUP可以实现在分组统计数据基础上在进行相同的统计
coalesce()可以设置一个取代null的名称
今天先弄到这