Вы находитесь на странице: 1из 2

mysql -u mysql -p -D test #login with password prompt

mysql --user=user --password=password -Dtest #login derictly into mysql>


safe_mysqld --skip-grant-tables #load DB without grant table
mysqldump --user=username --password=pass --tab=C:/ --opt weddinglist
BACKUP TABLE tb_info,tb_main TO 'D://mysql_backup/'; #create tables backups into
bin files
RESTORE TABLE tb_info FROM 'D://mysql_backup/';
OPTIMIZE TABLE 'tb_name';
SELECT BENCHMARK(1000000,10*100); # check 10*100 operation * milion times
ANALYZE TABLE tb_name #for avoiding table scan and better preformance
USE database_name; #will take to into the selected database
SHOW databases; #show databases
SHOW tables; #showing tables within the database
SHOW index tb_name; #showing indexes
SHOW culom tb_name; #
SHOW field tb_name; #
SHOW VARIABLES;
SHOW grants for root; #show user grants
SHOW index from tb_info;
DESCRIBE tb_name #showing all tables fields with thier data type
DESC tb_name; #showing all tables fields with thier data type
LOAD DATA INFILE 'C:\\tb_info.txt' INTO TABLE tb_info (name_id,address,info);
#will LOAD/ADD text file by order of giving fields (spearetor=tab)
LOAD DATA INFILE 'C:\\tb_info.txt' INTO TABLE tb_info FIELDS TERMINATED BY '\t';
#The same
LOAD DATA INFILE 'C:\\tb_info.txt' INTO TABLE tb_info FIELDS TERMINATED BY '|';
#The same but field seapretor = |
LOAD DATA INFILE 'C:\\tb_info.txt' INTO TABLE tb_info FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'; #the same
LOAD DATA INFILE 'data.txt' IGNORE INTO TABLE my_table FIELDS TERMINATED BY '\t'
ENCLOSED BY '' ESCAPED BY '\\'
LOAD DATA INFILE 'C:\\E164_PSTN_partnerco7489_csv_v0802.csv' IGNORE INTO TABLE
wnnp FIELDS TERMINATED BY ';';
SELECT * from tb_info;
SELECT * from tb_info where name like 'L%';
SELECT count(*) name from tb_info; OR select count(*) * from tb_info; #for
counting recoreds
SELECT COUNT(DISTINCT phone) FROM tb_info;
SELECT DISTINCT phone FROM tb_info;
SELECT * FROM tb_info group by name;
SELECT * FROM tb_info order by name;
SELECT * FROM tb_info LIMIT 0,10; #Will give us result from 0(the first one) till
the 10
SELECT m.name,m.phone,i.address from tb_info i,tb_main m where
(i.name_id=m.name_id); #Join two tables
SELECT name,address from tb_info LEFT JOIN tb_main ON
tb_info.name_id=tb_main.name_id; #Join two tables
SELECT * from tb_info LEFT JOIN tb_main ON tb_info.name_id=tb_main.name_id;
SELECT sum(coming) from name; #will count from field name coming
SELECT distinct category from name; #give you distinct data from table name for
culom category
UPDATE tb_info set phone=NULL where name="name";
UPDATE user set password="mysql" where user="mysql";
INSERT tb_name values ("Bubi",'000010',NULL);
INSERT INTO tb_info(name,phone,name_id) VALUES("Moshe",'0',"000015");
INSERT INTO temp SELECT * FROM tb_info; #Will cpoy into predifined tbale "temp"
the tb_info data (have to match data types and field location)
INSERT INTO tb_info (name_id,info,address) values('000001',"A great man","Cfar
Sircin"); #Another example
CREATE INDEX name_Index ON tb_info (name);
CREATE table temp (name varchar(20),name_id varchar(6),phone int(11)); #creates
table
CREATE TABLE Customer (
number VARCHAR(40) NOT NULL,
name VARCHAR(100) NOT NULL,
ssn VARCHAR(50) NOT NULL,
age INTEGER NOT NULL,
CONSTRAINT cust_pk PRIMARY KEY (number),
UNIQUE ( ssn ), CONSTRAINT age_check CHECK (age >= 0 AND age < 200)
);
CREATE TABLE tb_new AS SELECT * FROM tb_old where 1=0; #Create new table with the
same fields and definitions like old
DELETE from tb_name where name="Bubi";
DELETE tb_info from tb_info; #Will delete the all entries within the table !!!!!
(use truncate insted)
ALTER TABLE before_name RENAME after_name; #To rename the table from
before_name to after_name:
ALTER TABLE tb_info modify address CHAR(40); #Will change the data type into
CHAR(40)
ALTER TABLE name add ref int; #will ADD colum name ref with data type
integer
TRUNCATE table tb_info
DROP table temp;
DROP database;
DROP INDEX name_index ON tb_list;
SELECT * INTO OUTFILE 'C:\\data.txt' FIELDS TERMINATED BY '|' FROM tb_main; #Write
file with selected data
SELECT * INTO OUTFILE 'C:\\wedding-out.TXT' FIELDS TERMINATED BY "\t" LINES
TERMINATED BY "\n" FROM name; #the same but with tab-delimeter
delete from ivr where (length(phone) < 3);

### STORED PROCEDURES - Start ###


DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_get` $$
CREATE PROCEDURE `sp_get`()
BEGIN
select * from mytable;
END $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_deleteUserInfo` $$
CREATE PROCEDURE `sp_deleteUserInfo`($userID INT)
BEGIN
DELETE FROM mytable WHERE (ID = $userID);
END $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_insertUserInfo` $$
CREATE PROCEDURE `sp_insertUserInfo`(userID INT,userValue INT, userName
VARCHAR(45))
BEGIN
INSERT INTO mytable (id,value,name) values (userID,userValue,userName);
END $$
DELIMITER ;