2024: Domino Containers - The Next Step. News from the Domino Container commu...
Â
Ddl commands
1. A. DDL Commands on MySQL:
ï Create a database
Create database [database name];
ï To delete a db.
Drop database [database name];
ï To delete a table.
Drop table [table name];
ï Delete a column.
Alter table [table name] drop column [column name];
ALTER TABLE employee DROP INDEX name_dept; - get rid of
ï Add a new column to db.
Alter table [table name] add column [new column name] varchar (20);
ALTER TABLE employee ADD EmpId INT NOT NULL AUTO_INCREMENT PRIMARY
KEY;
ï Change column name.
Alter table [table name] change [old column name] [new column] varchar (50);
ï Make a unique column so you get no dupes.
Alter table [table name] add unique ([column name]);
ï Make a column bigger.
2. Alter table [table name] modify [column name] varchar (3);
ï Delete unique from table.
Alter table [table name] drop index [column name];
ï Delete data from specific fields within a column:
Mysql> DELETE FROM mytable WHERE mycolumn="mydata";
ï Rename a table within a specific database
Mysql> RENAME TABLE first TO second;
(Or)
Mysql> ALTER TABLE mytable rename as mynewtable;
ï Create Table Example 1.
Create table [table name] (first name varchar(20),middleintial varchar(3),last
name varchar(35),suffix varchar(3),officeid varchar(10),userid
varchar(15),username varchar(8),email varchar(35),phone varchar(25),groups
varchar(15),datestamp date,timestamp time,pagemail varchar(255));
CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species
VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
CREATE TABLE retired_employee (Name char(20) DEFAULT '' NOT NULL,Dept
char(10) DEFAULT '' NOT NULL,JobTitle char(20)UNIQUE name_dept
(Name,Dept));
CREATE UNIQUE index name_dept on employee (name,dept); - avoids duplicate
keys
ï Create Table Example 2.
Create table [tablename] (personid int(50) not null auto_increment primary
key,firstname varchar(35),middlename varchar(50),lastname char(50) default
âbatoâ;
3. Create table employees (employee_id integer, first_name varchar (20), last_name
Varchar (20)) type=MyISAM;
ï Drop Database
Drop database databasename;
ï Delete a row(s) from a table.
DELETE from [tablename] where [fieldname]=âwhateverâ;
ï DROP TABLE
DROP TABLE TABLENAME;
ï Simple column and field manipulation
Show columns within a table:
Mysql> DESC mytable;
(Or)
Mysql> SHOW COLUMNS FROM mytable;
B. DML Commands :
ï Show all data in a table
SELECT * FROM [TABLENAME];
ï Show certain selected rows with the value âwhateverâ
SELECT * FROM [tablename] where [field] =âwhateverâ;
ï Show all records containing the name "Bob" AND the phone number '3444444'.
SELECT * FROM [TABLENAME] WHERE NAME=âbobâ AND
phone_number=â3444444â;
4. ï Show all records not containing the name "Bob" AND the phone number
'3444444' order by the phone_number field.
SELECT * FROM [TABLENAME] WHERE NAME! =âbobâ and
phone_number=â3444444â order by phone_number;
ï Show all records starting with the letters 'bob' AND the phone number '3444444'.
SELECT * FROM [TABLENAME] WHERE NAME LIKE âbob%â and
phone_number=â3444444â;
ï Show all records starting with the letters 'bob' AND the phone number '3444444'
limit to records 1 through 5.
SELECT * FROM [TABLENAME] WHERE NAME LIKE âbob%â and
phone_number=â3444444â limit 1,5;
ï Use a regular expression to find records. Use "REGEXP BINARY" to force case-
sensitivity. This finds any record beginning with a.
SELECT * FROM [TABLENAME] WHERE rec RLIKE â^aâ;
ï Show unique records.
SELECT DISTINCT [column name] FROM [TABLE NAME];
ï Show selected records sorted in an ascending (asc) or descending (desc).
Select [col1], [col2] from [tablename] order by [col2] desc;
ï Return number of rows.
SELECT COUNT (*) FROM [TABLENAME];
ï Is there a way to query the DB to find out how many rows there are in all the
tables?
SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE
`table_schema` = 'mrsqa4';
5. ï Is there a way to get the count of rows in all tables in a mysql database without
running a SELECT count () on each table?
SELECT SUM (TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLESWHERE
TABLE_SCHEMA = 'mrsqa4';
ï Sum column.
SELECT SUM (*) FROM [TABLENAME];
ï Show the last 200 queries to your database with the sample table name "queries"
and the sample field "query_id"
Mysql> SELECT * FROM queries ORDER BY query_id DESC LIMIT 200;
ï Show data within a specific table in a previously selected database
Mysql> SELECT * FROM table name;
ï Select data within a specific table in a previously selected database:
Mysql> SELECT * FROM mytable WHERE mycolumn='mydata' ORDER BY
mycolumn2;
ï Join tables on common columns.
Select lookup.illustrationid, looup.personid,person.birthday from lookup
Left join person on lookup.personid=person.personid=statement to join
Birthday in person table with primary illustrationid;
MySQL> SELECT DISTINCT dept FROM bedrock;
MySQL> SELECT * FROM pet WHERE species = "snake" OR species = "bird";
MySQL> SELECT * FROM pet WHERE species = "dog" AND sex = "f";
MySQL> SELECT * FROM pet WHERE birth >= "1998-1-1";
MySQL> SELECT name, birth FROM pet ORDER BY birth DESC;
MySQL> SELECT * FROM pet WHERE name LIKE "b%";
MySQL> SELECT * FROM pet WHERE name REGEXP "^b";
6. MySQL> SELECT species, COUNT(*) FROM pet GROUP BY species;
MySQL> SELECT MAX(article) AS article FROM shop;
MySQL> SELECT * FROM employee WHERE name LIKE "%Sm%";
MySQL> SELECT * FROM employee WHERE name REGEXP "^Ja";
Insert:
MySQL> INSERT INTO employee VALUES ("Jane Smith","Sales","Custome Rep");
MySQL> INSERT INTO employee VALUES ('Jane Smith','Sales','Account Manager');
MySQL> INSERT INTO employee VALUES ('Jane smithâ,âEngineeinâ,âManagerâ);
MySQL> UPDATE employee SET dept='HR' WHERE name='Jane Smith';
ï Insert data into a table and its columns within a specific database:
mysql> INSERT INTO mytable VALUES('column1data','column2data',
'column3data','column4data','column5data','column6data',
'column7data','column8data','column9data');
ï Update data in a specific table, column, field:
Mysql> UPDATE mytable SET column1="mydata" WHERE column2="mydata";