mysql 练习(有空就练)
mysql -h192.168.137.10 -uroot -p123
mysql -uroot -p123
mysqladmin -uroot -p password "redhat"
mysqladmin -uroot -predhat password "123"
grant all on *.* to zhang@"%" identified by "redhat";
mysql -uzhang-predhat
create database luzhi default character set utf8;
create database luzhi1 character set utf8;
drop database luzhi;
drop database if exists luzhi1;
create database if not exists luzhi444;
drop database if exists luzhi444;
use mysql;
show tables;
select * from user;
desc user;
select database();
select version();
select dayofmonth(current_date);
mysql>use mytest;
mysql> create table myclass(
-> id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> sex int(3) not null default 0,
-> degree double(16,2));
mysql> insert into MyClass values(1,‘Tom‘,96.45),(2,‘Joan‘,82.99), (2,‘Wang‘, 96.59);
select * from MyClass;
select * from MyClass order by id limit 0,2;
mysql> delete from MyClass where id=1;
php操作:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName=‘Griffin‘"); mysql_close($con);
?>
mysql> update MyClass set name=‘Mary‘ where id=1;
mysql> alter table MyClass add passtest int(4) default ‘0‘;
mysql> rename table MyClass to YouClass;
create user guest@localhost identified by ’123456′;
grant select on mydb.* to guest@localhost;
drop user guest;
grant select, insert, update, delete on new_db.* to guest@’%’ identified by ’88888888′;
实例1
drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default ‘深圳’,
year date
); //建表结束
//以下为插入字段
insert into teacher values(”,’allen’,‘大连一中’,‘1976-10-10′);
insert into teacher values(”,’jack’,‘大连二中’,‘1975-12-23′);
如果你在mysql提示符键入上面的命令也可以,但不方便调试。
1、你可以将以上命令原样写入一个文本文件中,假设为school.sql,然后复制到c:\\下,并在DOS状态进入目录[url=file://\\mysql\\bin]\\mysql\\bin[/url],然后键入以下命令:
mysql -uroot -p密码 < c:\\school.sql
如果成功,空出一行无任何显示;如有错误,会有提示。(以上命令已经调试,你只要将//的注释去掉即可使用)。
2、或者进入命令行后使用 mysql> source c:\\school.sql; 也可以将school.sql文件导入数据库中。
实例2
drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default ‘‘深圳‘‘,
year date
); //建表结束
//以下为插入字段
insert into teacher values(‘‘‘‘,‘‘glchengang‘‘,‘‘深圳一中‘‘,‘‘1976-10-10‘‘);
insert into teacher values(‘‘‘‘,‘‘jack‘‘,‘‘深圳一中‘‘,‘‘1975-12-23‘‘);
注:在建表中
1、将ID设为长度为3的数字字段:int(3);并让它每个记录自动加一:auto_increment;并不能为空:not null;而且让他成为主字段primary key。
2、将NAME设为长度为10的字符字段
3、将ADDRESS设为长度50的字符字段,而且缺省值为深圳。
4、将YEAR设为日期字段。
本文出自 “天道酬勤” 博客,请务必保留此出处http://luzhi1024.blog.51cto.com/8845546/1655472
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。