文章
原文链接
实践
CREATE TABLE friend(
fid INT NOT NULL,
NAME VARCHAR(10) NOT NULL,
age INT NOT NULL,
adress VARCHAR(10)
)
SHOW TABLES;
SELECT * FROM friend;
SELECT fid,NAME FROM friend;
INSERT INTO friend VALUES(1,'Jack',18,'Tianjing');
INSERT INTO friend VALUES(2,'Liming',17,'Beijing');
INSERT INTO friend (fid, NAME, age,adress) VALUES (3,'Zhangwei',22,'Wuhan');
INSERT INTO friend (fid,NAME,age) VALUES (4,'Wangmei',17);
INSERT INTO friend VALUES(5,'Lihua',18,'Shanghei'),
(6,'Wangyang',18,'Shanxi');
INSERT INTO friend VALUES(7,'Penchen',19,'Beijing'),
(8,'Yenuoyi',20,'Wuhan');
SELECT DISTINCT adress FROM friend;
SELECT age FROM friend WHERE age>18;
SELECT * FROM friend WHERE age>18;
SELECT * FROM friend WHERE age>18 AND adress='Wuhan';
SELECT * FROM friend WHERE age<18 OR adress='Beijing';
SELECT * FROM friend WHERE (age<20 AND NAME='Jack') OR adress='Tianjing';
SELECT * FROM friend ORDER BY adress ASC;
SELECT * FROM friend ORDER BY age DESC;
UPDATE friend SET adress='Chengdu' WHERE fid=4;
UPDATE friend SET adress='Sichuan' WHERE NAME='Wangmei';
UPDATE friend SET age=18 WHERE adress='Wuhan';
DELETE FROM friend WHERE fid=8
SELECT * FROM student;
TRUNCATE TABLE student;
SELECT * FROM student;
SELECT * FROM student;
DROP TABLE student;
SELECT * FROM student;
SELECT * FROM friend;
SELECT * FROM friend WHERE NAME LIKE 'L%';
SELECT * FROM friend WHERE adress LIKE '%g';
SELECT * FROM friend WHERE adress NOT LIKE '%ng%';
SELECT * FROM friend;
SELECT * FROM friend WHERE adress IN('Wuhan','Shanghei');
SELECT adress FROM friend WHERE adress IN('wuhan','shanghei');
SELECT * FROM friend WHERE fid BETWEEN 1 AND 5;
SELECT * FROM friend ORDER BY adress ASC;
SELECT * FROM friend WHERE adress BETWEEN 'chengdu' AND 'tianjing';
总结
一些术语解释
附上代码
//创建表
CREATE TABLE friend(
fid INT NOT NULL,
NAME VARCHAR(10) NOT NULL,
age INT NOT NULL,
adress VARCHAR(10)
)ENGINE=INNODB;
//select
SHOW TABLES;
SELECT * FROM friend;
SELECT fid,NAME FROM friend;
//insert
INSERT INTO friend VALUES(1,'Jack',18,'Tianjing');
INSERT INTO friend VALUES(2,'Liming',17,'Beijing');
INSERT INTO friend (fid, NAME, age,adress) VALUES (3,'Zhangwei',22,'Wuhan');
INSERT INTO friend (fid,NAME,age) VALUES (4,'Wangmei',17);
INSERT INTO friend VALUES(5,'Lihua',18,'Shanghei'),
(6,'Wangyang',18,'Shanxi');
INSERT INTO friend VALUES(7,'Penchen',19,'Beijing'),
(8,'Yenuoyi',20,'Wuhan');
//distinct去重
SELECT DISTINCT adress FROM friend;
//where约束
SELECT age FROM friend WHERE age>18;
SELECT * FROM friend WHERE age>18;
SELECT * FROM friend WHERE age>18 AND adress='Wuhan';
SELECT * FROM friend WHERE age<18 OR adress='Beijing';
SELECT * FROM friend WHERE (age<20 AND NAME='Jack') OR adress='Tianjing';
//order by 排序
SELECT * FROM friend ORDER BY adress ASC;
SELECT * FROM friend ORDER BY age DESC;
//update修改
UPDATE friend SET adress='Chengdu' WHERE fid=4;
UPDATE friend SET adress='Sichuan' WHERE NAME='Wangmei';
UPDATE friend SET age=18 WHERE adress='Wuhan';
//delete删除行
DELETE FROM friend WHERE fid=8;
//truncate 清除数据
TRUNCATE TABLE student;
SELECT * FROM student;
DROP TABLE student;
SELECT * FROM student;
//like
SELECT * FROM friend;
SELECT * FROM friend WHERE NAME LIKE 'L%';
SELECT * FROM friend WHERE adress LIKE '%g';
SELECT * FROM friend WHERE adress NOT LIKE '%ng%';
//in
SELECT * FROM friend WHERE adress IN('Wuhan','Shanghei');
SELECT adress FROM friend WHERE adress IN('wuhan','shanghei');
//and
SELECT * FROM friend WHERE fid BETWEEN 1 AND 5;
SELECT * FROM friend ORDER BY adress ASC;
SELECT * FROM friend WHERE adress BETWEEN 'chengdu' AND 'tianjing';
SELECT * FROM friend WHERE adress BETWEEN(LIKE 'B%') AND (LIKE 'D%'); /*false*/
//as别名
SELECT * FROM friend AS partner;
SELECT * FROM friend parner;
SELECT * FROM friend parner WHERE partner.adress='Shanghei'; /*false*/
SELECT * FROM friend adress AS place; /*false*/
SELECT adress AS place FROM friend;
SELECT adress place FROM friend;
CREATE TABLE `rock_sql`.`colleague`( `sid` INT(10) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50),
`adress` VARCHAR(50),
`phone` INT(15),
`age` INT(10),
`major` VARCHAR(50),
PRIMARY KEY (`sid`)
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;
SHOW FULL TABLES FROM `rock_sql` WHERE table_type = 'BASE TABLE';
SHOW CHARSET;
SHOW TABLE STATUS FROM `rock_sql` LIKE 'colleague';
SHOW CHARSET;
SHOW FULL FIELDS FROM `rock_sql`.`colleague`;
SHOW KEYS FROM `rock_sql`.`colleague` ;
SHOW COLLATION;