SQL Cheat Sheet: Database and Table Commands in MySQL

Arts and Humanities3.81 KB

SQL Commands for Databases and Tables

Create and Use a Database

Create and use a database:

  • create database Nombretabla;
  • use Nombretabla;

Delete and View Databases

  • Delete a database: drop database Nombrebbdd;
  • View databases: show databases;

Table Structure and Data

  • View table information and columns: desc Nombretabla;
  • View table data: select * from Nombretabla;
  • Delete a table: drop table Nombretabla;

Modify Column Order and Constraints

  • Move a column to first position: alter table nombretabla modify Nombreytipo first; (places it in 1st position)
  • Move a column after another: alter table nombretabla modify Nombreytipo after nombrecampo; (places it after...)
  • Set a field as NOT NULL after creation: alter table nombretabla modify Nombrecampoytipo not null;
  • Set a default value for a field: alter table nombretabla modify Nombrecampoytipo default 'valorquieropordefecto';

Rename, Add and Drop Columns

  • Rename a column: alter table Nombretabla change Nombrecolumna nombrequequieroponer tipo;
  • Set a field as primary key: alter table Nombretabla add primary key (nombrecampoquequierocomoclave);
  • Add a foreign key to an existing table: alter table Nombretabla add foreign key (Nombrecampo) references Nombretabla (Nombrecampo);
  • Add a column: alter table Nombretabla add (Nombrecampo y tipo);
  • Delete a column: alter table Nombretabla drop Nombrecampo;
  • Delete a foreign key: alter table Nombretabla drop foreign key Nombretabla_ibfk_1;

Insert, Update and Delete Records

  • Insert a record into a table: insert into Nombretabla values ('registroainsertar','registroainsertar',........);
    Records must be inserted in the same order the table was created.
  • Insert a record without entering fields in order: insert into Nombretabla (Nombrecampo1, Nombrecampo2, Nombrecampo3) values ('registrodelcampo1', registrodelcampo2, registrodelcampo3);
  • Modify a record in a table: update Nombretabla set Nombrecampo= where Nombrecampo= ;
  • Delete a record from a table: delete from Nombretabla where Nombrecampo='';

Create Tables with Keys

  • Create a table with a composite primary key and one foreign key:
    create table Nombretabla (Nombrecampo1 y tipo, Nombrecampo2 y tipo, Nombrecampo3 y tipo, Nombrecampo4 y tipo, foreign key (Nombrecampo4) references Nombretabla (Nombrecampo), primary key (Nombrecampo1, Nombrecampo2, Nombrecampo3)) engine=innodb;
  • Create a table with two foreign keys that together form a composite primary key:
    create table Nombretabla (Nombrecampo1 y tipo, Nombrecampo2 y tipo, primary key (Nombrecampo1, nombrecampo2), foreign key (Nombrecampo1) references Nombretablaalaquehacereferencia (Nombrecampo), foreign key (Nombrecampo2) references Nombretablaalaquehacereferencia (Nombrecampo)) engine=innodb;
  • Create an auto_increment field:
    create table Nombretabla (Nombrecampo1 y tipo auto_increment primary key, Nombrecampo2 y tipo) engine=innodb;

Download

PDF Word HTML