MySQL从入门到精通(四)

一、建表语句

DROP TABLE IF EXISTS EMP;
DROP TABLE IF EXISTS DEPT;
DROP TABLE IF EXISTS SALGRADE;
 
CREATE TABLE DEPT
       (DEPTNO int(2) not null ,
	DNAME VARCHAR(14) ,
	LOC VARCHAR(13),
	primary key (DEPTNO)
	);
CREATE TABLE EMP
       (EMPNO int(4)  not null ,
	ENAME VARCHAR(10),
	JOB VARCHAR(9),
	MGR INT(4),
	HIREDATE DATE  DEFAULT NULL,
	SAL DOUBLE(7,2),
	COMM DOUBLE(7,2),
	primary key (EMPNO),
	DEPTNO INT(2) 
	)
	;
 
CREATE TABLE SALGRADE
      ( GRADE INT,
	LOSAL INT,
	HISAL INT );
 
 
 
 
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 
10, 'ACCOUNTING', 'NEW YORK'); 
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 
20, 'RESEARCH', 'DALLAS'); 
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 
30, 'SALES', 'CHICAGO'); 
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 
40, 'OPERATIONS', 'BOSTON'); 
commit;
 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7369, 'SMITH', 'CLERK', 7902,  '1980-12-17'
, 800, NULL, 20); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7499, 'ALLEN', 'SALESMAN', 7698,  '1981-02-20'
, 1600, 300, 30); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7521, 'WARD', 'SALESMAN', 7698,  '1981-02-22'
, 1250, 500, 30); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7566, 'JONES', 'MANAGER', 7839,  '1981-04-02'
, 2975, NULL, 20); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7654, 'MARTIN', 'SALESMAN', 7698,  '1981-09-28'
, 1250, 1400, 30); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7698, 'BLAKE', 'MANAGER', 7839,  '1981-05-01'
, 2850, NULL, 30); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7782, 'CLARK', 'MANAGER', 7839,  '1981-06-09'
, 2450, NULL, 10); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7788, 'SCOTT', 'ANALYST', 7566,  '1987-04-19'
, 3000, NULL, 20); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7839, 'KING', 'PRESIDENT', NULL,  '1981-11-17'
, 5000, NULL, 10); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7844, 'TURNER', 'SALESMAN', 7698,  '1981-09-08'
, 1500, 0, 30); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7876, 'ADAMS', 'CLERK', 7788,  '1987-05-23'
, 1100, NULL, 20); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7900, 'JAMES', 'CLERK', 7698,  '1981-12-03'
, 950, NULL, 30); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7902, 'FORD', 'ANALYST', 7566,  '1981-12-03'
, 3000, NULL, 20); 
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES ( 
7934, 'MILLER', 'CLERK', 7782,  '1982-01-23'
, 1300, NULL, 10); 
commit;
 
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES ( 
1, 700, 1200); 
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES ( 
2, 1201, 1400); 
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES ( 
3, 1401, 2000); 
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES ( 
4, 2001, 3000); 
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES ( 
5, 3001, 9999); 
commit;

二、表

2.1 定义

表:是数据库最基本的组成单元,数据库是用来存储数据的,数据库中有很多表,每一个表都是一个独立的单元,表也是一个结构化的文件,由行和列组成,行称为数据或记录,列称为字段,字段又包括:字段名称、字段类型、长度、约束。

2.2 创建表

2.2.1.语法格式:create table 表名称(字段名 类型(长度) 约束);

2.2.2.MySQL常用数据类型

数据类型 占用字节数 描述
char char(n) 定长字符串,存储空间大小固定使用char(2)来表示类型或状态
varchar varchar(n) 变长字符串,存储空间等于实际数据空间只包含英文字符的字符串
int 4个字节 表示整型,比如自增ID和表示数量
bigint 8个字节 表示长整型,比如自增ID
float float(有效数字位数,小数位) 数值型
double double(有效数字位数,小数位) 数值型
date 8字节 表示日期和时间
BLOB   二进制大对象
CLOB   字符大对象
其他..    

varchar与char对比:

a)都是字符串

b)varchar比较智能,可以根据实际的数据长度分配空间,比较节省空间,但在分配的时候需要相关判断

c)char不需要动态分配空间,所以执行效率高,但是可能会导致空间浪费

d)若字段中的数据不具备伸缩性,建议采用char类型存储

e)若字段中的数据具有很强的伸缩性,建议采用varchar类型存储

2.2.3 创建表格<student学生信息表>

字段包括:

学号:no INT(10)

姓名:name varchar(32)

性别:sex char(1)

出生日期:birth date

邮箱:email varchar(128)

建表语句:

create table t_student(
    no int(10),
    name varchar(32),
    sex char(1),
    birth date,
    email varchar(128)
)

2.2.4 删除表格

1)drop table t_student;

2)drop table if exists t_student;(推荐)

2.2.5 向t_student表格中插入数据

1)DML语句包括:insert、update、delete;

a)插入数据insert语法

insert into 表名(字段名1,字段名2,....) values(值1,值2);

注意:字段和数值必须一一对应,字段与数据个数必须相同,数据类型必须一致;

第一种方式:向表格中所有字段插入数据

insert into t_student(no,name,sex,birth,email) values(1,'zz','m','1970-01-01','zz');

