【数据库】1库、引擎、约束、表、字段、数据

来源:互联网 发布:linux qt调试器未设置 编辑:程序博客网 时间:2024/06/05 16:24

一、库

库操作 DDL语言 创建 create database 库名 删除 drop database 库名 查看 show databases 用库 use 数据库名

二、引擎

InnoDB:事务型数据库首选引擎,支持事务安全表,支持行锁定
MyISAM:较高的插入,查询速度,不支持事物
MEMORY:将表中数据存储到内存,默认

引擎操作 改引擎 my.ini→mysqld→default-storage-engine的值→重启 alter table 表 engine = 更改后的引擎名; 展示 show engines; show variable like ‘storage_engine’

三、约束

create table 表名(
sno int ①primary key ⑧auto_increment,
sname varchar(10)⑤not null,
sage int(4)⑥unique,
ssex varchar(20) ⑦default ‘男’,(default 111)
②primary key(son),
③foreign key(sno)references 另一个表(id);
⑨check (sno>0)
);

    ②可以多字联合主键primary key(son,sname,sage)
序号 约束 功能 ①② 主键 primary key 不可以重复,不能为空,一个表只有一个主键 ③ 外键 foreign key 外键等于另一个表的主键 ⑤ 非空 not null ⑥ 唯一约束 unique 不出现重复值 ⑦ 默认约束 default ‘默认数据’或数值 默认值 ⑧ 自动增加 auto_increment 初始值1只有一个只能在主键后面 赋值null ⑨ 检查 check(条件) 放在最后
约束 单列增加 alter table 表 modify 列名 数据类型 约束 取消非空 alter table 表名 modify 列名 数据类型 null 多列增加 alter table 表名 add 约束(列名) 删除 alter table 表名 drop 约束名 约束字段名 删除主键 alter table 表名 drop primary key 删除外键 alter table 表名 drop foreign key 外键名

四、表

1、创建 creat

create table 表名(
sno int primary key auto_increment,
sname varchar(10)not null,
sage int(4)unique,
ssex varchar(20) default ‘男’,(default 111)
primary key(son),
foreign key(sno)references 另一个表(id)
);

2、 删除表(先删子,再删父)
drop table 表名; drop table if exists 表名1,表名2
3、改表名
alter table 旧名 rename 新名;
4、查看表
show tables;desc 表名;show create tables 表名/G;
5、别名
select * from 表 as 别名 where 别名.字段=条件;
select c.c_id,o.o_id from cus as c left join order as o on c.c_id=o.o_id;
6、另存为表
create table 新名 as select * from 旧名;

五、字段

1、增
alter table 表名 add 列名 类型()not null;
alter table 表名 add 列名 类型() (first/after 已存在列名 );
2、 删除
alter table 表名 drop 字段名;
3、改列名
alter table 表 change 旧名 新名 数据类型(旧名=新名只改变数据类型)
改数据类型
alter table 表modify 列名 数据类型
改位置
alter table 表 modify 列名 数据类型 first/after 列名2;
4、查
desc 表名;
5、别名 as
select f1.f_name as fruit_name,f1.f_price as fruit_price from fruit as f1 where f1.fprice<8;


六、数据

1、增
insert into 表名(列名1,列名2) value (‘据1’,’据2’);
insert into 表名(列名1,列名2) value (‘据1’,’据2’),(‘据3’,’据4’);

没有写列名,插入数据必须与表中所有字段一致,主键插入为null
返回信息:
. Recorde插入记录条数
. Duplicutes插入被忽略记录(主键重复)
. Warning有问题数值(数据类型)

2、 删除
delete from 表 where 条件;自动增长,不会从1开始
清空:truncate table 表;自动增长从1开始
删除重复:delete distinct 类型 from 表名;
3、改 updatate set value
update 表 set 字段=值,字段=值 where 条件;
4、查询结果插入表中
insert into 表1(列名)select 列名 from 表2;

0 0
原创粉丝点击