关于Mysql的数据库操作(数据库和表操作)

此篇文章讲的内容包含:

数据库的增删

数据表的增删改

数据库的数据插入和更新

索引创建

---------------------------------------database据库篇--------------------------------------------

#数据库的创建
create
database crm_info_test;
#数据库的删除
drop
database crm_info_test;

-------------------table表篇----------------

#创建中介表
create table zj_zjxx(
    ID varchar(64) primary key ,
    zj_name varchar(10) unique
);
#创建客户表
create table kh_khxx(
    ID varchar(64) primary key,
    zj_id varchar(64) not null,
    kh_name varchar(20),
    constraint fk_zhkh foreign key(zj_id) references zj_zjxx(ID)
);
#创建合同表
create table ht_htxx(
    ID varchar(64) primary key,
    kh_id varchar(64) not null,
    ht_date datetime,
    ht_name varchar(20),
    ht_price float
) ;
#修改表
#新增一列
alter table kh_khxx
    add kh_no1 varchar(10),
    add kh_temp int;
#修改列名
alter table kh_khxx
    change kh_no1 kh_no varchar(10);
#删除列
alter table kh_khxx 
    drop column kh_temp;
#新增约束 
alter table ht_htxx
    add constraint htkh foreign key(kh_id) references kh_khxx(ID),
    add constraint price_5 check (ht_price>5);
#删除约束
alter table ht_htxx
    drop check price_5;
#删除表
drop table test;

----------------插入数据篇----------------

#表中插入数据 

insert into zj_zjxx values (replace(uuid(),"-",""),'广州你最好中介机构');
#指定列名插入数据 
insert into kh_khxx(ID,zj_id) values (replace(uuid(),"-",""),'22ffadefb2d611eaba23005056c00001');

--------------更新数据篇---------------

#修改某个值
update kh_khxx
set kh_name='上海' where kh_no = 1;

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.  To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

如果更新有以上的报错,执行一下下面的语句即可,解锁安全模式

SET SQL_SAFE_UPDATES = 0

-----------------索引篇------------

#建立索引 
create index kh_index on kh_khxx(kh_no);

持续更新。。。。(数据的查询会重新开一篇,这一篇不讲查询)

猜你喜欢

转载自www.cnblogs.com/xiaoqingSister/p/13170559.html