الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

انشاء قاعدة بيانات بجمل اس كيو ال

بدأه pro2007 في 17 يوليو 2010 · 1 رد · 747 مشاهدة · في قواعد بيانات Microsoft SQL Server
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

عندي سكيما ديجرام عن شركة معينة وكنت بحاجه الى تحويلها الى داتا بيس عن طريق الاكواد فكتبت الكود الاتى ولكن يظهر لى خطأ انه لا يوجد جدول اسمه

department

فما هو الناقص فى الكود جزاكم الله خيرا

USE [Company]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[depertment]') AND type in (N'U'))
DROP TABLE [dbo].[depertment]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dept_location]') AND type in (N'U'))
DROP TABLE [dbo].[dept_location]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[project]') AND type in (N'U'))
DROP TABLE [dbo].[project]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[work_on]') AND type in (N'U'))
DROP TABLE [dbo].[work_on]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dependant]') AND type in (N'U'))
DROP TABLE [dbo].[dependant]
GO

SET ANSI_NULLS ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id= OBJECT_ID(N'[dbo].[Employee]') AND type in (N'U'))
BEGIN

CREATE TABLE [dbo].[Employee]
( Fname  VARCHAR(15)  NOT NULL,
  Minit  CHAR,
  Lname  VARCHAR(15)  NOT NULL,
  Ssn    CHAR(9)      NOT NULL,
  Bdate  DATETime,
  Address VARCHAR(30),
  Sex CHAR,
  Salary DECIMAL(10,2),
  Super_ssn CHAR(9),
  Dno INT     NOT NULL,

 PRIMARY Key (Ssn),
 Foreign Key (Super_ssn) REFERENCES employee(Ssn),
 Foreign Key (Dno) REFERENCES Department(Dnumber));
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id= OBJECT_ID(N'[dbo].[Department]') AND type in (N'U'))
BEGIN

CREATE TABLE [dbo].[Department]
(Dname VARCHAR(15)   NOT NULL,
 Dnumber  INT NOT NULL,
 Mgr_ssn CHAR(9),
 Mgr_start_date  DATEtime,
PRIMARY KEY (Dnumber),
UNIQUE(Dname),
Foreign KEY (Mgr_ssn) REFERENCES Employee(Ssn));
END 
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id= OBJECT_ID(N'[dbo].[dept_location]') AND type in (N'U'))
BEGIN

CREATE TABLE [dbo].[dept_location]
(Dnumber  INT NOT NULL,
 Dlocation  VARCHAR(15) NOT NULL,
 PRIMARY KEY(Dnumber,Dlocation),
 Foreign KEY(Dnumber) REFERENCES department(Dnumber));
END 
GO

IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id=OBJECT_ID(N'[dbo].[project]')AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Project]
(
Pname varchar(15) Not null,
Pnumber int not null,
Plocation varchar(15),
Dnum int not null,
Primary Key (Pnumber),
Unique (Pname),
Foreign key (Dnum) references Department(Dnumber));
End
GO
IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id=OBJECT_ID(N'[dbo].[Work_on]')AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Work_on]
(
Essn Char(9) not null,
Pno int not null,
Hours Decimal(3,1) not null,
Primary Key(Essn,Pno),
Foreign Key (Essn) References Employee(Snn),
Foreign Key (Pno) References Project(pnumber));
End
GO

IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id=OBJECT_ID(N'[dbo].[Dependant]')AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Dependant]
(
Essn Char(9) not null,
Deendant_name varchar(15) not null,
Sex char,
Bdate datetime,
Relationship varchar(8),
Primary Key (Essn,Dependant_name),
Foreign Key(Essn) References Employee(Ssn));
End

فيظهر لى الاخطا الاتية

Msg 1767, Level 16, State 0, Line 4

Foreign key 'FK__Employee__Dno__793DFFAF' references invalid table 'Department'.

Msg 1750, Level 16, State 0, Line 4

Could not create constraint. See previous errors.

Msg 1767, Level 16, State 0, Line 5

Foreign key 'FK__Departmen__Mgr_s__7D0E9093' references invalid table 'Employee'.

Msg 1750, Level 16, State 0, Line 5

Could not create constraint. See previous errors.

Msg 1767, Level 16, State 0, Line 5

Foreign key 'FK__dept_loca__Dnumb__7FEAFD3E' references invalid table 'department'.

Msg 1750, Level 16, State 0, Line 5

Could not create constraint. See previous errors.

Msg 1767, Level 16, State 0, Line 4

Foreign key 'FK__Project__Dnum__03BB8E22' references invalid table 'Department'.

Msg 1750, Level 16, State 0, Line 4

Could not create constraint. See previous errors.

Msg 1767, Level 16, State 0, Line 3

Foreign key 'FK__Work_on__Essn__0697FACD' references invalid table 'Employee'.

Msg 1750, Level 16, State 0, Line 3

Could not create constraint. See previous errors.

Msg 1911, Level 16, State 1, Line 4

Column name 'Dependant_name' does not exist in the target table or view.

Msg 1750, Level 16, State 0, Line 4

Could not create constraint. See previous errors.

لا اله الا انت سبحانك انى كنت من الظالمين

#2

انت هنا تنشأ الجداول مرة واحدة هذا خطأ لانه يوجد Foreign Key معتمدة على جدول لم يتم إنشاءه بعد

وبالتالي ينتج الاخطأ ( references invalid table )

نصحيتي اليك كتابة كل جدول على حدة مضاف اليها PRIMARY Key

ثم استخدم ALTER TABLE لاضافة ال constraint المناسب لكل جدول للتخطى (Could not create constraint)

تحياتي

وَعَبْدٌ قَدْ يَنامُ عَلَى حَرِيرٍ ××× وَذُو نَسَبٍ مَفَارِشُهُ التُّرَابُ

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…