I need to delete all the records of a SQLServer table without indicating which one I want to delete, I want them to be all and this code deletes data in specific
delete from tbl_usuarios where nombre='Abi';
I need to delete all the records of a SQLServer table without indicating which one I want to delete, I want them to be all and this code deletes data in specific
delete from tbl_usuarios where nombre='Abi';
Of the query that you are doing you can remove from where onwards
Your code:
delete from tbl_usuarios where nombre='Abi';
Your modified code:
delete from tbl_usuarios;
Another option is to use TRUNCATE
TRUNCATE TABLE tbl_usuarios;
But it is important that you know the differences between delete and truncate , here I share a table of the differences (Source: Differences between delete and truncate )
It is enough to remove the WHERE
that is the basic condition with:
DELETE FROM tbl_usuarios
Source: link
Just put DELETE FROM *Nombre_de_tu_tabla*
TRUNCATE TABLE tbl_usuarios
I would serve you.
You can use the Truncate code, you have to take into account that if you use it you should verify that the table where you will make Truncate does not have Foreline Key constraints, if this is the case you should remove the relationships that affect it before to perform Truncate, if it is a table that is referenced by others through a foreign key, you should check if the impact of deleting the records in the table does not affect, and if this is the case, it establishes the cascade elimination only for these effects, next I show you an example;
This table is the parent table
CREATE TABLE Prueba
(
Id int IDENTITY(1,1) NOT NULL,
Descripcion varchar(50) NOT NULL,
CONSTRAINT [PK_id] PRIMARY KEY CLUSTERED
(
Id ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
This is the daughter table
CREATE TABLE Detalle
(
Id_detalle int IDENTITY(1,1) NOT NULL,
Id int NOT NULL
CONSTRAINT PK_Detalle PRIMARY KEY CLUSTERED
(
Id_detalle ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON PRIMARY
) ON PRIMARY
GO
ALTER TABLE FK_MiTablaHija_RegistroTablaPadreDebeExistir WITH CHECK ADD CONSTRAINT FK_MiTablaHija_RegistroTablaPadreDebeExistir FOREIGN KEY(Id)
REFERENCES Prueba (Id)
GO
ALTER TABLE Detalle CHECK CONSTRAINT FK_MiTablaHija_RegistroTablaPadreDebeExistir
GO
The detail table refers to the ID of the test table, which corresponds in this case to facilitate the cascading elimination, that is, if a record is deleted in the Test table, all records will be deleted in the tables that refer to this field in the table Test, first you must modify the Constraint before, and modify its action in elimination and if you want in update, for demonstration purposes I will place the constraint including action when updating (although here is not the case ),
Alter Table Detalle
Drop Constraint FK_MiTablaHija_RegistroTablaPadreDebeExistir
GO
After removing the Constraint you proceed to create the new ...
Alter Table Detalle
Add Constraint FK_MiTablaHija_RegistroTablaPadreDebeExistir Foreign Key (CampoHijo) References MiTablaPadre(CampoPadre) On Update Cascade On Delete Cascade
After this you can perform without any problem Truncate in the table and will not generate an error, this is in development environment and not in production (the customer database with your information and interacting with users through of an application), then you can return the elimination policies to do nothing when deleting as they are by default doing the same procedure to delete the modified Constraint ...
Alter Table Detalle
Drop Constraint FK_MiTablaHija_RegistroTablaPadreDebeExistir
GO
Alter Table Detalle
Add Constraint FK_MiTablaHija_RegistroTablaPadreDebeExistir Foreign Key (CampoHijo) References MiTablaPadre(CampoPadre) On Update No action On Delete No action
Ready ... here you already returned the relationship as it was before performing Truncate ...
This link can help you see what is written above ...