300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > datetime类型怎么输入_精心整理MySQL基本使用(数据库的操作 数据类型 MySQL的常用命令)...

datetime类型怎么输入_精心整理MySQL基本使用(数据库的操作 数据类型 MySQL的常用命令)...

时间:2021-03-02 11:56:53

相关推荐

datetime类型怎么输入_精心整理MySQL基本使用(数据库的操作 数据类型 MySQL的常用命令)...

前言:瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。

MySQL是一种关联数据库管理系统 由于其体积小、速度快、总体拥有成本低。MySQL软件采用了双授权政策(本词条"授权政策"),它分为社区版和商业版LDAP。

一、MySQL 产品下载

版本细节:linux(32 64) win(32 64)

请到如下网站:/

官方文档上有关 MySQL 安装文件类型说明:

Linux supports anumber of different solutions for installing MySQL. The recommended method isto use one of the distributions from Oracle. If you choose this method, thereare three options available: 产品下载。

(1)Installingfrom a generic binary package in .tar.gz format. See Section 2.2,”Installing MySQL from Generic Binaries on Unix/Linux” for moreinformation.

(2)Extractingand compiling MySQL from a source distribution. For detailed instructions,see Section 2.9, “InstallingMySQL from Source”.

(3)Installingusing a pre-compiled RPM package. For more information on using the RPMsolution, see Section 2.5.1,”Installing MySQL from RPM Packages on Linux”.

(1)DML

DML:数据操作语言 增 (INSERT)、删 (DELETE)、查 (SELECT)、改 (UPDATE)。

(2)DLL

DDL:数据定义语言 创建 (CREATE)、删除 (DROP)、修改 (ALTER)。

(3)DCL

DCL:数据控制语言

GRANT

REVOKE

是用来设置或更改数据库用户或角色权限的语句。

(4)数据库的操作

连接 MYSQL 服务器:mysql -uroot -proot

mysql -uroot -p --default_character_set=gbk; (影响数据的输入和输出)

show variables like 'character%';

数据库的操作:创建,查看,修改,删除

1、创建:

创建一个名称为mydb1的数据库:create database mydb1;

创建一个使用utf-8字符集的mydb2数据库:create database mydb2 character set utf8;

创建一个使用utf-8字符集,并带校对规则的mydb3数据库:create database mydb3 character set utf8 collate utf8_general_ci;

2、查看:

显示所有数据库:show databases;

显示创建数据库的语句信息:show create database mydb2;

3、修改:

修改mydb1的字符集为gbk(不能修改数据库名):alter database mydb1 character set utf8;

4、删除:

删除数据库mydb2:drop database mydb1;

表的操作

表的操作:创建,查看,修改,删除

*创建:

根据实体类Person创建表person

Person {int id;String name;}create table person(id int,name varchar(20));

数据类型

mysql 中的数据类型:

bit 1 位但可以指定位数,如:bit<3>。int 2 字节 可以指定最大位数,如:int<4>最大为 4 位的整数。float 2 个字节可以指定最大的位数和最大的小数位数,如:float<5,2> 最大为一个 5 位的数,小数位最多 2 位。double4 个字节可以指定最大的位数和最大的小数位数,如:float<6,4> 最大为一个 6 位的数,小数位最多 4 位。char必须指定字符数,如 char (5) 为不可变字符即使存储的内容为’ab’, 也是用 5 个字符的空间存储这个数据。varchar必须指定字符数,如 varchar (5) 为可变字符如果存储的内容为’ab’, 占用 2 个字符的空间;如果为’abc’, 则占用 3 个字符的空间。text: 大文本 (大字符串)。blob:二进制大数据如图片,音频文件,视频文件。date: 日期如:’1921-01-02’datetime: 日期时间如:’1921-01-02 12:23:43’timeStamp: 时间戳,自动赋值为当前日期时间

表的基本操作

创建一个员工表

create table employee(id int,name varchar(20),sex bit,birthday date,salary double,entry_date date,resume text);

* 查看

查看所有的表:show tables;

查看指定表的创建语句:show create table employee;

mysql表 名称区分大小写

显示指定表的结构:desc employee;

* 删除

删除employee表:drop table employee;

* 修改表

