SQL 数据库基础知识

数据库: 数据库是按照一定规则来管理数据的仓库. 数据库用的最多的就是增删改查

sql 语言

sqlserver
mysql
oracle
sqlite

数据库 是一个文件柜 数据库里面存储数据表 数据表存储详细数据
数据表有结构
数据行成为记录

–创建数据库
***–sqlite3 dbname.db ***
– 数据库的后缀.db
–使用sqlite studio 创建数据库

–打开数据库
.open dbname.db

–创建数据表
– int float double string char
integer int
real double
text 文本 string

int --> int
char(1) --> char
varchar(N)/char(N) --> string
float --> float

–创建一个学生信息表
– stuid name sex age phone email
– varchar(30) varchar(10) char int char(11) varchar(50)

create table stuinfo
(
id varchar(30) primary key not null,
name varchar(10) not null,
sex char(1) default ‘男’,
age int,
phone char(11) unique ,
email varchar(50) unique
);
–删除表
drop table stuinfo;
–查看所有数据表
.tables
–查看数据库
.databases

–一张完整的数据表只能必须有一个主键 主键是一个唯一能够区分一条记录的表头项
primary key–主键
not null – 某一列不能为null
default: – 默认
unique: --唯一

.schema name —查看表结构

–插入数据

insert into stuinfo values() – 按照默认的顺序依次插入
insert into stuinfo(sex, age, name) values() – 按照约定好的顺序插入信息

insert into stuinfo values
(‘180703’,‘张三疯’,‘男’,20,‘13245678903’,‘[email protected]’),
(‘180704’,‘李思思’,‘女’,18,‘13245678904’,‘[email protected]’),
(‘180705’,‘王污污’,‘男’,45,‘13245678905’,‘[email protected]’),
(‘180706’,‘赵六六’,‘女’,20,‘13245678906’,‘[email protected]’),
(‘180707’,‘呵呵哒’,‘男’,18,‘13245678907’,‘[email protected]’),
(‘180708’,‘巴扎黑’,‘女’,16,‘13245678908’,‘[email protected]’),
(‘180709’,‘select’,‘男’,18,‘13245678909’,‘[email protected]’),
(‘180710’,‘update’,‘男’,15,‘13245678910’,‘[email protected]’);

insert into stuinfo(age,email,phone,id,name)
values(18,‘[email protected]’,‘13245678902’,‘180702’,“王哈哈”);
– 这样做法是错误的 主键不能为null 在工程开发中主键必定是唯一的
insert into stuinfo(age,email,phone,name)
values(19,‘[email protected]’,‘13245678904’,“呵呵哒”);
–查询 通配符 就是all 列
select * from stuinfo;
select id, name, sex from stuinfo;
–查找符合要求的信息
select * from stuinfo where sex != ‘女’ or age = 18;
–删除符合要求的信息
delete from stuinfo where age = 45;
–删除数据表中全部信息
delete from stuinfo;
–修改符合要求的信息
update stuinfo set age = 1000 where name = ‘李思思’ or sex = ‘男’;

select * from stuinfo where age > 1000;

猜你喜欢

转载自blog.csdn.net/wang18236618195/article/details/82595556