DDL 数据定义语言 - Data Definition Language
DML 数据处理语言 - Data Manipulation Language – 增删改
DCL 数据控制语言 – Data Control Language
DQL 数据查询语言 – Data Query Language - 查询
--------数据库操作语句--------
1.连数据库
mysql -h IP地址 -p 端口号 -u root -p
mysql -h 192.168.\**.11 -p 3306 -u root -p
2.查询当前所有的数据库
show databases;
3.查看当前数据库的创建方式:查看数据库的编码表
show create database 库名;
show create database mydb2;
4.创建数据库
不指定编码表
create database 库名;
create database mydb1;
指定编码表
create database 库名 character set 编码集;
create database mydb2 character set utf8;
5.删除数据库
drop database 库名;
drop database mydb1;
6.修改数据库编码集
alter database 库名 character set 字符集;
alter database mydb2 character set utf8;
7.切换数据库
use 库名;
use mydb1;
8.查询当前正在使用的数据库
select database();
--------数据表整体结构操作语句--------
1.创建表
create table 表名(
列名 类型(长度),
列名 类型(长度),
…
列名 类型(长度)
);
create table emp(
name varchar(50),
password varchar(50),
sex char(10),
birthday date
);
2.数据类型
varchar char
blob
text
tinyint smallint int bigint
bit
Date time datetime timestamp
Double 小数
3.查看该数据库的所有表
show tables;
4.查看表的列的信息(查看表结构)
desc 表名;
5.单表创建时约束
列名 数据类型 约束条件
id int
1).主键约束
primary key
2).自增长
auto_increment
id int primary key auto_increment
3).唯一约束
unique
4).非空约束
not null
age int unique not null,
6.修改数据表
增加列
alter table 表名 add 列名 类型(长度) 约束;
alter table emp2 add salary double;
alter table emp2 add age int;
修改现有列类型、长度和约束
alter table 表名 modify 列名 类型(长度) 约束;
修改birthday列不能为null
alter table emp2 modify birthday date not null;
修改name列的长度为60
alter table emp2 modify name varchar(60) not null;
修改现有列名称
alter table 表名 change 旧列名 新列名 类型(长度) 约束;
修改列名name变为username
alter table emp2 modify birthday date not null;
修改name列的长度为60
alter table emp2 modify name varchar(60) not null;
修改现有列名称
alter table 表名 change 旧列名 新列名 类型(长度) 约束;
修改列名name变为username
alter table emp2 name username varchar(60) unique not null;
删除现有列
alter table 表名 drop 列名;
删除age列
alter table emp2 drop age;
修改表名
rename table 旧表名 to 新表名;
将emp2表名修改为person表
rename table emp2 to person;
修改表的字符集
alter table 表名 character set 编码集;
将emp的编码修改成utf8
alter table person character set utf8;
7.数据表删除
drop table 表名;
drop table person;
--------简单数据表内容的增删改--------
DML - 增删改
DQL - 查询
1.insert语句----向数据表插入数据
insert into 表名(列名1,列名2,列名3…) values(值1,值2,值3…);
insert into 表名 values(值1,值2,值3…);
insert into person(id,username,password,sex,birthday,salary) values(null,'zhangsan','123','nan','1992-8-19',12000);
2.update语句—数据记录修改操作
update 表名 set 列名=值,列名=值… [ where条件语句 ];
update person set dalary=8888 where username='zhangsan';
update person set username='zhaoliu',password='zhaoliu' where id =3;
3.delete语句-----删除表中数据的语句
delete from 表名 [where条件语句];
删除person表中username为zhaoliu的用户记录
delete from person where username='zhaoliu';
删除表中所有记录
delete from person;
4.truncate 语句----删除数据
truncate table 表名;
truncate atble person;
--------数据表中的数据记录操作语句(★★★★★)--------
1.查询数据库中的某张表的所有数据
select * from 表明;
select * from student;
2.查询某张表中指定的列
select 列名,列名… from 表名;
select name,age from student;
3.按条件查询
select 列名,列名…from 表名 where 条件;
查询表中年龄大于等于24岁的学生信息
select * from student where age >= 24;
4.运算符
(大于) <(小于) >=(大于等于) <=(小于等于) =(相等) <>或者!=(不相等)
and(逻辑与) or(逻辑或) not(逻辑非)
select * from student where age >23 and score >80;
between…and…在两之间取值
select * from student where score >= 80 and score <= 100;
select * from student where score between 80 and 100;
in(值1,值2,值3)在指定值中任意取
select * from student where age in(23,24,25);
select * from student where age =23 or age = 24 or age = 25;
模糊查询:like’模糊查询部分’
%表示零或任意多个字符;
_任意单个字符;
select * from student where name like '%张%';
is null 判断该列值是否为空
select * from student where birthday is not null;
5.过滤重复数据
select distinct 列名 from 表名 [where 条件];
select distinct age from student;
6.对查询的结果进行排序
select * from 表名 order by 列名1 asc|desc,列名2(asc|desc),列名3(asc|desc) 。。。。。;
asc是升序排列,desc是降序排列。默认是升序排列
select * from student order by score desc,age,desc;
7.别名:可以对查询出来列名起别名
select 列名 as 别名,列名 as 别名,列名 as 别名… from 表名 where 条件;
在使用别名的时候,as 关键字可以省略
select name as 姓名,age as 年龄 from student;
select name 姓名,age 年龄 from student;
————————————————
版权声明:本文为博主的原创文章,转载请附上原文出处链接。