MySQL:结构化查询语言
DBMS:数据库管理系统
数据库(DB)存储数据的基本单位:表(table)
默认端口号:
MySQL:3306
SQL Server:1433
Oracle:1521
数据类型:
整型:int 4个字节(32位)
小数类型:
decimal(5,2) 5:总长度、2:小数位
float 4个字节(小数点后6~7位)
字符型:
char --定长:固定长度字符串,数据占用的存储空间是按建表时设计的长度来计算的[char(10):10是字符个数]
varchar --变长:可变长度字符串,数据占用的存储空间是按数据实际长度来计算的
日期型:
date
time
datetime: [height(3,2)]
-- 启动/关闭mysql服务:net start/stop [服务名]
net start/stop mysql57;
-- 命令行进入数据库:mysql –h [IP地址] –p [端口号] –u [用户名] –p[密码]
mysql –u root –p
-- 查看数据库(系统数据库:mysql、sys、performance_schema、information_shema)
show databases;
--查看数据库下的库表
show tables;
--创建数据库(带字符集):create database [数据库名] charset=utf8;
create database Student charset=utf-8;
--删除数据库:drop database [数据库名];
drop database Student;
--使用数据库:use [数据库名];
use Student;
--创建表: create table [表名] ([列名1] [数据类型1],[列名2] [数据类型2]…);
create table stu(
no int,
name varchar(10),
sex char,
bir date
)
--新建表名1,从表名2提取字段列表中的字段到表2:create table 表名1 select 字段列表 from 表名2;
--字段取别名as:create table 表名1 select 字段列表 as 别名 from 表名2;
--拷贝表结构:create table 表2 select * from 表1 where 不成立条件(1=2);
create table temp select * from stu where 1=2;
--查看表结构:desc(describe) [表名];
desc stu;
--向表插入数据:insert into 表名( 字段列表 ) values ( 值列表 );
insert into stu(no,name,sex,bir) values(1,’路飞’,’男’,’2000-05-05’);
--造数据 insert into 表1(字段名) select 字段名 from 表2;
insert into temp(no) select no from stu;
--删除数据:delete from [表名] where [条件子句];
delete from stu where name is null;
--销毁表: drop table [表名];
drop table stu;
--更新/修改数据:update 表名 set 字段名1=值1,字段名2=值2… where [条件子句];
update stu set sex=’女’ where no=1;
--查询表的全部数据: select [字段名] from [表名];
select * from stu;
--去重
1.提取唯一数据放入备份表:create table 备份表 select 唯一数据 from 源表;
create table stu_bak2 select distinct * from stu;
2.删除重复数据(所有数据)distinct:delete from 源表;
delete from stu;
3.唯一数据写回到源表:insert into stu select * from stu_bak2;
insert into 源表 select * from 备份表;
--数据排序:select 字段列表 from 表名 [where...] order by 字段名;
order by 默认按字段数据的升序asc排列,降序desc
1.order by 放在select最后
2.order by 可用别名
3.order by 复合排序(联合排序)
--先按年龄,再按学号排序
select * from stu order by age,no;