INSERT INTO stu(id,NAME,age) VALUES(1,'知识',18);
SELECT * FROM stu;
INSERT INTO stu VALUES(2,'地点',22,98.3,NULL,NULL);
INSERT INTO stu VALUES(3,'语句',20,99.5,"98-7-9",NULL);
DELETE FROM stu WHERE id=1;
UPDATE stu SET age=18 WHERE id=3;
UPDATE stu SET age=20,score=99 WHERE id=2;
CREATE TABLE student(
id INT,
NAME VARCHAR(20),
age INT,
sex VARBINARY(5),
address VARBINARY(100),
math INT,
english INT
);
INSERT INTO student(id,NAME,age,sex,address,math,english) VALUES
(1,'马云',55,'男','杭州',66,78),
(2,'马化腾',45,'女','深圳',98,87),
(3,'马景涛',55,'男','香港',66,77),
(4,'柳岩',20,'女','湖南',99,99),
(5,'刘德华',35,'男','香港',95,95),
(6,'柳青',25,'男','湖南',65,NULL),
(7,'马丹丹',34,'女','香港',60,72),
(8,'德玛西亚',55,'男','南京',59,67);
SELECT * FROM student;
TRUNCATE TABLE student;
SELECT
NAME,
age
FROM
student;
SELECT address FROM student;
SELECT DISTINCT address FROM student;
SELECT DISTINCT NAME,address FROM student;
SELECT NAME,math,english,math+english FROM student;
SELECT NAME,math,english,math+IFNULL(english,0) FROM student;
SELECT NAME,math,english,math+IFNULL(english,0) AS 总分 FROM student;
SELECT NAME,math 数学,english 英语,math+IFNULL(english,0) 总分 FROM student;
SELECT * FROM student WHERE age > 20;
SELECT * FROM student WHERE age >= 20;
SELECT * FROM student WHERE age = 20;
SELECT * FROM student WHERE age != 20;
SELECT * FROM student WHERE age <> 20;
SELECT * FROM student WHERE age>=20 AND age<=30;
SELECT * FROM student WHERE age BETWEEN 20 AND 30;
SELECT * FROM student WHERE age=22 OR age=19 OR age=25;
SELECT * FROM student WHERE age IN(22,19,25);
SELECT * FROM student WHERE english = NULL;
SELECT * FROM student WHERE english IS NULL;
SELECT * FROM student WHERE english IS NOT NULL;
SELECT * FROM student WHERE NAME LIKE '马%';
SELECT * FROM student WHERE NAME LIKE '_化%';
SELECT * FROM student WHERE NAME LIKE '___';
SELECT * FROM student WHERE NAME LIKE '%德%';
SELECT * FROM student ORDER BY math;
SELECT * FROM student ORDER BY math ASC;
SELECT * FROM student ORDER BY math DESC;
SELECT * FROM student ORDER BY math ASC , english ASC;
SELECT COUNT(NAME) FROM student;
SELECT COUNT(english) FROM student;
SELECT COUNT(IFNULL(english,0)) FROM student;
SELECT COUNT(*) FROM student;
SELECT MAX(math) FROM student;
SELECT MIN(math) FROM student;
SELECT SUM(math) FROM student;
SELECT AVG(math) FROM student;
SELECT sex,AVG(math) FROM student GROUP BY sex;
SELECT sex,AVG(math),COUNT(id) FROM student GROUP BY sex;
SELECT sex,AVG(math),COUNT(id) FROM student WHERE math>70 GROUP BY sex;
SELECT sex,AVG(math),COUNT(id) FROM student WHERE math>70 GROUP BY sex HAVING COUNT(id)>1;
SELECT sex,AVG(math),COUNT(id) 人数 FROM student WHERE math>70 GROUP BY sex HAVING 人数>1;
SELECT * FROM student LIMIT 0,3;
SELECT * FROM student LIMIT 3,3;
SELECT * FROM student LIMIT 6,3;
CREATE TABLE stu1(
id INT,
NAME VARCHAR(20) NOT NULL
);
SELECT * FROM stu1;
ALTER TABLE stu1 MODIFY NAME VARCHAR(20);
ALTER TABLE stu1 MODIFY NAME VARCHAR(20) NOT NULL;
CREATE TABLE stu2(
id INT,
phone_number VARCHAR(20) UNIQUE
);
SELECT * FROM stu2;
ALTER TABLE stu2 DROP INDEX phone_number;
ALTER TABLE stu2 MODIFY phone_number VARCHAR(20) UNIQUE;
CREATE TABLE stu3(
id INT PRIMARY KEY,
NAME VARCHAR(20)
);
SELECT * FROM stu3;
ALTER TABLE stu3 DROP PRIMARY KEY;
ALTER TABLE stu3 MODIFY id INT PRIMARY KEY;
CREATE TABLE stu4(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
SELECT * FROM stu4;
INSERT INTO stu4 VALUES(NULL,'ccc');
INSERT INTO stu4 VALUES(10,'hh');
INSERT INTO stu4 VALUES(NULL,'tt');
ALTER TABLE stu4 MODIFY id INT;
ALTER TABLE stu4 MODIFY id INT AUTO_INCREMENT;
DROP TABLE stu1,stu2,stu3,stu4;
CREATE TABLE emp(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30),
age INT,
dep_name VARCHAR(30),
dep_location VARCHAR(30)
);
INSERT INTO emp(NAME,age,dep_name,dep_location) VALUES ('张三',20,'研发部','广州');
INSERT INTO emp(NAME,age,dep_name,dep_location) VALUES ('李四',21,'研发部','广州');
INSERT INTO emp(NAME,age,dep_name,dep_location) VALUES ('王五',20,'研发部','广州');
INSERT INTO emp(NAME,age,dep_name,dep_location) VALUES ('老王',20,'销售部','深圳');
INSERT INTO emp(NAME,age,dep_name,dep_location) VALUES ('大王',22,'销售部','深圳');
INSERT INTO emp(NAME,age,dep_name,dep_location) VALUES ('小王',18,'销售部','深圳');
SELECT * FROM emp;
DROP TABLE emp;
CREATE TABLE department(
id INT PRIMARY KEY AUTO_INCREMENT,
dep_name VARCHAR(30),
dep_location VARCHAR(30)
);
CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30),
age INT,
dep_id INT,
CONSTRAINT emp_dept_id FOREIGN KEY (dep_id) REFERENCES department(id)
);
INSERT INTO department VALUES(NULL,'研发部','广州'),(NULL,'销售部','深圳');
INSERT INTO employee (NAME,age,dep_id) VALUES ('张三',20,1);
INSERT INTO employee (NAME,age,dep_id) VALUES ('李四',21,1);
INSERT INTO employee (NAME,age,dep_id) VALUES ('王五',20,1);
INSERT INTO employee (NAME,age,dep_id) VALUES ('老王',20,2);
INSERT INTO employee (NAME,age,dep_id) VALUES ('大王',22,2);
INSERT INTO employee (NAME,age,dep_id) VALUES ('小王',18,2);
SELECT * FROM employee;
SELECT * FROM department;
DROP TABLE department,employee;
ALTER TABLE employee DROP FOREIGN KEY emp_dept_id;
ALTER TABLE employee ADD CONSTRAINT emp_dept_id FOREIGN KEY (dep_id) REFERENCES department(id) ON UPDATE CASCADE ON DELETE CASCADE;
UPDATE employee SET dep_id = NULL WHERE dep_id = 1;
UPDATE employee SET dep_id = 5 WHERE dep_id IS NULL;