-- cmd控制台中链接mysql
mysql -h localhost -u root -p
-- 显示所有数据库
show databases;
-- 建表
create table t_test(
id int auto_increment primary key not null,
name varchar(20) unique,
birthday date,
begintime datetime,
stamp timestamp,
payAmount float default 0,
peopleid int,
constraint fk_peopleid foreign key(peopleid) references t_people(id)-- 外键约束1
);
-- 外键约束2
alter table t_test add constraint fk_peopleid foreign key(peopleid) references t_people(id);
-- 删除外键
alter table customerinfo drop foreign key fk_reference_13;
-- 修改表名
alter table t_test rename t_newname;
-- 创建索引(5是长度,desc是排序方式)
create unique index test_index1 on t_test(name(5) desc, stamp);
create index test_index2 on test(name);
-- 查看表格结构
describe t_test;
-- 查看表格语法
show create table t_test;
-- 在表的最后一个位置增加字段
alter table t_test add name varchar(20);
-- 在表的第一个位置增加字段
alter table t_test add name varchar(20) first;
-- 在表的指定字段之后增加字段
alter table t_test add name varchar(20) after id;
-- 删除字段
alter table t_test drop name;
-- 修改字段
alter table t_test modify name char(50);
-- 修改字段的名字(若需要改变类型,则把varchar也做改变)
alter table t_test change name newname varchar(20);
-- 修改字段的顺序
alter table t_test modify name first;-- 放在第一个位置
alter table t_test modify name after id;-- 放在id后
-- 修改某字段为主键
alter table t_test add primary key (id);
-- 使主键成为自增长
alter table t_test change id id int primary key not null auto_increment;
mysql -h localhost -u root -p
-- 显示所有数据库
show databases;
-- 建表
create table t_test(
id int auto_increment primary key not null,
name varchar(20) unique,
birthday date,
begintime datetime,
stamp timestamp,
payAmount float default 0,
peopleid int,
constraint fk_peopleid foreign key(peopleid) references t_people(id)-- 外键约束1
);
-- 外键约束2
alter table t_test add constraint fk_peopleid foreign key(peopleid) references t_people(id);
-- 删除外键
alter table customerinfo drop foreign key fk_reference_13;
-- 修改表名
alter table t_test rename t_newname;
-- 创建索引(5是长度,desc是排序方式)
create unique index test_index1 on t_test(name(5) desc, stamp);
create index test_index2 on test(name);
-- 查看表格结构
describe t_test;
-- 查看表格语法
show create table t_test;
-- 在表的最后一个位置增加字段
alter table t_test add name varchar(20);
-- 在表的第一个位置增加字段
alter table t_test add name varchar(20) first;
-- 在表的指定字段之后增加字段
alter table t_test add name varchar(20) after id;
-- 删除字段
alter table t_test drop name;
-- 修改字段
alter table t_test modify name char(50);
-- 修改字段的名字(若需要改变类型,则把varchar也做改变)
alter table t_test change name newname varchar(20);
-- 修改字段的顺序
alter table t_test modify name first;-- 放在第一个位置
alter table t_test modify name after id;-- 放在id后
-- 修改某字段为主键
alter table t_test add primary key (id);
-- 使主键成为自增长
alter table t_test change id id int primary key not null auto_increment;