Cursussen/Courses Codesnippets     Top 
SQL - SQL Data Definition


1. Database creation
Create and use a database.
CREATE SCHEMA IF NOT EXISTS `snippets` DEFAULT CHARACTER SET utf8 ;
USE `snippets` ;


2. Table creation
Create a table.
CREATE TABLE `category` (
  `id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `active` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


3. Primary Key
Change the structure of a table and add a primary key index.
ALTER TABLE `category`
  ADD PRIMARY KEY (`id`);


4. Auto increment
Modify a table and make the 'id' field 'auto-increment'. The 'id' field is incremented by the value 1 each time.
ALTER TABLE `category`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;


5. Delete table
Delete a table.
DROP TABLE `category`;