增加一个字段:alter table worker add column height double;

修改一个字段:alter table worker modify column height float;

删除一个字段:alter table worker drop column height;

更改表名:rename table employee to worker;

修改表的字符集:alter table worker character set gbk;

表数据的 CRUD

*C(create增加数据) Insert语句

新建Employee表并表中添加一些记录

create table employee(id int,name varchar(20),sex bit,birthday date,salary double,entry_date date,resume text);

create table employee(id int, name varchar(20),sex bit,birthday date,salary double,entry_date date,resume text);

插入数据

insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(1,'张三',1,'1983-09-21',15000,'-06-24','一个大牛');insert into employee(id,name,sex,birthday,salary,entry_date,resume)values(2,'李四',1,'1984-09-21',10000,'-07-24','一个中牛');insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(3,'王五',0,'1985-09-21',7000,'-08-24','一个小牛');

delete from employee where id=1

create table employee( id int,name varchar(20),sex bit,birthday date,salary double,entry_date date,resume text);

修改数据

*U (update 更新数据) Update 语句

将所有员工薪水都增加500元。

update employee set salary=salary+500;

将王五的员工薪水修改为10000元,resume改为也是一个中牛

update employee set salary=10000,resume='也是一个中牛' where name='王五';

*删除数据

*D(drop删除数据) Delete语句

删除表中姓名为王五的记录。

delete from employee where name='王五';

删除表中所有记录。

delete from employee; --可以有条件,但删除所有记录差了一点

使用truncate删除表中记录。

truncate employee;--无条件 效率高

*R(Retrieve查找数据) Select语句

准备环境:

create table student(id int,name varchar(20),chinese int,english int,math int);insert into student(id,name,chinese,english,math) values(1,'何东',80,85,90);insert into student(id,name,chinese,english,math) values(2,'权筝',90,95,95);insert into student(id,name,chinese,english,math) values(3,'何南',80,96,96);insert into student(id,name,chinese,english,math) values(4,'叶坦',81,97,85);insert into student(id,name,chinese,english,math) values(5,'何西',85,84,90);insert into student(id,name,chinese,english,math) values(6,'丁香',92,85,87);insert into student(id,name,chinese,english,math) values(7,'何北',75,81,80);insert into student(id,name,chinese,english,math) values(8,'唐娇',77,80,79);insert into student(id,name,chinese,english,math) values(9,'任知了',95,85,85);insert into student(id,name,chinese,english,math) values(10,'王越',94,85,84);

查询表中所有学生的信息:select * from student;

查询表中所有学生的姓名和对应的英语成绩:select name,english from student;

内置函数练习

重复

过滤表中重复数据。

select english from student;select DISTINCT english from student;select DISTINCT english,name from student;select english+chinese+math from student;select english+chinese+math as 总分 from student;select name,english+chinese+math as 总分 from student;

在所有学生英语分数上加10分特长分:select name,english+10 from student;

统计每个学生的总分:select english+chinese+math from student;

别名(使用别名表示学生分数)

select name,english+chinese+math as 总分 from student;

select name,english+chinese+math 总分 from student;

查询姓名为何东的学生成绩:select * from student where name='何东';

查询英语成绩大于90分的同学:select * from student where english>90;

查询总分大于250分的所有同学:select * from student where english+chinese+math>250;

范围匹配

查询英语分数在 85-95之间的同学。select * from student where english>=85 and english<=95;select * from student where english between 85 and 95;

or

查询数学分数为84,90,91的同学。select * from student where math=84 or math=90 or math=91;

in

select * from student where math in(84,90,91);查询所有姓何的学生成绩。

模糊查询

select * from student where name like '何%';

and

查询数学分>85,语文分>90的同学。select * from student where math>85 and chinese>90;

oder by [asc/desc]

对数学成绩排序后输出。select * from student order by math;对总分排序后输出,然后再按从高到低的顺序输出select * from student order by math+chinese+english desc;对姓何的学生成绩排序输出select * from student where name like '何%' order by math+chinese+english desc;select name, math+chinese+english from student where name like '何%' order by math+chinese+english desc;

count

