Share your experience about web programming. PHP Script,Java Script,Ajax and etc.
วันจันทร์ที่ 21 พฤษภาคม พ.ศ. 2555
Mysql command
# [mysql dir]/bin/mysql -h hostname -u root -p
Create a database on the sql server.
mysql> create database [databasename];
List all databases on the sql server.
mysql> show databases;
Switch to a database.
mysql> use [db name];
To see all the tables in the db.
mysql> show tables;
To see database's field formats.
mysql> describe [table name];
To delete a db.
mysql> drop database [database name];
To delete a table.
mysql> drop table [table name];
Show all data in a table.
mysql> SELECT * FROM [table name];
Returns the columns and column information pertaining to the designated table.
mysql> show columns from [table name];
Show certain selected rows with the value "whatever".
mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
Show all records containing the name "Bob" AND the phone number '3444444'.
mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.
mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
Show all records starting with the letters 'bob' AND the phone number '3444444'.
mysql> SELECT * FROM [table name] 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.
mysql> SELECT * FROM [table name] 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.
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
Show unique records.
mysql> SELECT DISTINCT [column name] FROM [table name];
Show selected records sorted in an ascending (asc) or descending (desc).
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
Return number of rows.
mysql> SELECT COUNT(*) FROM [table name];
Sum column.
mysql> SELECT SUM(*) FROM [table name];
Join tables on common columns.
mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
Change a users password from unix shell.
# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;
Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
Set a root password if there is on root password.
# mysqladmin -u root password newpassword
Update a root password.
# mysqladmin -u root -p oldpassword newpassword
Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;
Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
or
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
To update info already in a table.
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
Delete a row(s) from a table.
mysql> DELETE from [table name] where [field name] = 'whatever';
Update database permissions/privilages.
mysql> flush privileges;
Delete a column.
mysql> alter table [table name] drop column [column name];
Add a new column to db.
mysql> alter table [table name] add column [new column name] varchar (20);
Change column name.
mysql> alter table [table name] change [old column name] [new column name] varchar (50);
Make a unique column so you get no dupes.
mysql> alter table [table name] add unique ([column name]);
Make a column bigger.
mysql> alter table [table name] modify [column name] VARCHAR(3);
Delete unique from table.
mysql> alter table [table name] drop index [colmn name];
Load a CSV file into a table.
mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
Dump all databases for backup. Backup file is sql commands to recreate all db's.
# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
Dump one database for backup.
# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
Dump a table from a database.
# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
Restore database (or database table) from backup.
# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
Create Table Example 1.
mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname 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,pgpemail VARCHAR(255));
Create Table Example 2.
mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');
SQL Command part2 (STOU 96408)
---กิจกรรมที่ 5.1------------
-- 2.DML
/*
update,delete,select
*/
select * from customer;
--การแก้ใขข้อมูลในตาราง
update customer set cus_lname='pothong' where cus_code='10010';
update employee set salary=salart*1.5;
select * from pilot;
select * from employee;
--การลบข้อมูลในตาราง
delete from employee where emp_fname like 'k%';
----------------------------------------------------
--5.1
--select * from employee where emp_fname='jitrlada';
delete from employee where emp_fname='Jitrlada' AND emp_lname='Polnimit';
delete from employee where emp_num='106';
--5.2
delete from customer where cus_code='10018';
--5.3
update customer set cus_phone='988-2588' where cus_fname='Jittinan' AND cus_lname='pothong';
--5.4
update customer set cus_balance=45650.75 where cus_fname='Pong' AND cus_lname='Smith';
--5.5
update pilot set PIL_MED_TYPE='2' where pilot.emp_num=employee.emp_num AND employee.emp_fname='George';
update pilot set PIL_MED_TYPE='1' where pilot.emp_num = (select emp_num from employee where employee.emp_fname='George');
create database aircraft52
--5.2 select
select * from employee;
select emp_num,emp_fname,emp_lname from employee order by emp_fname;
select emp_num,emp_fname,emp_lname from employee order by 2;
select emp_num,emp_fname,emp_lname from employee order by 2 desc; --เรียงจากมากไปน้อย
select cus_fname,cus_lname,cus_balance from customer where cus_balance > 30000 order by 3 desc;
select cus_fname,cus_lname,cus_balance from customer
where cus_balance between 20000 and 30000 order by 3 desc;
--5.2.3
/* แสดงหมายเลขสัญญาเช่า จุหมายปลายทางของเครื่องบินที่ไปลงที่ atlanta และจำนวนชั่วโมงที่พักเครื่อง
มากกว่าหรือเท่ากับ 3.5
*/
select char_num,char_destination from charter
where char_destination = 'ATL' and char_hours_wait >=3.5;
--5.2.1
select * from charter order by char_date desc;
--5.2.2
select * from charter where char_date between '02/13/2003' and '02/14/2003' ;
--5.2.3
select char_date,char_destination,char_distance,char_hours from charter
where ac_num='2778V';
--5.2.4
select cus_fname,cus_lname,cus_phone,cus_balance from customer
where cus_balance > 0;
--5.2.5
select emp_title,emp_fname,emp_lname from employee,pilot
where pilot.emp_num=employee.emp_num and pilot.pil_license='COM' ;
/************************
ให้แสดง หมายเลขสัญญาเช่า ชื่อลูกค้า ของลูกค้าที่ชื่อ jittinan
*************************/
select char_num,cus_fname from charter,customer
where charter.cus_code=customer.cus_code and cus_fname = 'jittinan';
--5.2.6
select char_date,char_destination,ac_num,cus_fname,cus_lname,cus_areacode,cus_phone from charter,customer
where charter.ac_num='2778v' and charter.cus_code=customer.cus_code;
--5.2.7
select cus_fname,cus_lname,cus_areacode,cus_phone from customer,charter
where customer.cus_code=charter.cus_code and charter.ac_num='2778V' and customer.cus_balance > 30000;
--5.2.8
select char_date,cus_fname,cus_lname,cus_areacode,cus_phone from charter,customer
where charter.cus_code=customer.cus_code and cus_fname like '_i%';
--5.2.9
select char_num,char_date,char_destination,char_hours,mod_name,charter.ac_num from charter,aircraft,model
where charter.ac_num = aircraft.ac_num and aircraft.mod_code=model.mod_code and co_pil_num <> '';
-- 5.2.10
select char_date,ac_num,emp_fname,emp_lname from charter,employee,pilot
where charter.ac_num='2778V' and charter.pil_num=pilot.pil_num and pilot.emp_num=employee.emp_num;
-- 5.2.11
select char_date,char_num,charter.ac_num,mod_name from charter,aircraft,model
where charter.ac_num = aircraft.ac_num and aircraft.mod_code=model.mod_code and char_date >= '02/15/2003';
--5.2.12
select char_date,char_destination,charter.ac_num,mod_chg_mile,char_distance,cus_fname,cus_lname
from charter,customer,aircraft,model
where char_destination='ATL' and charter.ac_num = aircraft.ac_num and aircraft.mod_code=model.mod_code
and charter.cus_code=customer.cus_code;
--5.2.13
select char_date,charter.ac_num,emp_fname,char_hours,char_fuel,charter.char_fuel/char_hours as fuelPerHours from charter,pilot,employee,aircraft
where char_date >= '02/15/2003' and charter.ac_num=aircraft.ac_num
and charter.pil_num=pilot.pil_num and pilot.emp_num=employee.emp_num
--5.2.14
select char_date,char_distance,mod_chg_mile,mod_chg_mile*char_distance as mileageChange from charter,model,aircraft
where char_date >='02/14/2003' and charter.ac_num=aircraft.ac_num and aircraft.mod_code=model.mod_code;
--5.2.15 *******
select char_date,char_distance
from charter,customer
where char_date >='02/16/2003' and charter.cus_code=customer.cus_code;
--5.2.16
select avg(cus_balance) as SumOfBalance,
min(cus_balance) as MinBalance,
max(cus_balance) as MaxBalance,
sum(cus_balance) as SumBalance
from customer;
--5.2.17
select ac_num,sum(char_distance)as'Number of Flow',
count(ac_num) as 'Total Distanct',sum(char_hours)as 'Total Hours'
from charter
group by ac_num ;
--5.2.18
select char_destination,count(char_destination) as 'charter amout',min(char_hours)as'Minimum Hours'
from charter
group by char_destination order by count(char_destination) desc ;
--5.2.19
select ac_num,ac_ttel,mod_name,mod_chg_mile as 'change per mile'
from aircraft,model
where mod_chg_mile < 100 and aircraft.mod_code=model.mod_code ;
--5.2.20
select ac_num,count(ac_num)as 'Number of Flow',max(char_hours)as 'MaxIMUM HOURS'
from charter
group by ac_num having count(ac_num) > 4;
SQL Command Part1
--1. DDL
--create database Testaircraft; --create database
-- การสร้าง Table
create table test1
( T_ID char(4) not null primary key,
T_Name char(40) not null,
T_salary int not null
);
-- ดูรายละเอียด ของตาราง
sp_help test1;
---------------
create table test2
( T_ID char(4) not null,
T_code char(3) not null,
T_salary int not null,
primary key (T_ID,T_code)
);
sp_help test2;
-- สร้าง fk
create table test3
( T_ID char(4) not null primary key,
T_code char(3) not null references test4(T_code),
T_salary int not null
);
create table test4
( T_code char(3) not null primary key,
T_Name char(3) not null,
T_salary int not null
);
------------------------------------------
create table test11
( T_ID char(4) not null primary key,
T_name char(4) not null
);
create table test12
( T_Code char(3) not null primary key,
T_name char(4) not null
);
create table test13
( T_Des int not null primary key,
T_name char(4) not null
);
create table test5
( T_ID char(4) not null references test11(T_ID),
T_Code char(3) not null references test12(T_Code),
T_name char(3) not null,
T_Des int not null references test13(T_Des),
primary key(T_ID,T_Code)
);
sp_help test5;
-------------------------------------
drop table test5;
------ เปลี่ยนชื่อตาราง--------
sp_rename 'test1','Exam1';
------ เปลี่ยนชื่อ arrtibute--------
sp_rename 'Exam1.T_name','T_surname';
-------------------------------------
drop table test11;
---- การเปลี่ยนแปลงแก้ใขโครงลร้าง Table
create table Ex1
( E_ID char(4) not null,
E_name varchar(40) not null
);
alter table Ex1 add primary key(E_ID); -- add primary key in table ex1
alter table Ex1 add constraint PK_E_ID primary key(E_ID); -- add primary key in table ex1
alter table Ex1 drop constraint PK__Ex1__2C3393D0; -- add primary key in table ex1
sp_help Ex1;
alter table Ext add E_slary int; -- add colum E_slary value by int
--------------------------------------------------------------------------
-- DML data manipulation language
/*
insert ,update ,delete,select
*/
create table Ex2
(
--E_ID char(4) not null check(E_ID like 'E?[a-z][0-9]') primary key,
E_ID char(4) not null check(E_ID like 'EA[0-9][0-9]') primary key,
E_Name varchar(30) not null,
E_BD datetime,
E_salary int check(E_Salary > 5000)
);
drop table Ex2;
--การ เพิ่มข้อมูล
insert into Ex2 values('EA01','Pirom konglerd','3/26/2011',50001);
insert into Ex2 values('EA02','rom konglerd','3/26/2011',50001);
insert into Ex2 (E_ID,E_Name)values('EA03','rom konglerd');
set dateformat dmy; -- set format date
select * from Ex2;
update Ex2 set E_salary=6000;
-- DCL
/*
grant revoke
examp:
create user pirom set password 123456
*/
สมัครสมาชิก:
บทความ (Atom)