MYSQL基础

SQL 语言分类

  • 数据定义语言:简称DDL(Data Definition Language),用来定义数据库对象:数据库,表,列等。关键字:create,alter,drop等。
  • 数据操作语言:简称DML(Data Manipulation Language),用来对数据库中表的记录进行更新。关键字:insert,delete,update等。
  • 数据控制语言:简称DCL(Data Control Language),用来定义数据库的访问权限和安全级别,及创建用户。
  • 数据查询语言:简称DQL(Data Query Language),用来查询数据库中表的记录。关键字:select,from,where 等。

SQL 数据类型分类

  • int:整型
    • tinyint:很小的整数
    • smallint:小的整数
    • mediumint:中等大小的整数
    • int:普通大小的整数
  • double:浮点型
    • float:单精度浮点数
    • double:双精度浮点数
    • decimal(m,d):严格压缩的定点数
  • varchar:可变字符型
    • char(M):M 为0~255之间的整数
    • varchar:M 为0~65535之间的整数
    • tinyblob:允许长度为0~255字节
    • blob:允许长度为0~65535之间的整数
  • date:日期类型
    • year:YYYY:1901~2155
    • time:HH:MM:SS
    • date:YYYY-MM-DD
    • datetime:YYYY-MM-DD HH:MM:SS
    • timestamp:YYYY-MM-DD HH:MM:SS

数据库常用操作

  • 创建数据库
1
2
create database databasename character set (character set);		//创建数据库并指定字符集
> create database mydatabase character set utf8;
  • 显示数据库
1
> show databses;
  • 删除数据库
1
> drop database mydatabase;
  • 切换数据库
1
> use mydatabase;
  • 表创建
1
2
3
4
5
6
7
8
9
10
11
12
create table tablename(
row_name1 datatype constraint, //row_name1, 列名
row_name2 datatype constraint, //datatype, 数据类型
......
row_name2 datatype constraint //constraint, 约束
);

> create table users(
uid int,
uname varchar(20),
useraddress varchar(200)
);
  • 删除表
1
> drop table users;
  • 主键约束
1
2
3
4
5
6
7
8
9
10
create table tablename(
row_name1 int primary key, //在创建表时创建主键,在字段后面加上 'primary key'
......
);

create table users(
uid int primary key auto_increment, //auto_increment 实现主键的uid 的自动增长
uname varchar(20),
useraddress varchar(200 )
);
  • 查看表
1
> show tables;
  • 查看表格式
1
> desc users;

修改表结构

  • 添加列
1
2
3
alter table tablename  add row_name datatype constraint;

alter table users add tel int;
  • 修改列
1
2
3
alter table tablename modify row_name datatype constraint;

alter table users modify tel varchar(50);
  • 修改表明
1
2
3
alter table tablename change old_row new_row datatype constraint;

alter table users change tel telphone double;
  • 删除列
1
2
3
alter table tablename drop row_name;

alter table users drop telphone;
  • 修改表名

    1
    2
    3
    rename table old_table to new_table;

    rename table users to user;
  • 数据库表中添加数据

商品表product

id (primary key,int) name(varchar(100)) price(double)
1 笔记本 22
2 Google Pixel3 8800
3 RedMi 5A 400
1
2
3
4
5
> create table product(
id int primary key auto_increment,
name varchar(100) not null,
price double
);
1
2
3
4
5
insert into table_name(row_name1,row_name2...) values(value1,value2...);

insert into product(id,name,price) values(1,'笔记本','22');
insert into product(id,name,price) values(2,'Google Pixel3','8800');
insert into product(name,price) values('RedMi 5A','400');
  • 数据库表中修改数据
1
2
3
4
update table_name set row1_name='value1',row2_name='value2' where condition

update product set price=30 where id=1;
update product set price=2000 where id=2 or id=3;
  • 数据库表中删除数据
1
2
3
delete from table_name where condiction;

delete from product where id=3;
  • SQL 查询语句
1
2
3
4
5
create table zhangwu(
id int primary key auto_increment,
names varchar(200),
money double
);
1
insert into zhangwu(names,money)values('服装支出',1000),('吃饭支出',325),('股票收入',8000),('打麻将支出',8000);
1
select id,names,money from zhangwu;	//查询指定列的数据
  • 条件查询
1
2
3
selete row_name from table_name where condition;

select id,names,money from zhangwu where money > 5000;