PHP SQL 数据库

发布于 2024-12-02 06:55:17 字数 280 浏览 1 评论 0原文

我有一个创建数据库,数据库中有大约 8 个表,还在相应的表中创建了主键和外键。但是当我在主表中插入数据时,我的其他表不显示更新的数据。

我的意思是,假设我有一个表,其中包含诸如以下名称的数据: N 是(名字) N1 = George,N2 = Ross,N3 = Rim ...等等,这意味着我有主键 N1,N2,N3 等。

现在,当我在其他表中插入这个主键时,它应该显示我的名字,例如 George,ross和 rim 而不是主键编号本身(N1,N2,N3)。

我怎样才能得到名字而不是PK本身?

I have a create database and have about 8 tables in Database also created Primary keys and foreign-keys in appropriate tables. But when I insert data in primary-table, my other table doesn't show updated data.

I mean, say I have a table which has data for names like ;
N is (name)
N1 = George, N2 = Ross, N3 = Rim ...etc now that means i have Primary key N1,N2,N3 etc..

Now, when I insert this primary keys in others table it should shows me name like George, ross and rim instead of primary-key number it self(N1,N2,N3).

How can I get names instead PK itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看海 2024-12-09 06:55:17

您误解了关系数据库中键的概念。键的存在,不是为了从相似的表中复制数据,而是为了显示不同表中数据之间的关系。它们有助于理解不同表之间的数据如何相关——这就是“关系数据库”名称的由来。如果建立索引,它们还可以加快数据的查询速度。

您可以在此处阅读有关密钥用法的更多信息:密钥和规范化

You are misunderstanding the concept of keys in relational databases. Keys are there, not to copy data from similar tables but to show the relations between data in different tables. They help to understand how the data between different tables is related - that is where the name "relational database" comes from. They also speed up querying of that data if indexed.

You can read more about the usage of keys here: Keys and normalization

岁月无声 2024-12-09 06:55:17

我仍然不清楚你到底想用数据库做什么。但让我向您演示数据库的基础知识以及应该如何使用它。考虑一个表users,您将在其中存储与用户相关的数据。

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) DEFAULT NULL,
  `phone` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`),
);

id 保存主键并具有一个名为 auto_increment 的属性,现在这意味着每次您向该表插入一条记录时,id 属性都会递增,而您不需要不必担心在此列中插入任何值,因为您的数据库会处理这个问题。例如,看看下面的插入查询。

INSERT INTO users(name,email,phone) VALUES('First Name', '[email protected]', '9999999999');
INSERT INTO users(name,email,phone) VALUES('Second Name', '[email protected]', '8888888888');
INSERT INTO users(name,email,phone) VALUES('Third Name', '[email protected]', '2222222222');
INSERT INTO users(name,email,phone) VALUES('Fourth Name', '[email protected]', '3333333333');

您看到了吗,您没有在此处插入任何id。这是因为由数据库来处理逻辑。现在,第一条记录将保存值 1,第二条记录将保存 2,第三条记录将保存 3,第四条记录将保存 4 >等等。

希望这对你有帮助。

I am still unclear on what exactly you want to do with the database. but let me demonstrate you on the basic of database and how you should be using it. Consider a table users where you will be storing the data related to user.

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) DEFAULT NULL,
  `phone` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`),
);

the column id holds the primary key and have an attribute called auto_increment, now what this means is every time you insert a record to this table the id attribute gets incremented and you don't have to worry about inserting any value in this column because your database will take care of that. for example take a look at insert query below.

INSERT INTO users(name,email,phone) VALUES('First Name', '[email protected]', '9999999999');
INSERT INTO users(name,email,phone) VALUES('Second Name', '[email protected]', '8888888888');
INSERT INTO users(name,email,phone) VALUES('Third Name', '[email protected]', '2222222222');
INSERT INTO users(name,email,phone) VALUES('Fourth Name', '[email protected]', '3333333333');

did you see you did not insert any id here. this is because it is database who will handle the logic. now the first record will hold the value 1 the second will have 2 the third one 3 and the fourth one 4 and so on.

hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文