建库建表:
CREATE DATABASE IF NOT EXISTS test2 DEFAULT CHARSET utf8;
USE test2;
CREATE TABLE IF NOT EXISTS websites2(id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(50) NOT NULL,
url VARCHAR(50) NOT NULL,
alexa INT NOT NULL COMMENT '网站排名',
country VARCHAR(50) NOT NULL
);
增:
INSERT INTO websites2(NAME,url,alexa,country)VALUES
("google",'http://www.google.com',1,'usa'),
("淘宝",'http://www.taobao.com',13,'usa'),
("菜鸟",'http://www.runoob.com',4689,'usa'),
("微博",'http://www.weibo.com',20,'usa'),
("Facebook",'http://www.facebook.com',3,'usa'),
("StackOverflow",'http://www.stackoverflow.com',0,'usa'),
("百度",'http://www.baidu.com',4,'usa');
删:
DELETE FROM websites2;
改:
UPDATE websites2 SET country="cn" WHERE id=2;
查:
SELECT * FROM websites2
关联查询:
SELECT st.*,sc.*,su.* FROM student st
INNER JOIN score sc ON st.no=sc.no
LEFT JOIN sub su ON sc.subno=su.subno
WHERE st.no > 1;
子查询:
SELECT e.emp_ name, e.salary, e.dept_ id, d. dept_ nameFROM (
SELECT emp_ name , dept_ id, salary FROM empWHERE dept_ id >= 2
AND salary > 5000
)e
LEFT JOIN dept d
ON e.dept_ id = d.id~ ;