统计一个班级共有多少学生?select count(*) from student;统计数学成绩大于90的学生有多少个?select count(*) from student where math>90;统计总分大于250的人数有多少?select count(*) from student where math+chinese+english>250;

sum

统计一个班级数学总成绩?select sum(math) from student;统计一个班级语文、英语、数学各科的总成绩select sum(math), sum(chinese), sum(english) from student;统计一个班级语文、英语、数学的成绩总和select sum(math+chinese+english)from student;select sum(math)+sum(chinese)+sum(english) from student;

avg

求一个班级数学平均分?select avg(math) from student;求一个班级总分平均分select avg(math+chinese+english)from student;select avg(math)+avg(chinese)+avg(english) from student;

maxAndmin

求班级最高分和最低分select max(math+chinese+english),min(math+chinese+english) from student;

group by

综合性练习:为学生表,增加一个班级列,然后训练分组查询查出各个班的总分,最高分准备环境给表添加一个字段:alter table student add column class_id int;更新表:update student set class_id=1 where id<=5;update student set class_id=2 where id>5;select sum(math+chinese+english),max(math+chinese+english) from student group by class_id;查询出班级总分大于1300分的班级IDselect class_id from student group by class_id having sum(math+chinese+english)>1300;select class_id from student where sum(math+chinese+english)>1300 group by class_id ;note:where和group区别: 在wehre子句中不能使用分组函数

时间和日期

mysql> select year (now()), month(now()), day(now()) , date(now());+--------------+--------------+------------+-------------+| year (now()) | month(now()) | day(now()) | date(now()) |+--------------+--------------+------------+-------------+| | 9 | 7 | -09-07 |+--------------+--------------+------------+-------------+select date_add(now(), INTERVAL 2 year) from dual;//增加两年select charset('name') employee;select date_add(now(), INTERVAL -1 day) 昨天, now() 今天, date_add(now(), INTERVAL +1 day) 明天;

字符串相关函数

select concat( charset('name'), 'aaaa') 自定义 from dual;

约束(表的约束)

