CREATE DATABASE mydb1;
CREATE DATABASE mydb2 CHARACTER SET gbk;
SHOW DATABASES;
DROP DATABASE mydb2;
ALTER DATABASE mydb1 CHARACTER SET utf8;
SELECT database();
USE mydb1;
CREATE TABLE student (
name VARCHAR(10),
age INT,
score DOUBLE
);
SHOW TABLES;
DESC student;
ALTER TABLE student ADD num INT;
ALTER TABLE student MODIFY num DOUBLE;
ALTER TABLE student DROP num;
RENAME TABLE student TO stu;
SHOW CREATE TABLE stu;
ALTER TABLE stu CHARACTER SET gbk;
ALTER TABLE stu CHANGE name username VARCHAR(10);
DROP TABLE stu;
DESC stu;
SELECT *FROM stu;
INSERT INTO stu (username, age, score, birthday) VALUES (‘tom’, 11, 55.5, ‘2000-03-22’);
INSERT INTO stu VALUES
(‘sokeaa1’, 12, NULL , ‘1990-03-30’),
(‘bikebbc1’, 33, 34.4, ‘2001-03-03’),
(‘jarycccc1’, 22, 33.4, ‘1994-04-06’);
UPDATE stu SET age = 50;
UPDATE stu SET score = 100 WHERE username = ‘tom1’;
UPDATE stu SET score = 99.9, age = 11 WHERE username = ‘tom2’;
UPDATE stu SET score = score + 10 WHERE username = ‘tom3’;
SELECT *FROM stu;
DELETE FROM stu;
DELETE FROM stu WHERE age = 10;
TRUNCATE TABLE stu;
SELECT *FROM stu;
SELECT username,age FROM stu;
SELECT *FROM stu WHERE age = 50 AND username = ‘tom1’;
SELECT *FROM stu WHERE score = 55.5 OR username = ‘tom1’;
SELECT *FROM stu WHERE score IN (55.5, 99.9, 100);
SELECT *FROM stu WHERE score NOT IN (55.5, 99.9, 100);
SELECT *FROM stu WHERE birthday IS NULL;
SELECT *FROM stu WHERE score BETWEEN 55.5 AND 100;
SELECT *FROM stu WHERE score >=55.5 AND score<=100;
SELECT *FROM stu WHERE age != 50;
SELECT *FROM stu WHERE NOT age = 50;
SELECT *FROM stu WHERE age <> 50;
SELECT *FROM stu WHERE birthday IS NOT NULL;
SELECT *FROM stu WHERE username LIKE ‘__‘;
SELECT *FROM stu WHERE username LIKE ‘___1’;
SELECT *FROM stu WHERE username LIKE ‘t___’ ;
SELECT *FROM stu WHERE username LIKE ‘o_‘;
SELECT *FROM stu WHERE username LIKE ‘%o%’;
SELECT DISTINCT age FROM stu;
SELECT *,age+score FROM stu;
SELECT *,age+ifnull(score, 0) FROM stu;
SELECT *,age+ifnull(score,0) AS total FROM stu;
SELECT *,age+ifnull(score,0)total FROM stu;
SELECT *FROM stu ORDER BY age ASC ;
SELECT *FROM stu ORDER BY age;
SELECT *FROM stu ORDER BY age DESC ;
SELECT *FROM stu ORDER BY age DESC, score ASC ;
SELECT *FROM stu ORDER BY age DESC, score;
SELECT count(*) AS cnt FROM stu;
SELECT count(score)AS cnt FROM stu;
SELECT count(*) AS cnt FROM stu WHERE score > 90;
SELECT count(*)AS cnt FROM stu WHERE ifnull(score,0) + ifnull(age,0)>120;
SELECT count(score), count(birthday) FROM stu ;
SELECT sum(age)FROM stu;
SELECT sum(age),sum(score)FROM stu;
SELECT sum(age+ifnull(score,0))FROM stu ;
SELECT avg(age)FROM stu;
SELECT max(score),min(score)FROM stu;
SELECT age,sum(score)FROM stu GROUP BY age;
SELECT age,count(*)FROM stu GROUP BY age;
SELECT age,count(*) FROM stu WHERE score>56 GROUP BY age;
SELECT age,sum(score)FROM stu GROUP BY age HAVING sum(score) > 100;
SELECT *FROM stu LIMIT 0,5;
SELECT *FROM stu LIMIT 2,5;
CREATE TABLE person(
id INT PRIMARY KEY ,
name VARCHAR(50)
);
CREATE TABLE person(
id INT ,
name VARCHAR(50),
PRIMARY KEY (id)
);
CREATE TABLE person(
id INT ,
name VARCHAR(50)
);
ALTER TABLE person ADD PRIMARY KEY (id);
CREATE TABLE person1(
id INT ,
name VARCHAR(50)UNIQUE
);
INSERT INTO person (id, name) VALUES (1,’tom1’);
CREATE TABLE teacher(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)
);
INSERT INTO teacher (name) VALUES (‘tom’);
SELECT *FROM teacher;
CREATE TABLE teacher1(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)NOT NULL
);
INSERT INTO teacher1 (name) VALUES (NULL );
CREATE TABLE teacher11(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)NOT NULL ,
sex VARCHAR(10)DEFAULT ‘man’
);
INSERT INTO teacher11 (name) VALUES (‘tom’);
INSERT INTO teacher11 (name, sex) VALUES (‘tom’,’woman’);
INSERT INTO teacher11 (name, sex) VALUES (‘tom’,DEFAULT );
SELECT *FROM teacher11;
“`
SHOW DATABASES ;
DROP DATABASE mydb;
CREATE DATABASE mydb;
SELECT database();
SHOW DATABASES ;
USE mydb;
CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT
) ;
INSERT INTO emp VALUES(7369,’SMITH’,’CLERK’,7902,’1980-12-17’,800,NULL,20);
INSERT INTO emp VALUES(7499,’ALLEN’,’SALESMAN’,7698,’1981-02-20’,1600,300,30);
INSERT INTO emp VALUES(7521,’WARD’,’SALESMAN’,7698,’1981-02-22’,1250,500,30);
INSERT INTO emp VALUES(7566,’JONES’,’MANAGER’,7839,’1981-04-02’,2975,NULL,20);
INSERT INTO emp VALUES(7654,’MARTIN’,’SALESMAN’,7698,’1981-09-28’,1250,1400,30);
INSERT INTO emp VALUES(7698,’BLAKE’,’MANAGER’,7839,’1981-05-01’,2850,NULL,30);
INSERT INTO emp VALUES(7782,’CLARK’,’MANAGER’,7839,’1981-06-09’,2450,NULL,10);
INSERT INTO emp VALUES(7788,’SCOTT’,’ANALYST’,7566,’1987-04-19’,3000,NULL,20);
INSERT INTO emp VALUES(7839,’KING’,’PRESIDENT’,NULL,’1981-11-17’,5000,NULL,10);
INSERT INTO emp VALUES(7844,’TURNER’,’SALESMAN’,7698,’1981-09-08’,1500,0,30);
INSERT INTO emp VALUES(7876,’ADAMS’,’CLERK’,7788,’1987-05-23’,1100,NULL,20);
INSERT INTO emp VALUES(7900,’JAMES’,’CLERK’,7698,’1981-12-03’,950,NULL,30);
INSERT INTO emp VALUES(7902,’FORD’,’ANALYST’,7566,’1981-12-03’,3000,NULL,20);
INSERT INTO emp VALUES(7934,’MILLER’,’CLERK’,7782,’1982-01-23’,1300,NULL,10);
INSERT INTO emp VALUES(7902,’MILLE’,’CLERK’,7782,’1982-01-23’,1200,NULL,50);
CREATE TABLE dept(
deptno INT,
dname VARCHAR(14),
loc VARCHAR(13)
);
INSERT INTO dept VALUES(10, ‘ACCOUNTING’, ‘NEW YORK’);
INSERT INTO dept VALUES(20, ‘RESEARCH’, ‘DALLAS’);
INSERT INTO dept VALUES(30, ‘SALES’, ‘CHICAGO’);
INSERT INTO dept VALUES(40, ‘OPERATIONS’, ‘BOSTON’);
SELECT * FROM emp;
SELECT * FROM dept;
SELECT *FROM emp UNION SELECT *FROM dept;
SELECT *FROM emp UNION ALL SELECT *FROM dept;
SELECT *FROM emp,dept;
SELECT *FROM emp,dept WHERE emp.deptno = dept.deptno;
SELECT *FROM emp INNER JOIN dept ON emp.deptno=dept.deptno;
SELECT *FROM emp JOIN dept ON emp.deptno=dept.deptno;
SELECT *FROM emp e JOIN dept d ON e.deptno=d.deptno;
SELECT *FROM emp e LEFT JOIN dept d ON e.deptno = d.deptno;
SELECT *FROM emp e RIGHT JOIN dept d ON e.deptno = d.deptno;
SELECT *FROM emp NATURAL JOIN dept;
SELECT *FROM emp NATURAL LEFT JOIN dept;
SELECT *FROM emp NATURAL RIGHT JOIN dept;
SELECT *FROM emp;
SELECT sal FROM emp WHERE ename=’JONES’;
SELECT *FROM emp WHERE sal>2957;
SELECT *FROM emp WHERE sal>(SELECT sal FROM emp WHERE ename=’JONES’);
SELECT *FROM emp WHERE job =(SELECT job FROM emp WHERE ename=’SCOTT’);
SELECT *FROM emp WHERE sal>(SELECT max(sal) FROM emp WHERE deptno=’30’);
SELECT *FROM emp WHERE (job,sal)IN (SELECT job,sal FROM emp WHERE ename=’MARTIN’);
SELECT *FROM emp WHERE empno IN (SELECT mgr FROM emp GROUP BY mgr HAVING count(mgr)>1);
SELECT e.empno,e.ename,e.mgr, e1.ename FROM emp e ,emp e1 WHERE e.mgr=e1.empno AND e.empno=’7369’;
CREATE TABLE stu(
sid INT PRIMARY KEY ,
name VARCHAR(50),
sex VARCHAR(10) DEFAULT ‘男’
);
CREATE TABLE score(
id INT,
score DOUBLE,
sid INT
);
CREATE TABLE teacher(
tid INT PRIMARY KEY ,
name VARCHAR(50)
);
CREATE TABLE student(
sid INT PRIMARY KEY ,
name VARCHAR(50)
);
CREATE TABLE tea_stu_rel_(
tid INT,
sid INT
);
CREATE TABLE stu1(
sid INT PRIMARY KEY ,
name VARCHAR(50),
sex VARCHAR(10) DEFAULT ‘男’
);
CREATE TABLE score1(
id INT ,
score DOUBLE,
sid INT PRIMARY KEY
);
CREATE TABLE stu(
sid INT PRIMARY KEY ,
name VARCHAR(50),
sex VARCHAR(10) DEFAULT ‘男’
);
CREATE TABLE score(
id INT,
score DOUBLE,
sid INT, – 外键类型一定要与关联的主键类型一致
#方式1
CONSTRAINT fk_score_sid FOREIGN KEY (sid) REFERENCES stu(sid)
);
ALTER TABLE score ADD CONSTRAINT fk_score_sid FOREIGN KEY (sid) REFERENCES stu(sid) ;