sql语句之增、删、改、查

1、常用数据库端口号

(1)关系型数据库
Postgre SQL 5432
Mysql 3306
Sql server 1433
Oracle 1521

(2)NOSQL数据库(非关系型数据库)
mongoDB 27017
Redis 6379
Memcached 11211

2、sql语句(编写一些不同类型的sql命令)

Sql---->结构化查询语句
(1)数据定义语言DDL
Creat drop alter
(2)数据操作语言DML
Insert delete update select
(3)数据控制语言DCL
Commit grant rollback(事物) recoke(用户角色)

3、数据库中的表和字段是什么?

表是具有列和行的模型中设计的数据集合
在表中,制定了列数称为字段,未定义的行数称为记录

https端口号443
http端口号80

4、什么是数据库

有序形成的一组信息,用于访问、存储和检索数据

5、数据库定义语句

Create drop after 创建、删除、修改
Show select use 查询、切换

1、查看有哪些数据库
Show database;
Show craete database 库名;
Select database();

2、创建数据库
Create database 数据库名称 default charset=utf-8;
创建数据库并定义数据库语言

3、删除数据库
Drop database 数据库名称;

4、切换数据库
Use 数据库名称;

5、数据库库名规则
可以使用数字、字母、下划线;但是不能使用纯数字
库名具有唯一性
不能使用特殊字符和mysql关键字

Create database student default charset=utf-8;
Drop database student;
Select database();
Show database;
Use student;

6、表的创建、删除create drop

Create alter drop
Insert update
1、创建基本表格
Create table 表名称(列 字段类型 逗号隔开)
库 create database 库名称 default 库字符类型;

Create table scoret(
Id int primary key zuto_increment, 主键且自增长
Name varchar(20) not null unique, 不能为空且唯一
Age int(20) default 20, 默认值约束
Foreign key(sc_id) references student(id) 外键

2、删除表
Drop table 表名称;
3、显示表结构
Desc stuent
Show tables;
Use student

4、删除外键
Alter table 表名 drop constraint 外键约束名

5、主键增删
Alter table 表名 add constraint primaryKey primary key();
Alter table 表名 drop constraint 主键名称

表规则auto_increment
主键:primary key(id)
自增长,必须设置主键auto_increment (int类型、唯一不能为空)
不能为空 :not null
外键 :foreign key() references 表() 引用
默认值约束:default ‘北京’
唯一:unique

Primary key auto_increment
Foreign ker() references 别的表的()
Unique
Nou null
Default ‘’

6、表中列修改 alter

Alter table 表名 drop constrint 字段
1、增加
基本形式:alter table 表名 add 列名 列数据类型 【after 插入位置】;
Alter table student add city char(30);

2、删除
基本形式:alter table 表名 drop 列名称
Alter table student drop city;

3、修改
基本形式:alter table 表名 change 列名称 新的列名 新列数据类型
Alter table student change name city char(20);

7、数据添加insert into…values

Select * from srudent
Select * form student where id =6;
增加数据(整行插入、部分值、多行插入(字段、表))
(1)整行插入
Insert into student values(2,’张珊’,’男’,20)
(2)部分值插入
Insert into student (name,age) values (‘李四’,21)

(3)多行插入
多个值中间用逗号隔开
Insert into student (name,age) values (‘李四’,21),(‘李四’,21),(‘李四’,21),(‘李四’,21)
Insert into student (name,age) select name,age from test;

8、数据删除 delete from …where

Drop database student; 删除库
Drop table student; 删除表和数据
(1)删除所有
Delete from student;
(2)删除部分
Delete from student where id=4;
Delete from student where id>6 and sex=’女’;
Delete from student where id =4 and sex=’女’;
Delete from student where sex is null;
Delete from student whers sex=’’;
= > < >= <= and or is

9、数据创建update …set

1、修改单列、多列
Update student set sex=’女’;
Update student set sex=’女’,address=’’;

2、where条件update…set …where…
Update student set sex=’女’ where id=2;

Update student set age=age+1 where date=’2021-01-02’;
Update student set name=’额吉’,sex=’男’ where date=’2022-11-22’;

10、数据查询select 值 from …

模糊查询 % _ l
_ 任意一个字符
% 任意0或多个字符

1、条件下所有值
(1)全部
Select * from student;
(2)区间:
Select * from student where id>2 and id<4;
Select * from student where id between 2 and 4
(3)某个值
Select * from student where id=2;
Select * from student where id=2 or id=4;
Select * from student where id in (2,4);

2、条件下部分字段
Select name,id from student where id between 2 and 8;

3、模糊查询
Select * from student where name like ‘涨__’

4、去重distinct
Select distinct address from student

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值