Jakie dane mogę wstawić do wiesza?

0

Nie chce dodać mi wiersza. Wyskakuje mi błąd

Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.
 

Kod który tworzy tabelę.

 
CREATE TABLE employee 
(
   emp_id  varchar
      CONSTRAINT PK_emp_id PRIMARY KEY NONCLUSTERED
      CONSTRAINT CK_emp_id CHECK (emp_id LIKE 
         '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][F]' or
         emp_id LIKE '[A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][M]'),
      /* Each employee ID consists of three characters that 
      represent the employee's initials, followed by a five 
      digit number ranging from 10000 through 99999 and then the 
      employee's gender (M or F). A (hyphen) - is acceptable 
      for the middle initial. */
   fname   varchar(20)     NOT NULL,
   minit   char(1) NULL,
   lname   varchar(30)     NOT NULL,
   job_id  smallint        NOT NULL
      DEFAULT 1
      /* Entry job_id for new hires. */
      REFERENCES jobs(job_id),
   job_lvl tinyint
      DEFAULT 10,
      /* Entry job_lvl for new hires. */
   pub_id  char(4) NOT NULL
      DEFAULT ('9952')
      REFERENCES publishers(pub_id),
      /* By default, the Parent Company Publisher is the company
      to whom each employee reports. */
   hire_date       datetime        NOT NULL
      DEFAULT (getdate())
      /* By default, the current system date is entered. */
)

Jak chciałem wstawić dane do wiersza.

 
INSERT INTO employee (emp_id, fname, minit, lname, job_id, job_lvl, pub_id) 
VALUES ('AAD10000F', 'Bartek', 'W', 'Ole', 1, 8, 56); 
0

Na oko to do pub_id powinieneś przekazać ciąg znaków, a nie liczbę.

0

Faktycznie ale i tak dalej ten sam błąd

 
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.
0
emp_id VARCHAR

bez podania w nawiasie wartości rozmiaru tupu tworzy emp_id VARCHAR(1)

 dlatego dostajesz taki komunikat o błedzie, Musisz podać rozmiar typu Varchar.
0

Po zmianie na "emp_id varchar(50)" wyskakuje błąd

 
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__employee__job_id__398D8EEE". The conflict occurred in database "Moja_baza", table "dbo.jobs", column 'job_id'.
The statement has been terminated.

Próbuje dodać wiersz tak

 
INSERT INTO employee (emp_id, fname, lname, job_id, pub_id) 
VALUES ('AAA11111F', 'Bartek', 'Ole', 1, '54'); 
0

Wygląda na to, jak byś miał róże typy danych do klucza job_id w tabeli employee i tabeli dbo.jobs job_id ma pewnie inny typ, lub wstawiasz wartość job_id, która nie istnieje w tabeli [dbo].[jobs]

0

bo pewnie w jobs job_id też jest typu varchar

0

Tabela dbo.jobs nie posiada w kolumnie job_id wartości 1.

Może spróbuj przejść jakiś prosty tutorial dotyczący SQL zamiast na chybił trafił próbować wstawiać wartości. W bazie pubs pole job_id jest typu smallint.

1 użytkowników online, w tym zalogowanych: 0, gości: 1