Mysql 基础操作教程

来源:互联网 发布:sql联合主键语句 编辑:程序博客网 时间:2024/06/15 21:38

1登录Mysql

启动   /etc/init.d$ mysql start

登录   /mysql/bin$ mysql -u root -p

mysql -D 所选择的数据库名 -h 主机名 -u 用户名 -p

 

2.show databases 显示现在所有的数据库

3.win 下用navicat 来连接虚拟机中linux下数据库需要给予权限。

 

允许所有用户通过密码123来访问root数据库

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION

允许用户从ip为192.168.1.3的主机连接到mysql服务器

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'password' WITH GRANT OPTION;   

 

4.创建数据库并选择数据库

create database 数据库名字 character set gbk字符编码,gbk可显示中文);

create database test character set gbk

use 数据库名;

use test;

 

5.创建数据库表

create table 表名称(列声明);

create table students

id int unsigned not null auto_increment primary key,

name char(8) not null,

sex char(4) not null,

age tinyint unsigned not null,

tel char(13) null default "-"

);

 

create table tablename(columns) 为创建数据库表的命令, 列的名称以及该列的数据类型将在括号内完成;

括号内声明了5列内容, id、name、sex、age、tel为每列的名称, 后面跟的是数据类型描述, 列与列的描述之间用逗号(,)隔开;

"id int unsigned not null auto_increment primary key" 行进行介绍:

    "id" 为列的名称;

    "int" 指定该列的类型为 int(取值范围为 -8388608到8388607), 在后面我们又用 "unsigned" 加以修饰, 表示该类型为无符号型, 此时该列的取值范围为 0到16777215;

    "not null" 说明该列的值不能为空, 必须要填, 如果不指定该属性, 默认可为空;

    "auto_increment" 需在整数列中使用, 其作用是在插入数据时若该列为 NULL, MySQL将自动产生一个比现存值更大的唯一标识符值。在每张表中仅能有一个这样的值且所在列必须为索引列。

    "primary key" 表示该列是表的主键, 本列的值必须唯一, MySQL将自动索引该列。

 

下面的 char(8) 表示存储的字符长度为8, tinyint的取值范围为 -127到128, default 属性指定当该列值为空时的默认值。

 

6向表中插入数据

(1)顺序插入

insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

方括号内可以省略,因为是顺序插入,如下例

insert into students values(NULL, "王刚", "男", 20, "13811371377"); 

 

(2)任意列插入

则需要罗列出所插入数据所在的列,如下

insert into students (name, sex, age) values("孙丽华", "女", 21);

 

7.查询表中的数据

select 列名称 from 表名称 [查询条件];

(1)要查询部分列

 select name, age from students;

(2)要查询全部列

 select * from students;

(3)按条件查询

 select 列名称 from 表名称 where 条件;

 以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";

判断条件包含=、>、<、>=、<、!=is [not] null、in、likeor 和 and

例如:

查询年龄在21岁以上的所有人信息: select * from students where age > 21;

查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";

查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;

 

8更新表中的数据

update 表名称 set 列名称=新值 where 更新条件;

使用示例:

id为5的手机号改为默认的"-": update students set tel=default where id=5;

将所有人的年龄增加1: update students set age=age+1;

将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19: update students set name="张伟鹏", age=19 where tel="13288097888";

 

9.删除表中的元素

delete from 表名称 where 删除条件;

使用示例:

删除id为2的行: delete from students where id=2;

删除所有年龄小于21岁的数据: delete from students where age<20;

删除表中的所有数据: delete from students;

 

10.创建表之后的修改表属性

alter table 语句用于创建后对表的修改, 基础用法如下:

添加列

alter table 表名 add 列名 列数据类型 [after 插入位置];

在表的最后追加列 address:

alter table students add address char(60);

在名为 age 的列后插入列 birthday:

alter table students add birthday date after age;

修改列

alter table 表名 change 列名称 列新名称 新数据类型;

示例:

将表 tel 列改名为 telphone:

alter table students change tel telphone char(13) default "-";

name 列的数据类型改为 char(16):

alter table students change name name char(16) not null;

删除列

alter table 表名 drop 列名称;

删除 birthday 列:

alter table students drop birthday;

重命名表

alter table 表名 rename 新表名;

重命名 students 表为 workmates:

alter table students rename workmates;

删除整张表:

drop table 表名;

删除 workmates 表: drop table workmates;

删除整个数据库:

drop database 数据库名;

删除 samp_db 数据库: drop database samp_db;

 

11修改Mysql 登录密码

/mysql/bin/ mysqladmin -u root -p password "test123"

再键入原密码即可。

参考文献

http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#d11

 


0 0