(php)导入.sql并在表存在时添加列或创建新表

时间:2022-07-14 22:26:33

I don't know if its possible.

我不知道它是否可能。

I released a script version 1.0 and now I am about to release 1.2 version and i made some upgrade in database tables like change name field size or default value also added few new columns and table then export full db to .sql file(new db).

我发布了一个脚本版本1.0,现在我即将发布1.2版本,我在数据库表中进行了一些升级,如更改名称字段大小或默认值,还添加了几个新列和表,然后将完整数据库导出到.sql文件(新数据库) 。

Is it possible from php to import new .sql file and add new tables and also change columns structure without deleting any data from table and add new columns in existing tables ?

是否有可能从php导入新的.sql文件并添加新表并更改列结构而不删除表中的任何数据并在现有表中添加新列?

I tried to import it but it shows this error : 'ALTER TABLE table1 ADD PRIMARY KEY (id); ': Multiple primary key defined

我试图导入它,但它显示此错误:'ALTER TABLE table1 ADD PRIMARY KEY(id); ':定义了多个主键

Example :

示例:

Old Database v1.0

Table 1

ID (primary key , AI)

ID(主键,AI)

First Name(size-50 ,default - none)

名字(大小为50,默认 - 无)

Last Name(size-50 ,default - none)

姓氏(大小为50,默认 - 无)

Email(size-50 ,default - none)

电子邮件(大小为50,默认 - 无)

Password(size-50 ,default - none)

密码(大小为50,默认 - 无)


New Database v1.2

Table 1

ID (primary key , AI)

ID(主键,AI)

First Name(size-100 ,default - none)

名字(大小为100,默认 - 无)

Last Name(size-100 ,default - none)

姓氏(大小为100,默认 - 无)

Email(size-255 ,default - none)

电子邮件(大小255,默认 - 无)

Password(size-50 ,default - none)

密码(大小为50,默认 - 无)

Gender (size 10 , default - none)

性别(大小10,默认 - 无)

Added Table 2

ID (primary key , AI)

ID(主键,AI)

coloum1 (size-100 ,default - none)

coloum1(size-100,默认 - 无)

coloum2 (size-255 ,default - none)

coloum2(size-255,default - none)


Is it Possible to import new .sql over old database without loosing any data from php and also add data from new database?

是否可以在旧数据库上导入新的.sql而不从php中删除任何数据并从新数据库中添加数据?

I done my best to explain hope you understand.

我尽力解释希望你理解。

EDIT : I use this code to import new .sql file and its not working: how to import .sql file in mysql database using php

编辑:我使用此代码导入新的.sql文件,它无法正常工作:如何使用php导入mysql数据库中的.sql文件

1 个解决方案

#1


0  

you get that message because column id already exist and has KEY, if its PRIMARY KEY and AUTOINCREMENTED then it can not be duplicated, try this:

你得到那条消息,因为列id已经存在并且有KEY,如果它的PRIMARY KEY和AUTOINCREMENTED那么它不能重复,试试这个:

ALTER TABLE Table1 ADD COLUMN Gender varchar(10) NOT NULL DEFAULT 'none';
ALTER TABLE Table2 ADD COLUMN coloum1 varchar(100) NOT NULL DEFAULT 'none';
ALTER TABLE Table2 ADD COLUMN coloum2 varchar(255) NOT NULL DEFAULT 'none';

or just import the new columns

或者只是导入新列

#1


0  

you get that message because column id already exist and has KEY, if its PRIMARY KEY and AUTOINCREMENTED then it can not be duplicated, try this:

你得到那条消息,因为列id已经存在并且有KEY,如果它的PRIMARY KEY和AUTOINCREMENTED那么它不能重复,试试这个:

ALTER TABLE Table1 ADD COLUMN Gender varchar(10) NOT NULL DEFAULT 'none';
ALTER TABLE Table2 ADD COLUMN coloum1 varchar(100) NOT NULL DEFAULT 'none';
ALTER TABLE Table2 ADD COLUMN coloum2 varchar(255) NOT NULL DEFAULT 'none';

or just import the new columns

或者只是导入新列