*定义主键约束primary key:不允许为空,不允许重复*定义主键自动增长auto_increment*定义唯一约束unique*定义非空约束not null*定义外键约束constraint ordersid_FK foreign key(ordersid) references orders(id)*删除主键:alter table tablename drop primary key ;create table myclass(id INT(11) primary key auto_increment,name varchar(20) unique);create table student(id INT(11) primary key auto_increment,name varchar(20) unique,passwd varchar(15) not null,classid INT(11), #注意这个地方不要少逗号constraint stu_classid_FK foreign key(classid) references myclass(id));

连接查询

使用 SQL99 标准的连接查询(JOIN..ON..)

内连接(只返回满足连接条件的数据(两边都有的才显示))

select e., d.from emp einner join dept don e.deptno=d.deptno-- 也可以省略inner关键字。select e.*, d.*from emp e inner join dept don e.deptno=d.deptno

左外连接(左边有值才显示)

select e.empno, e.ename, e.sal, d.dnamefrom emp eleft outer join dept don e.deptno=d.deptno

右外连接(右边边有值才显示)

select e.*, d.*from emp e right outer join dept don e.deptno=d.deptno

满外联接

mysql 不支持full所以需要left right union任一边有值就会显示。//select e.*, d.*from emp efull outer join dept d on e.deptno=d.deptno

mysql> select e.,d. from emp e left join dept d on (e.deptno=d.deptno) union select e.,d. from emp e right join dept d on (e.deptno=d.deptno)

交叉连接(叉集,就是笛卡尔积)

select e.*, d.*from emp ecross join dept d

eg: 查询员工信息,员工号,姓名,月薪,部门名称

select e.empno, e.ename, e.sal, d.dnamefrom emp e, dept dwhere e.deptno=d.deptnoselect e.empno, e.ename, e.sal, d.dnamefrom emp e inner join dept d -- 逗号joinon e.deptno=d.deptno -- where on

// 显示所有部门信息

// 显示各个部门的部门人数

select d.deptno 部门号, d.dname 部门名称,count(e.empno) 人数from emp e, dept dwhere e.deptno(+)=d.deptnogroup by d.deptno, d.dnameselect d.deptno 部门号, d.dname 部门名称,count(e.empno) 人数from emp e right outer join dept don e.deptno=d.deptnogroup by d.deptno, d.dname

mysql 的常用命令

mysql 安装完之后,登陆后发现只有两个数据库:mysql> show databases;+——————–+| Database |+——————–+| information_schema || test |+——————–+,mysql> use mysqlERROR 1044 (42000): Access denied for user ‘‘@’localhost’ to database ‘mysql’访问被拒绝,原因就是在删除数据库时(rpm -e mysql*)没有删除干净,需要把 /var/lib/mysql 的目录全部删除干净,然后再重新安装即可。

连接 MYSQL

格式: mysql -h 主机地址 -u 用户名 -p 用户密码

1、连接到本机上的 MYSQL:回车后提示你输密码,注意用户名前可以有空格也可以没有空格,但是密码前必须没有空格,否则让你重新输入密码。(如果刚安装好 MYSQL,超级用户 root 是没有密码的,故直接回车即可进入到 MYSQL 中了,MYSQL 的提示符是: mysql>)

2、连接到远程主机上的 MYSQL。假设远程主机的 IP 为:192.168.2.2,用户名为 root,密码为 123456。则键入以下命令:mysql -h192.168.2.2 -uroot -p123456。

3、退出 MYSQL 命令:exit (回车)。

修改密码

格式:mysqladmin -u 用户名 -p 旧密码 password 新密码

1、给 root 加个密码 123456。键入以下命令:mysqladmin -u root -password 123456。

2、再将 root 的密码改为 56789:mysqladmin -u root -p123456 password 56789。

增加新用户

格式:grant select on 数据库.* to 用户名 @登录主机 identified by “密码”

1、增加一个用户 test1 密码为 abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用 root 用户连入 MYSQL,然后键入以下命令:mysql>grant select,insert,update,delete on . to test1@”%” Identified by “abc”;mysql>flush privileges; 使之生效2、增加一个用户 test2 密码为 abc, 让他只可以在 localhost 上登录,并可以对数据库 mydb 进行查询、插入、修改、删除的操作(localhost 指本地主机,即 MYSQL 数据库所在的那台主机),这样用户即使用知道 test2 的密码,他也无法从 internet 上直接访问数据库,只能通过 MYSQL 主机上的 web 页来访问了。mysql>grant select,insert,update,delete on mydb.* to test2@localhost identified by “abc”;mysql>flush privileges; 使之生效如果你不想 test2 有密码,可以再打一个命令将密码消掉。mysql>grant select,insert,update,delete on mydb.* to test2@localhost identified by “”;mysql>flush privileges; 使之生效

操作技巧

1、如果你打命令时,回车后发现忘记加分号,你无须重打一遍命令,只要打个分号回车就可以了。也就是说你可以把一个完整的命令分成几行来打,完后用分号作结束标志就 OK。2、你可以使用光标上下键调出以前的命令。

查询、创建、删除、更新命令

1、显示当前数据库服务器中的数据库列表:mysql>show databases;注意:mysql 库里面有 MYSQL 的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。2、显示数据库中的数据表:mysql>use 库名;mysql>show tables;3、显示数据表的结构:mysql>describe 表名;4、建立数据库:mysql>create database 库名;5、建立数据表:mysql>use 库名;mysql>create table 表名 (字段名 varchar (20), 字段名 char (1));6、删除数据库:mysql>drop database 库名;7、删除数据表:mysql>drop table 表名;8、将表中记录清空:mysql>delete from 表名;9、显示表中的记录:mysql>select * from 表名;10、往表中插入记录:mysql>insert into 表名 values (“123”,”b”);11、更新表中数据:mysql>update 表名 set 字段名 1=’a’, 字段名 2=’b’ where 字段名 3=’c’;12、用文本方式将数据装入数据表中:mysql>load data local infile “/root/mysql.txt” into table 表名;13、导入.sql 文件命令:mysql>use 数据库名;mysql>source /root/mysql.sql;14、命令行修改 root 密码:mysql>update mysql.user set password=PASSWORD (‘新密码’) where user=’root’;mysql>flush privileges;15、显示 use 的数据库名:mysql>select database();16、显示当前的 user:mysql>select user();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。