mysqldump -uroot -p1234 my_db>D:\my_db.sql
第一种方式
在命令窗口(非数据库命令行)执行如下命令:
mysql -u用户名 -p密码 数据库<脚本文件路径
mysql -uroot -p1234 my_db
第二种方式,关键字:source
登录mysql,在mysq命令行执行如下命令:
source 脚本路径
source D:\my_db.sql
// 创建表并指定主键列
CREATE TABLE stuent(
sid INT PRIMARY KEY,
sname VARCHAR(20),
age INT
);
CREATE TABLE student(
sid INT,
sname VARCHAR(20),
age INT,
PRIMARY KEY(sid)
);
// 将已存在的表指定主键列
ALTER TABLE student ADD PRIMARY KEY(sid);
INSERT INTO stuent VALUES(100, 'ZhangSan',20);
指定sid列为主键列,添加主键自增长约束,关键字:AUTO_INCREMENT。
CREATE TABLE student(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(20),
age INT
);
插入数据:
INSERT INTO student VALUES(NULL, 'ZhangSan', 10);
执行DELETE指令后再次插入数据不会重新计数。
CREATE TABLE student(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(20) NOT NULL,
age INT
);
CREATE TABLE student(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(20) NOT NULL UNIQUE,
age INT
);
概念模型
// 创建表时添加外键约束
CREATE TABLE department(
deptno INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(50),
);
CREATE TABLE employee(
empno INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(50),
deptno INT,
CONSTRAINT fk_emp_dept FOREIGN KEY(deptno) REFERENCES department(deptno)
);
// 在已存在的表中添加外键约束
ALTER TABLE employee
ADD CONSTRAINT fk_emp_dept FOREIGN KEY(deptno) REFERENCES department(deptno);
CREATE TABLE hasband(
hid INT PRIMARY KEY AUTO_INCREMENT,
hname VARCHAR(50)
);
CREATE TABLE wife(
wid INT PRIMARY KEY AUTO_INCREMENT,
wname VARCHAR(50),
CONSTRAINT fk_wife_hasband FOREIGN KEY(wid) REFERENCES hasband(hid)
);
CREATE TABLE student(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(50)
);
CREATE TABLE teacher(
tid INT PRIMARY KEY AUTO_INCREMENT,
tname VARCHAR(50)
);
CREATE TABLE stu_tea(
sid INT,
tid INT,
CONSTRAINT fk_student FOREIGN KEY(sid) REFERENCES student(sid)
CONSTRAINT fk_teacher FOREIGN KEY(tid) REFERENCES teacher(tid)
);
关键字:UNION去除重复行;UNION ALL不去除重复行
注意:需要查询的结果集列数与列类型相同
CREATE TABLE department(
deptno INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(50),
);
CREATE TABLE employee(
empno INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(50),
deptno INT,
CONSTRAINT fk_emp_dept FOREIGN KEY(deptno) REFERENCES department(deptno)
);
// 报错
SELECT * FROM employee UNION ALL SELECT * FROM department;
// 报错
SELECT * FROM employee UNION SELECT * FROM department;
// 正确,不包含重复记录
SELECT deptno FROM employee UNION SELECT * FROM department;
// 正确,包含重复记录
SELECT deptno FROM employee UNION ALL SELECT * FROM department;
内连接查询出的所有记录都满足条件,关键字:INNER JOIN
// 如下查询会得到两个表记录数乘积的结果数。比如:emp有10条记录,dept有5条记录;查询结果会得到50条记录。这个结果叫“笛卡尔积”
SELECT * FROM employee, department;
// 笛卡尔积可以通过条件过滤无用的记录
SELECT * FROM employee, department WHERE employee.deptno=department.deptno;
SELECT * FROM employee e INNER JOIN department d ON e.deptno=d.deptno;
SELECT * FROM employee e NATURAL JOIN department d;
外连接分主次,主表中无论满不满足条件都打印,不满足的记录此表信息显示NULL。
SELECT * FROM employee e LEFT OUTER JOIN department d ON e.deptno=d.deptno;
SELECT * FROM employee e RIGHT OUTER JOIN department d ON e.deptno=d.deptno;
SELECT e.ename, e.sal, d.dname FROM employee e LEFT OUTER JOIN department d ON e.deptno=d.deptno
UNION
SELECT e.ename, e.sal, d.dname FROM employee e RIGHT OUTER JOIN department d ON e.deptno=d.deptno;
查询中包含查询(有多个SELECT)
// 查询雇员表中工资最高的员工
SELECT * FROM employee WHERE sal=(SELECT MAX(sal) FROM employee);
mysqldump source PRIMARY KEY AUTO_INCREMENT NOT NULL UNIQUE CONSTRAINT 名称 FOREIGN KEY(外键列名) REFERENCES 表名(列名) UNION UNION ALL INNER JOIN