第二种方式:向t_student表格中部分字段插入数据

insert into t_student(name,email) values('wangwu','[email protected]');

b)插入中文?

insert into t_student(no,name) values(5,'密码');

查看建表语句

show create table t_student;

错误原因:DOS窗口字符编码为GBK,数据库字符编码为UTF-8,可以使用数据库管理工具插入数据;

查看变量

show variables like '%char%'

设置结果编码集

set character_set_results = 'GBK'

2.2.6 创建表格时给字段设置默认值:default默认值

create table t_student(
    no int(10),
    name varchar(32),
    sex char(1) default 'm',
    birth date,
    email varchar(128)
)

2.2.7 表的复制

1.定义:将查询结果当作一张表创建

2.语法结构:create table 表名 as select 查询语句;

1)完全复制emp表到emp1

create table emp1 as select * from emp;

2)选择性复制emp表到emp2:

create table emp2 as select ename,sal from emp;

2.2.8 将查询结果插入到某张表中

1.语法结构:insert into 表名 select 查询语句

1)从emp2表中查询出员工工资为3000的,同时将查询结果插入到emp2表中

insert into emp2 select * from emp2 where sal = 3000

2)复制emp表为emp_bak,再将emp表中数据插入到emp_bak中

create table emp_bak as select * from emp;
insert into emp_bak select * from emp2 where sal = 3000;

2.3 增/删/改表结构

2.3.1 语法结构

1)新增:alter table 表名 add 字段名 字段类型(长度);

2)修改:alter table 表名 modify 字段名 字段类型(长度);

3)删除:alter table 表名 drop 字段名;

2.3.2 创建t_student表

字段

编号:no int(10)

姓名:name varchar(32)

drop table if exists t_student;
create table t_student(
    no int(10),
    name varchar(32)
);

2.3.3 给表添加一个电话字段

alter table t_student add tel varchar(10);

2.3.4 将字段tel长度扩展到20个长度

alter table t_student modify tel varchar(20);

2.3.5 将t_student表中的tel字段删除

alter table t_student drop tel;

2.4 添加/修改/删除 表数据

2.4.1 DML数据操作语句

insert、update、delete;

2.4.2 insert添加数据

语法格式:insert into 表名(字段名1,字段名2....) values(值1,值 2);

2.4.3 update修改数据

语法格式:update 表名 set 字段名 = 字段值,字段名 = 字段值 where 条件;

注意:update如果没有条件限制,将把整张表的数据全部更新;

1)向t_student表中插入数据

①向t_student表添加email varchar(128)

alter table t_student add email varchar(128);

②向t_student表插入测试数据

insert into t_student(no) values(1);
insert into t_student(no) values(2);
insert into t_student(no) values(3);

2)将编号no为3的记录name改为zhangsan,email改为[email protected]

update t_student set name = 'zhangsan',email = '[email protected]' where no = 3;

3)将所有名字name改为lisi

update t_student set name = 'lisi'

4)将emp_bak 表中的name包含"o"字母的改为wangwu

update emp_bak set name = 'wangwu' where name like '%o%' 

5)将emp_bak表中工作岗位为MANAGER和SALESMAN员工的工资上调10%

update emp_bak set sal = sal * 1.1 where job in ('MANAGER','SALESMAN');

2.4.4 delete删除数据

语法格式:delete from 表名 where 条件限制

注意:若没有条件限制,会将表中所有记录全部删除;

1)将t_student表中no为3的学生全部删除

delete from t_student where no = 3;

2)将emp_bak表中部门编号为20的MANAGER删除掉

delete from emp_bak where deptno = 20 and job = 'MANAGER';

2.5 创建表加入约束

英文单词:constraint

什么是约束?实际上是对表中数据的限制条件

设计表时加入约束的目的?保证表中数据的完整和有效

2.5.1 非空约束

作用:not null约束的字段不能为NULL值,必须赋具体数据;

示例:创建t_user表,name字段不能为空

drop table if exists t_user;
create table t_user(
    id int(10),
    name varchar(32) not null,
    email varchar(128)
)

2.5.2 唯一性约束(unique)

作用:unique约束的字段具有唯一性,不可重复

示例:创建t_user表,name不能为空,email保证唯一

1)方法一:列级约束

drop table if exists t_user;
create table t_user(
    id int(10),
    name varchar(32) not null,
    email varchar(128) unique
);

注意:unique约束的字段不能重复,但是可以为NULL,NULL不是一个值,也不能用等号比较;

2)方法二:表级约束

drop table if exists t_user;
create table t_user(
    id int(10),
    name varchar(32) not null,
    email varchar(128),
    unique(email)
);

3)使用表级约束给多个字段联合添加字段

drop table if exists t_user;
create table t_user(
    id int(10),
    name varchar(32) not null,
    email varchar(128),
    unique(name,email)
);

表级约束还可以给约束起名字,原因:以后可以通过名字操作这个约束

drop table if exists t_user;
create table t_user(
    id int(10),
    name varchar(32) not null,
    email varchar(128),
    constraint t_user_email_unique unique(email)
);

--使用information_schema
use information_schema;

--展示表
show tables;

--展示字段
desc table_constraints;

--查询出表中的唯一约束名称
select constraint_name from table_constraints where table_name = 't_user';
发布了98 篇原创文章 · 获赞 148 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_40310148/article/details/102989996