SQL veateated ja nende lahendamine

Veateated ja nende lahendamine

SQL SERVER

1.ebakorrektne SQL lause süntaks.

lahendus:

lisaga andmebaasi nimi:

create database veateade;

saama viga XAMPP’is

lahendus:

lisaga andmebaasi nimi:

create database veateade;

2. Invalid column name ‘test’.

puudub tabel nimega test
viigane SQL : Insert into test2(id,test) values (1,’t’)

lahendus:

create table test2;

Insert into test2(id,test) values (1,’t’)

saama viga XAMPP’is

lahendus:

create table test2;

Insert into test2(id,test) values (1,’t’)

3.String or binary data would be truncated in table ‘veateade.dbo.test’,
column ‘nimi’. Truncated value: ‘t’.

–oli salvestatud varchat(1) aga me sisestame ‘test’

õige SQL:
ALTER TABLE test alter column nimi varchar(25);
Insert into test(id, nimi) VALUES (1, ‘test’);

saama viga XAMPP’is

lahendus:

Create table test(
id int Primary Key AUTO_INCREMENT,
nimi varchar(30));

4.Cannot drop the table ‘test’ because it is referenced by a FOREIGN KEY constraint

Katse kustutada parent_table’i, millele viitab lapstabeli poolt viidatud tabel

lahendus:

— Kustuta kõigepealt tütardtabel või võõrvõti
ALTER TABLE child_table DROP CONSTRAINT FK__child_table__parent_id;

— Nüüd saame kustutada vanemtabeli
DROP TABLE parent_table;

5.Must declare the scalar variable “@rtest”

lahendus

create procedure test2
@ltest1 varchar(30),
@rtest1 int
as
begin
insert into okk(okNimi, kkunt)
values (@ltest1,@rtest1);
end;

6.Conversion failed when converting the varchar value ‘abc’ to data type int.

lahendus:

— Parandada ’id’ andmetüüpi, sisestades stringi asemel täisarvu.
INSERT INTO test (id, nimi) VALUES (1, ‘some_value’);

7.There is no unique constraint matching the given keys for the referenced table ‘parent_table’

lahendus:

— Loo unikaalne võti tabeli parent_table veerus ’id’.
ALTER TABLE parent_table ADD CONSTRAINT UQ_parent_table_id UNIQUE(id);

— Nüüd töötab välisvõti
CREATE TABLE child_table (
id INT,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parent_table(id)
);

saama viga XAMPP’is

8.Cannot execute a SELECT statement in a stored procedure without an explicit result set.

The error occurs when SELECT is executed in a stored procedure, but the result is not returned explicitly(SQL veateated ja nende lahendamine)

lahendus:

— Lisage RETURN avaldis või väljastage andmed PRINT-iga.
LOODA PROKURSUS GetTestData
AS
BEGIN
SELECT * FROM test;
RETURN 0;
END;

saama viga XAMPP’is

9.The column ’id’ cannot be modified because it is a computed column

The error occurs when you try to change a calculated column, which is impossible because its value depends on other columns.

lahendus:

— Eemaldage veerg ’id’ ja lisage see uuesti arvutatud väärtusega.
ALTER TABLE test DROP COLUMN id;
ALTER TABLE test ADD id INT;
ALTER TABLE test ADD computed_column AS (id * 2);

10.There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.(SQL veateated ja nende lahendamine)

Selgitus: Sisestatud väärtuste arv ei vasta tabeli määratlusel olevate veergude arvule.

lahendus:

INSERT INTO test(id, nimi) VALUES (1, ‘t’);

saama viga XAMPP’is