介绍数据库:
-
关系型数据库
mysql 3306
sql server 1433
postgress 5432
oracle 1521 -
非关系型数据库(NOSQL数据库)
mongDB 27017
redis 6379
memcached 11211
文章目录
sql语句:
- 数据库定义语句DDL
基本操作:creat drop alter - 数据库操作语句DML
基本操作: insert delete update select - 数据库控制语句DCL
基本操作:commit grant rollback recoke
1、数据库(database):
(1)创建数据库(creat)
create database 库名;
例1: create database test2;
例2: create database test1 default chartest=utf-8;
(2) 删除数据库(drop)
drop database test2
(3)查看数据库(select)
select database();
(4)切换数据库(use)
use 库名;
(5)显示库中所有表(show)
show tables;
show database;(查看所有库)
(6)查看创建库的语句(show)
show create database 库名;
数据库名:
1、 可以使用数字、字母和下划线,但是不能使用纯数字;
2、 库名具有唯一性;
3、 不能使用特殊字符和MySQL关键字
2、表单(table)
(1) 创建基本的表(create)
表名称、列名称,列属性,逗号分割每列,最后一列不要逗号
create table student(
id int ,
name varchar(22),
age int ,
birthday date
)
- 自增长(auto_increment)
注意:自增长必须是设置主键,int类型
create table student1(
id int(10) auto_increment ,
name varchar(22)
) - not null约束
create table student(
id int ,
age int not null
) - 主键约束
注意:主键唯一、不能为空、实体唯一
create table student(
id int(10) ,
birthday date not null,
PRIMARY key(id)
) - 外键约束
create table score(
id int(10) PRIMARY key auto_increment,
u_id int(10),
foreign key (u_id) references student(id)
) - 默认值约束
create table student(
id int(10) ,
name varchar(22) DEFAULT ‘北京’,
PRIMARY key(id)
) - 唯一约束
create table student(
id int (10) PRIMARY key auto_increment,
name VARCHAR(8) UNIQUE
) - 显示创建的表结构
desc student;
包含主键、外键、唯一、不能为空、默认值、自增长的表单创建
CREATE table student(
id int(10) PRIMARY KEY auto_increment,
name VARCHAR(20) not null,
sex CHAR,
birthday date,
department VARCHAR(20) not NULL,
address VARCHAR(50) DEFAULT '北京'
foreign key (sex) references score(s_sex)
)
(2) 修改表(alter table)
alter主要修改表中的列
- 添加列
基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];
alter table student add name char(20); - 修改列
基本形式:alter table 表名 change 旧列名称 新列名称 新列数据类型;
alter table student change name name char(20);
alter table student change name name char(20) not null; - 删除列
基本形式:alter table 表名 drop 列名称;
alter table student drop name;
(3) 修改表中字段(update … set)
基本形式:update 表名 set 某个值
update stuent set sex=‘女’;
update student set sex=‘男’ where id =2;
练习:
– 把2018-08-08出生的学生的年龄+1岁
update student set age=age+1 where birthday=‘2011-09-09’
– 把编号为大于10的,年龄在20-30 的地址都改为,上海
update student set address=‘上海’ where id>10 and age>=20 and age<=30
(4)添加(insert into … values)
基本形式:insert into 表名 values();
- 整行插入
不写插入哪几列时,主键必须写例如student表中主键为id
insert into student(name,sex) values(‘张珊’,‘男’);
insert into student values(3,‘里斯’,20,‘男’,‘2011-09-09’,‘北京沙河’); - 部分插入
insert into student(name,age,sex) values(‘王五’,25,‘男’); - 多行插入
insert into student(name,age,sex) values(‘小翟1’,25,‘男’),(‘小翟2’,25,‘男’),(‘小翟3’,25,‘男’);
多行插入 数据在其他表中 A—>B
insert into student(name,sex) select name,sex from test;
(5) 删除表(drop )
- drop 数据+表结构
drop table student
(6) 删除表中数据(delete from)
- 删除表中所有数据
delete from student - 删除指定数据
delete from student where id=6;
delete from student where id>6;
delete from student where id>6 and sex=‘女’;
delete from student where sex=‘男’ and age<=18 and age>=1;
删除 性别为null 的学生信息
delete from student where sex is null;
delete from student where sex=’’;
(7) 单表查询(select)
-
查询全部数据
select * from student;
select name,age from student; -
查询区间
select * from student where age>=20 and age<=30;
select * from student where age between 21 and 25; -
查询某个值
select * from student where id=2;
select * from student where id=2 or id=3;
select * from student where id in(1,222,300); -
模糊查询
select * from student where sex=‘女’ and name like ‘张%’ -
去重复(distinct)查询
select distinct address from student; -
排序(order by)
select * from student order by age asc;
select * from student order by id desc; -
where order by
select * from student where sex=‘女’ order by age asc;