MySQL
前置知识
-
首先知道下载的mysql相当于是下载了一个服务端(这个提供了数据存储的一个服务) 客户端想要连接我们的Mysql,其实就是连接的3306这个端口 连接到Mysql服务(Mysql数据库)的指令: mysql -h 主机IP -P 端口 用户名 -p密码 注意: 1)-p密码不要有空格 2)如果-p后面没有密码,回车会要求输入密码 输入一下指令就会连接到数据库 用命令行登录数据库的方法如下: mysql -h localhost -P 3306 -u root -plk -
数据库的三层结构(数据库的解析) 1.所谓的安装Mysql数据库,其实就是在主机安装了一个数据库的管理系统(DBMS),这个管理程序可以管理多个数据库(Database manage system) 2.一个数据库中可以创建多个表,以保存数据 Mysql数据库(DBMS)->管理很多的数据库(DB1,DB2,DB3)->每个数据库里面又有很多的表(表1,表2,视图等等) -
因为Mysql是有端口的,在3306端口监听 -
mysqld是DBMS当中最重要的程序(是核心的监听的程序) -
我们下载的数据库,在数据库下载的data包当中 -
Mysql数据库-表 的本质仍然是文件(关键)
SQL语句的分类: DDL:数据定义语句【creat表,库…】 DML:数据操作语言【增加 insert,修改 update,删除 delete】 DQL:数据查询语句【select】 DCL:数据控制语言【管理数据库:比如用户的权限 grant revoke】
Mysql的基本数据类型
#Mysql常见的数据类型(列类型) #Mysql列类型 #数值类型:(整型:tingint[1个字节],smallint[2个字节], #mediumint[3个字节],int[4个字节],bigint[8个字节]) #(小数类型:float[单精度 四个字节],double[双精度 8个字节], #decimal[M,D] 大小不确定的) M指定长度,D表示小数点的位数 #文本类型:(char 0-255,varchar 0-65535[0-2^16-1],text 0-2^16-1, #longtext 0-2^32-1) #二进制数据类型:blob[0-2^16-1] longblob[0-2^32-1] #日期的数据类型(日期类型):(date[日期类型 年月日],time[时间 时分秒] #datetime[年月日 时分秒 YYYY-MM-DD HH:mm:ss] timestamp[时间戳]
1.1bit类型的使用
CREATE TABLE t3(
id TINYINT
);
INSERT INTO t3 VALUES(-128);
CREATE TABLE t4(
id TINYINT UNSIGNED);
SELECT * FROM t3;
SELECT * FROM t4;
1.2整型的使用
CREATE TABLE t3(
id TINYINT
);
INSERT INTO t3 VALUES(-128);
CREATE TABLE t4(
id TINYINT UNSIGNED);
SELECT * FROM t3;
SELECT * FROM t4;
1.3浮点数类型
CREATE TABLE t06(
num1 FLOAT,
num2 DOUBLE,
num3 DECIMAL(30,20)
);
INSERT INTO t06 VALUES(88.12345678912345,88.12345678912345,88.12345678912345);
SELECT * FROM t06;
CREATE TABLE t07(
num DECIMAL(65)
)
INSERT INTO t07 VALUES(999999999999999999999999);
SELECT * FROM t07;
1.4字符型
CREATE TABLE t09(
`name` CHAR(255));
CREATE TABLE t10(
`name` VARCHAR(21844)
);
CREATE TABLE t11(
`name` VARCHAR(32766)) CHARSET gbk;
DROP TABLE t11;
CREATE TABLE t12(
`name` CHAR(4));
INSERT INTO t12 VALUES('李康你好');
SELECT * FROM t12;
INSERT INTO t12 VALUES('ab北京');
CREATE TABLE t13(content TEXT,content2 MEDIUMTEXT,content3 LONGTEXT);
INSERT INTO t13 VALUES('李康加油','李康加油不要放弃保持更新学习','李康坚持住,坚持住了就会有不一样的收货');
SELECT * FROM t13;
1.5日期的相关类型的使用
CREATE TABLE t14(
birthday DATE,
job_time DATETIME,
login_time TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
SELECT * FROM t14;
INSERT INTO t14(birthday,job_time)
VALUES('2022-11-11','2022-11-11 10:10:10');
1.6文本类型
CREATE TABLE emp(
id INT,
name1 VARCHAR(32),
sex CHAR(1),
birthday DATE,
entry_date DATETIME,
job VARCHAR(32),
salary DOUBLE,
RESUME TEXT) CHARSET utf8 COLLATE utf8_bin ENGINE INNODB;
INSERT INTO emp VALUES(100,'小妖怪','男','2002-03-28',
'2020-09-01 11-11-11','巡山',3000,'大王叫我来巡山');
SELECT * FROM emp;
DROP TABLE emp
Mysql的指令操作
2.1对包的操作
CREATE DATABASE lk_db01
DROP DATABASE lk_dbo1
CREATE DATABASE lk_db02 CHARACTER SET utf8
CREATE DATABASE lk_db03 CHARACTER SET utf8 COLLATE utf8_bin
SELECT * FROM t1 WHERE NAME = 'tom'
SHOW DATABASES
SHOW CREATE DATABASE lk_db01
CREATE DATABASE `CREATE`
DROP DATABASE `CREATE`
mysqldump -u root -p -B lk_db01 lk_db02 > D:\\lkmysql\\back\\bsk.sql
DROP DATABASE lk_db02;
SELECT * FROM users
INSERT INTO users VALUES(4,'马超','天津');
2.2对表的Mysql指令和操作
ALTER TABLE emp
ADD image VARCHAR(255) NOT NULL DEFAULT ''
AFTER RESUME
DESC emp
ALTER TABLE emp
MODIFY job VARCHAR(60) NOT NULL DEFAULT ''
ALTER TABLE emp
DROP sex
RENAME TABLE emp TO employee
ALTER TABLE employee CHARACTER SET utf8
ALTER TABLE employee
CHANGE name1 `username` VARCHAR(64) NOT NULL DEFAULT ''
DESC employee
CREATE TABLE `user`(
id INT,
`name` VARCHAR(255),
`password` VARCHAR(255),
`birthday` DATE)
CHARACTER SET utf8 COLLATE utf8_bin ENGINE INNODB;
表的数据的增删改查
CREATE TABLE `goods`(
id INT,
goods_name VARCHAR(10),
price DOUBLE NOT NULL DEFAULT 100);
INSERT INTO `goods` (id,goods_name,price)
VALUES(10,'华为手机',3000)
INSERT INTO `goods` (id ,goods_name,price)
VALUES(11,'苹果手机',4000)
INSERT INTO `goods` (id ,goods_name,price)
VALUES('30','小米手机',2000)
DESC employee
INSERT INTO employee(id,username,sex,birthday,entry_date,job,salary,`resume`)
VALUES(2,'林英','女','2001-11-11','2020-03-08 11-12-45','学生','-1200','魅力')
SELECT * FROM employee
INSERT INTO `goods` (id,goods_name,price)
VALUES(40,'vivo手机',NULL)
INSERT INTO `goods`(id,goods_name,price)
VALUES(50,'三星手机',6000),(60,'oppo手机',4500)
SELECT * FROM `goods`
INSERT INTO goods
VALUES(80,'锤子手机',3500)
INSERT INTO goods(id,goods_name)
VALUES(90,'格力手机');
CREATE TABLE `goods2`(
id INT,
goods_name VARCHAR(10),
price DOUBLE NOT NULL DEFAULT 100);
INSERT INTO goods2(id,goods_name)
VALUES(100,'蓝莓手机');
SELECT * FROM goods2
DESC employee
SELECT * FROM employee
UPDATE employee SET salary = 5000
UPDATE employee
SET salary = 3000
WHERE username = '小妖怪'
INSERT INTO employee
VALUE(5,'老妖怪','男','1990-11-11','2000-11-11 10:10:10','捶背的',5000,'给大王捶背的')
UPDATE employee
SET salary = salary + 1000,job = '出主意的'
WHERE username = '老妖怪'
DELETE FROM employee
WHERE username = '老妖怪';
DELETE FROM employee
SELECT * FROM employee;
DROP TABLE employee
CREATE TABLE student(
id INT NOT NULL DEFAULT 1,
`name` VARCHAR(20) NOT NULL DEFAULT '',
chinese FLOAT NOT NULL DEFAULT 0.0,
english FLOAT NOT NULL DEFAULT 0.0,
math FLOAT NOT NULL DEFAULT 0.0
);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(1,'lk',89,99,100);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(2,'张飞',69,66,78);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(3,'宋江',100,100,100);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(4,'关羽',89,99,100);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(5,'赵云',89,99,100);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(6,'刘邦',89,99,100);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(7,'黄荣',75,88,65);
INSERT INTO student(id,`name`,chinese,english,math) VALUES(8,'刘备',88,88,99);
SELECT * FROM student;
SELECT `name`,english FROM student;
SELECT english FROM student;
SELECT DISTINCT english FROM student;
SELECT DISTINCT `name`,english FROM student;
SELECT `name`,(chinese+english+math) FROM student;
SELECT `name`,(chinese+english+math+10) AS total_score FROM student;
SELECT `name` AS '名字',(chinese + english + math + 10) AS total_score FROM student;
SELECT * FROM student
WHERE `name` = '赵云'
SELECT * FROM student
WHERE english > 90
SELECT * FROM student
WHERE (chinese+english+math)>270
SELECT * FROM student
WHERE (math>60 AND id>4)
SELECT * FROM student
WHERE english > chinese
SELECT * FROM student
WHERE(chinese+english+math) > 200 AND
math > chinese AND `name` LIKE '赵%'
SELECT * FROM student
WHERE english>=80 AND english<90;
SELECT * FROM student
WHERE english>=80 AND english<=90
SELECT * FROM student
WHERE math=89 OR math=90 OR math=91;
SELECT * FROM student
WHERE math IN (89,90,91)
SELECT * FROM student
WHERE `name` LIKE 'L%'
SELECT * FROM student
WHERE math > 80 AND chinese > 80;
SELECT * FROM student
WHERE chinese>=70 AND chinese<=80
SELECT * FROM student
WHERE (chinese+english+math) IN (189,190,191);
SELECT * FROM student
WHERE `name` LIKE '张%' OR `name` LIKE '宋%'
SELECT * FROM student
WHERE (math-chinese)>=30
DESC student
SELECT * FROM student
SELECT * FROM student
SELECT * FROM student
ORDER BY math DESC
SELECT `name`,(chinese+english+math) AS total_score FROM student
ORDER BY total_score DESC
SELECT `name`,(chinese+english+math) AS total_score FROM student
ORDER BY total_score
SELECT `name`,(chinese + english +math) AS total_score FROM student
WHERE `name` LIKE '刘%'
ORDER BY total_score DESC
|