mysql操作语句_dos命令

mysql操作语句_dos命令
mysql操作语句_dos命令

Mysql dos操作语句

一. 安装与配置MYSQL

二. 常用mysql命令行命令

1 .mysql的启动与停止

启动MYSQL服务net start mysql

停止MYSQL服务net stop mysql

2 . netstat –na | findstr 3306 查看被监听的端口, findstr用于查找后面的端口是否存在

3 . 在命令行中登陆MYSQL控制台, 即使用MYSQL COMMEND LINE TOOL

语法格式mysql –user=root –password=123456 db_name

或mysql –u root –p123456 db_name

4 . 进入MYSQL命令行工具后, 使用status; 或\s 查看运行环境信息

5 . 切换连接数据库的语法: use new_dbname;

6 . 显示所有数据库: show databases;

7 . 显示数据库中的所有表: show tables;

8 . 显示某个表创建时的全部信息: show create table table_name;

9 . 查看表的具体属性信息及表中各字段的描述

Describe table_name; 缩写形式: desc table_name;

三。MySql中的SQL语句

1 . 数据库创建: Create database db_name;

数据库删除: Drop database db_name; 删除时可先判断是否存在,写成: drop database if exits db_name

2 . 建表: 创建数据表的语法: create table table_name (字段1 数据类型, 字段2 数据类型);

例: create table mytable (id int , username char(20));

删表: drop table table_name; 例: drop table mytable;

8 . 添加数据: Insert into 表名[(字段1 , 字段2 , ….)] values (值1 , 值2 , …..);

如果向表中的每个字段都插入一个值,那么前面[ ] 括号内字段名可写也可不写

例: insert into mytable (id,username) values (1,’zhangsan’);

9 . 查询: 查询所有数据: select * from table_name;

查询指定字段的数据: select 字段1 , 字段2 from table_name;

例: select id,username from mytable where id=1 order by desc;多表查询语句------------参照第17条实例

10 . 更新指定数据, 更新某一个字段的数据(注意,不是更新字段的名字)

Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段顺序]

例: update mytable set username=’lisi’ where id=1;

Order语句是查询的顺序, 如: order by id desc(或asc) , 顺序有两种: desc倒序(100—1,即从最新数据往后查询),asc(从1-100),Where和order语句也可用于查询select 与删除delete

11 . 删除表中的信息:

删除整个表中的信息: delete from table_name;

删除表中指定条件的语句: delete from table_name where 条件语句; 条件语句如: id=3;

12 . 创建数据库用户

一次可以创建多个数据库用户如:

CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’….

13 . 用户的权限控制:grant

库,表级的权限控制: 将某个库中的某个表的控制权赋予某个用户

Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ];

14 . 表结构的修改

(1)增加一个字段格式:

alter table table_name add column (字段名字段类型); ----此方法带括号

(2)指定字段插入的位置:

alter table table_name add column 字段名字段类型after 某字段;

删除一个字段:

alter table table_name drop字段名;

(3)修改字段名称/类型

alter table table_name change 旧字段名新字段名新字段的类型;

(4)改表的名字

alter table table_name rename to new_table_name;

(5)一次性清空表中的所有数据

truncate table table_name; 此方法也会使表中的取号器(ID)从1开始

15 . 增加主键,外键,约束,索引。。。。(使用方法见17实例)

①约束(主键Primary key、唯一性Unique、非空Not Null)

②自动增张auto_increment

③外键Foreign key-----与reference table_name(col_name列名)配合使用,建表时单独使用

④删除多个表中有关联的数据----设置foreign key 为set null ---具体设置参考帮助文档

16 . 查看数据库当前引擎

SHOW CREATE TABLE table_name;

修改数据库引擎

ALTER TABLE table_name ENGINE=MyISAM | InnoDB;

17 . SQL语句运用实例:

--1 建users表

create table users (id int primary key auto_increment,nikename varchar(20) not null unique,password varchar(100) not null,address varchar(200), reg_date timestamp not null default CURRENT_TIMESTAMP);

--2 建articles表,在建表时设置外键

create table articles (id int primary key auto_increment,content longtext not null,userid int,constraint foreign key (userid) references users(id) on delete set null);

-----------------------------------------------------------------------

--2.1 建articles表,建表时不设置外键

create table articles (id int primary key auto_increment,content longtext not null,userid int);

--2.2 给articles表设置外键

alter table articles add constraint foreign key (userid) references users(id) on delete set null;

------------------------------------------------------------------------

--3. 向users表中插入数据,同时插入多条

insert into users (id,nikename,password,address) values (1,'lyh1','1234',null),(10,'lyh22','4321','湖北武汉'),(null,'lyh333','5678', '北京海淀');

--4. 向article中插入三条数据

insert into articles (id,content,userid) values (2,'hahahahahaha',11),(null,'xixixixixix',10),(13,'aiaiaiaiaiaiaiaiaiaiaiaia',1),(14,'hohoahaoaoooooo oooo',10);

--5. 进行多表查询,选择users表中ID=10的用户发布的所有留言及该用户的所有信息select articles.id,articles.content,users.* from users,articles where users.id=10 and https://www.360docs.net/doc/6817177510.html,erid=users.id order by articles.id desc;

--6. 查看数据库引擎类型

show create table users;

--7. 修改数据库引擎类型

alter table users engine=MyISAM; ---因为users表中ID被设置成外键,执行此句会出错

--8. 同表查询,已知一个条件的情况下.查询ID号大于用户lyh1的ID号的所有用户

select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id;

------也可写成

select id,nikename,address from users where id>(select id from users where nikename='lyh1');

9. 显示年龄比领导还大的员工:

select https://www.360docs.net/doc/6817177510.html, from users a,users b where a.managerid=b.id and a.age>b.age;

查询编号为2的发帖人: 先查articles表,得到发帖人的编号,再根据编号查users得到的用户名。

接着用关联查询.

select * from articles,users得到笛卡儿积,再加order by articles.id以便观察

使用select * from articles,users where articles.id=2 筛选出2号帖子与每个用户的组合记录

再使用select * from articles,users where articles.id=2 and https://www.360docs.net/doc/6817177510.html,erid=users.id选出users.id等于2号帖的发帖人id的记录.

只取用户名:select user where user.id=(select userid from articles where article.id =2)

找出年龄比小王还大的人:假设小王是28岁,先想找出年龄大于28的人

select * from users where age>(select age from users where name='xiaowang');

*****要查询的记录需要参照表里面的其他记录:

select https://www.360docs.net/doc/6817177510.html, from users a,users b where https://www.360docs.net/doc/6817177510.html,='xiaowang' and a.age>b.age

表里的每个用户都想pk一下.select a.nickname,b.nickname from users a,users b where a.id>b.id ;

更保险的语句:select a.nickname,b.nickname from (select * from users order by id) a,(se lect * from users order by id) b where a.id>b.id ;

再查询某个人发的所有帖子.

select b.* from articles a , articles b where a.id=2 and https://www.360docs.net/doc/6817177510.html,erid=https://www.360docs.net/doc/6817177510.html,erid

说明: 表之间存在着关系,ER概念的解释,用access中的示例数据库演示表之间的关系.只有innodb引擎才支持foreign key,mysql的任何引擎目前都不支持check约束。

四、字符集出现错误解决办法

出现的问题:

mysql> update users

-> set username='关羽'

-> where userid=2;

ERROR 1366 (HY000): Incorrect string value: '\xB9\xD8\xD3\xF0' for column 'usern

ame' at row 1

向表中插入中文字符时,出现错误。

mysql> select * from users;

+--------+----------+

| userid | username |

+--------+----------+

| 2 | ???? |

| 3 | ???? |

| 4 | ?í?ù |

+--------+----------+

3 rows in set (0.00 sec)

表中的中文字符位乱码。

解决办法:

使用命令:

mysql> status;

--------------

mysql Ver 14.12 Distrib 5.0.45, for Win32 (ia32)

Connection id: 8

Current database: test

Current user: root@localhost

SSL: Not in use

Using delimiter: ;

Server version: 5.0.45-community-nt MySQL Community Edition (GPL)

Protocol version: 10

Connection: localhost via TCP/IP

Server characterset: latin1

Db characterset: latin1

Client characterset: gbk

Conn. characterset: gbk

TCP port: 3306

Uptime: 7 hours 39 min 19 sec

Threads: 2 Questions: 174 Slow queries: 0 Opens: 57 Flush tables: 1 Open ta

bles: 1 Queries per second avg: 0.006

--------------

查看mysql发现Server characterset,Db characterset的字符集设成了latin1,所以出现中文乱码。

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| users |

+----------------+

1 row in set (0.00 sec)

更改表的字符集。

mysql> alter table users character set GBK;

Query OK, 3 rows affected (0.08 sec)

Records: 3 Duplicates: 0 Warnings: 0

查看表的结构:

mysql> show create users;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'users

' at line 1

mysql> show create table users;

+-------+-----------------------------------------------------------------------

------------------------------------------------------------------------------+

| Table | Create Table

|

+-------+-----------------------------------------------------------------------

------------------------------------------------------------------------------+

| users | CREATE TABLE `users` (

`userid` int(11) default NULL,

`username` char(20) character set latin1 default NULL

) ENGINE=InnoDB DEFAULT CHARSET=gbk |

+-------+-----------------------------------------------------------------------

------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> desc users;

+----------+----------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+----------+------+-----+---------+-------+

| userid | int(11) | YES | | NULL | |

| username | char(20) | YES | | NULL | |

+----------+----------+------+-----+---------+-------+

2 rows in set (0.02 sec)

这时向表中插入中文然后有错误。

mysql> insert into users values(88,'中文');

ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column 'usern ame' at row 1

mysql> insert into users values(88,'中文');

ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column 'usern ame' at row 1

还要更改users表的username的字符集。

mysql> alter table users modify username char(20) character set gbk;

ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xCB\xC4' for column 'usern ame' at row 1

mysql> alter table users modify username char(20) character set gbk;

ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xCB\xC4' for column 'usern

ame' at row 1

因为表中已经有数据,所以更改username字符集的操作没有成***

清空users表中的数据

mysql> truncate table users;

Query OK, 3 rows affected (0.01 sec)

从新更改user表中username的字符集

mysql> alter table users modify username char(20) character set gbk;

Query OK, 0 rows affected (0.06 sec)

Records: 0 Duplicates: 0 Warnings: 0

这时再插入中文字符,插入成***。

mysql> insert into users values(88,'中文');

Query OK, 1 row affected (0.01 sec)

mysql> select * from users;

+--------+----------+

| userid | username |

+--------+----------+

| 88 | 中文|

+--------+----------+

1 row in set (0.00 sec)

mysql>

Mysql用户权限设置:

用户管理

mysql>use mysql;

查看

mysql> select host,user,password from user ;

创建

mysql> create user zx_root IDENTIFIED by 'xxxxx'; //identified by 会将纯文本密码加密作为散列值存储

修改

mysql>rename user feng to newuser;//mysql 5之后可以使用,之前需要使用update 更新user表

删除

mysql>drop user newuser; //mysql5之前删除用户时必须先使用revoke 删除用户权限,然后删除用户,mysql5之后drop 命令可以删除用户的同时删除用户的相关权限更改密码

mysql> set password for zx_root =password('xxxxxx');

mysql> update https://www.360docs.net/doc/6817177510.html,er set password=password('xxxx') where user='otheruser'

查看用户权限

mysql> show grants for zx_root;

赋予权限

mysql> grant select on dmc_db.* to zx_root;

回收权限

mysql> revoke select on dmc_db.* from zx_root; //如果权限不存在会报错

上面的命令也可使用多个权限同时赋予和回收,权限之间使用逗号分隔

mysql> grant select,update,delete ,insert on dmc_db.* to zx_root; 如果想立即看到结果使用

flush privileges ;

命令更新

设置权限时必须给出一下信息

1,要授予的权限

2,被授予访问权限的数据库或表

3,用户名

grant和revoke可以在几个层次上控制访问权限

1,整个服务器,使用grant ALL 和revoke ALL

2,整个数据库,使用on database.*

3,特点表,使用on database.table

4,特定的列

5,特定的存储过程

user表中host列的值的意义

% 匹配所有主机

localhost localhost不会被解析成IP地址,直接通过UNIXsocket连接

127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;

::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

grant 普通数据用户,查询、插入、更新、删除数据库中所有表数据的权利。

grant select on testdb.* to common_user@’%’

grant insert on testdb.* to common_user@’%’

grant update on testdb.* to common_user@’%’

grant delete on testdb.* to common_user@’%’

或者,用一条MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to common_user@’%’

9>.grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。

grant 创建、修改、删除MySQL 数据表结构权限。

grant create on testdb.* to developer@’192.168.0.%’;

grant alter on testdb.* to developer@’192.168.0.%’;

grant drop on testdb.* to developer@’192.168.0.%’;

grant 操作MySQL 外键权限。

grant references on testdb.* to developer@’192.168.0.%’;

grant 操作MySQL 临时表权限。

grant create temporary tables on testdb.* to develop er@’192.168.0.%’;

grant 操作MySQL 索引权限。

grant index on testdb.* to developer@’192.168.0.%’;

grant 操作MySQL 视图、查看视图源代码权限。

grant create view on testdb.* to developer@’192.168.0.%’;

grant show view on testdb.* to developer@’192.168.0.%’;

grant 操作MySQL 存储过程、函数权限。

grant create routine on testdb.* to developer@’192.168.0.%’; -- now, can show procedure status

grant alter routine on testdb.* to developer@’192.168.0.%’; -- now, you can drop a procedure

grant execute on testdb.* to developer@’192.168.0.%’;

10>.grant 普通DBA 管理某个MySQL 数据库的权限。

grant all privileges on testdb to dba@’localhost’

其中,关键字“privileges” 可以省略。

11>.grant 高级DBA 管理MySQL 中所有数据库的权限。

grant all on *.* to dba@’localhost’

12>.MySQL grant 权限,分别可以作用在多个层次上。

1. grant 作用在整个MySQL 服务器上:

grant select on *.* to dba@localhost; -- dba 可以查询MySQL 中所有数据库中的表。

grant all on *.* to dba@localhost; -- dba 可以管理MySQL 中的所有数据库

2. grant 作用在单个数据库上:

常用MySQL语句大全

MySQL服务的配置和使用 修改MySQL管理员的口令:mysqladmin –u root password 密码字符串 如:mysqldmin –u root password 111111 连接MySQL服务器,使用命令:mysql [-h 主机名或IP地址] [-u 用户名] [-p] 如:mysql –u root –p 如已有密码需修改root密码用命令: mysqladmin –u root –p password 新密码字符串 如:mysqladmin –u root –p password 111111 创建数据库格式为:CREATE DATABASE 数据库名称; 如:mysql>create database abc; 默认创建数据库保存在/var/lib/mysql中 查看数据库是 mysql>show abc; 选择数据库是 USE 数据库名称; 如:mysql>use abc; 删除数据库是 DROP DATABASE 数据库名称;如:mysql>drop database abc; 数据库的创建和删除 创建表是CREATE TABLE 表名称(字段1,字段2,…[表级约束]) [TYPE=表类型]; 其中字段(1,2 )格式为:字段名字段类型[字段约束] 如创建一个表student,如下: mysql>create table student ( sno varchar(7) not null, 字段不允许为空 sname varchar (20 )not null, ssex char (1) default …t?, sbirthday date, sdepa char (20), primary key (sno) 表的主键 ); 可用describe命令查看表的结构。 默认表的类型为MYISAM,并在/var/lib/mysql/abc 目录下建立student.frm(表定

数据库基础操作语句

一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar 类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、说明:几个高级查询运算词 A:UNION 运算符

MySQL语法语句汇编

MySQL语法语句大全 一、SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件(如表、索引、字段等)的建立和操纵。 以下是一些重要的SQL快速参考,有关SQL的语法和在标准SQL上增加的特性,请查询MySQL手册。 1.创建表 表是数据库的最基本元素之一,表与表之间可以相互独立,也可以相互关联。创建表的基本语法如下: create table table_name (column_name data无效{identity |null|not null}, …) 其中参数table_name和column_name必须满足用户数据库中的识别器(identifier)的要求,参数data无效是一个标准的SQL类型或由用户数据库提供的类型。用户要使用non-null从句为各字段输入数据。 create table还有一些其他选项,如创建临时表和使用select子句从其他的表中读取某些字段组成新表等。还有,在创建表是可用PRIMARY KEY、KEY、INDEX等标识符设定某些字段为主键或索引等。 书写上要注意: 在一对圆括号里的列出完整的字段清单。 字段名间用逗号隔开。 字段名间的逗号后要加一个空格。 最后一个字段名后不用逗号。 所有的SQL陈述都以分号";"结束。 例: mysql> CREATE TABLE test (blob_col BLOB,index(blob_col(10))); 2.创建索引 索引用于对数据库的查询。一般数据库建有多种索引方案,每种方案都精于某一特定的查询类。索引可以加速对数据库的查询过程。创建索引的基本语法如下: create index index_name on table_name (col_name[(length)],... ) 例: mysql> CREATE INDEX part_of_name ON customer (name(10)); 3.改变表结构 在数据库的使用过程中,有时需要改变它的表结构,包括改变字段名,甚至改变不同数据库字段间的关系。可以实现上述改变的命令是alter,其基本语法如下: alter table table_name alter_spec [,alter_spec ...] 例: mysql> ALTER TABLE t1 CHANGE a b INTEGER; 4.删除数据对象

面试数据库常用操作语句

数据库复习资料准备 1、创建/删除数据库:Create/ Drop database name 2、创建新表:Create table name(id int not null primary key, name char(20)) // 带主键 Create table name(id int not null, name char(20), primary key (id, name)) // 带复合主键Create table name(id int not null default 0, name char(20)) // 带默认值 3、删除表:Drop table name 4、表中添加一列:Alter table name add column size int 5、添加/删除主键:Alter table name add/drop primary key(size) 6、创建索引:Create [unique] index idxname on tabname(col) 7、删除索引:Drop index idxname 8、选择:Select *from table where 范围 9、删除重复记录Delete from name where id not in (select max(id) from name group by col1) 10、插入:Insert into table(field1, field2) values (value1, value) 11、删除:Delete from table where 范围 12、更新:Update table set field=value where 范围 13、查找:Select *from table where field like “” 14、排序:Select *from table order by field [desc] 15、总数:Select count as totalcount from table 16、求和:Select sum(field) as sumvalue from table 17、平均:Select avg(field) as avgvalue from table 18、最大:Select max(field) as maxvalue from table 19、最小:Select min(field) as minvalue from table 20、复制表:Select * into b from a where 范围Select top 0 * into b from a where 范围 21、拷贝表:Insert into b(a, b, c) select d,e,f from b; 22、子查询: select ename from emp where deptno=(select deptno from dept where loc='NEW');// 单查询select ename from emp where deptno in (select deptno from dept where dname like 'A%');// 多行子查询 select deptno,ename ,sal from emp where (deptno,sal) IN (select deptno,MAX(sal) from emp group by deptno);// 多列子查询 23、外连接查询:Select a.a, a.b, a.c, b.c, b.d, b.f from a left out join b on a.a = b.c 24、between用法:Select a,b,c, from table where a not between 数值1 and 数值2 25、in用法:select * from table1 where a [not] in (‘值1’,‘值2’,‘值4’,‘值6’) 26、两张关联表,删除主表中在副表中没有的信息:delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1) 27、日程提前五分钟提醒:select * from 日程where datediff(‘minute‘,f开始时间,getdate())>5 28、前10条记录:select top 10 * form table1 where 范围 29、包括所有在TableA 中但不在TableB和TableC 中的行:select a from tableA except (select a from tableB) except (select a from tableC) 30、随机取出10条数据:select top 10 * from tablename order by newid() 31、列出数据库里所有的表名:select name from sysobjects where type=‘U’ 32、列出表里的所有的字段:select name from syscolumns where id=object_id(‘TableName’)

mysql增删改查基本语句

mysql 增、删、改、查基本语句 数据库的链接和选择及编码 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'"); 添加数据 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'",$sel); $add="insert into title(title,content,username,time) values('$title','$content','$username',$time)"; $query=mysql_query($add); if($query){ echo "add sucess"; echo ""; } else echo "add false"; 删除数据 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'"); $id=$_GET['id']; $delete="delete from title where id='$id'"; $query=mysql_query($delete); if($query){ echo "delete sucess!"; echo ""; } else echo "delete false"; 改数据 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'",$sel);

数据库经典SQL语句大全

数据库经典SQL语句大全 篇一:经典SQL语句大全 下列语句部分是Mssql语句,不可以在access中使用。 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk','testBack', 'c:mssql7backupMyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2? from tab_old definition only 5、说明: 删除新表: tabname 6、说明: 增加一个列:Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明: 添加主键:Alter table tabname add primary key(col) 说明: 删除主键:Alter table tabname drop primary key(col) 8、说明: 创建索引:create [unique] index idxname on tabname(col?.) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。

mysql命令语句大全

show databases; 显示数据库 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删除数据库,不提醒 show tables; 显示表 describe tablename; 显示具体的表结构 select 中加上distinct去除重复字段 mysqladmin drop databasename 删除数据库前,有提示。 显示当前mysql版本和当前日期 select version(),current_date; 修改mysql中root的密码: shell>mysql -h localhost -u root -p //登录 mysql> update user set password=password("xueok654123") where user='root'; mysql> flush privileges //刷新数据库 mysql>use dbname; 打开数据库: mysql>show databases; 显示所有数据库 mysql>show tables; 显示数据库mysql中所有的表:先use mysql;然后 mysql>describe user; 显示表mysql数据库中user表的列信息); grant 创建用户firstdb(密码firstdb)和数据库,并赋予权限于firstdb数据库 mysql> create database firstdb; mysql> grant all on firstdb.* to firstdb identified by 'firstdb' 会自动创建用户firstdb mysql默认的是本地主机是localhost,对应的IP地址就是127.0.0.1,所以你用你的IP 地址登录会出错,如果你想用你的IP地址登录就要先进行授权用grant命令。 mysql>grant all on *.* to root@202.116.39.2 identified by "123456"; 说明:grant 与on 之间是各种权限,例如:insert,select,update等 on 之后是数据库名和表名,第一个*表示所有的数据库,第二个*表示所有的表 root可以改成你的用户名,@后可以跟域名或IP地址,identified by 后面的是登录用的密码,可以省略,即缺省密码或者叫空密码。 drop database firstdb; 创建一个可以从任何地方连接服务器的一个完全的超级用户,但是必须使用一个口令something做这个 mysql> grant all privileges on *.* to user@localhost identified by 'something' with 增加新用户 格式:grant select on 数据库.* to 用户名@登录主机identified by "密码"

mysql数据库常用语句大全

mysql数据库常用语句 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice ’disk’, ’testBack’, ’c:mssql7backupMyNwind_1.dat’ --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only 5、说明: 删除新表:drop table tabname 6、说明: 增加一个列:Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar 类型的长度。 7、说明: 添加主键:Alter table tabname add primary key(col) 说明: 删除主键:Alter table tabname drop primary key(col) 8、说明: 创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明: 创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围

数据库语言大全

经典SQL语句大全 —、基础 1说明:创建数据库 CREATE DATABASE database-n ame 2、说明:删除数据库 drop database dbn ame 3、说明:备份sql server ---创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwi nd_1.dat' ---开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tab name(col1 type1 [not nu II] [primary key],col2 type2 [not nul l] ,..) 根据已有的表创建新表: A: create table tab_new like tab_old ( 使用旧表创建新表) B: create table tab_new as select col1,col2 …from tab_old definition only 5、说明:删除新表 drop table tab name 6、说明:增加一个列 Alter table tab name add colu mn col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varc har类型的长度。 7、说明:添加主键:Alter table tab name add primary key(col) 说明:删除主键 : Alter table tab name drop primary key(col) 8、说明:创建索弓丨:create [unique] index idxname on tabname(col ….) 删除索弓丨:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view view name as select stateme nt 删除视图:drop view view name 10、说明:几个简单的基本的sql语句 选择:select * from tablei where 范围 插入:in sert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ' %value1% ---like 的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1

mysql sql语句大全_mysql sql语句面试题_mysql的常用sql语句推荐

mysql sql语句大全_mysql sql语句面试题_mysql的常用sql 语句推荐 今天我们向大家整理了一些【mysql的常用sql语句】系列文章,希望大家对【mysql的常用sql语句】技术点有更深的了解。 小编下面整理一下mysql sql语句大全_mysql sql语句面试题_mysql的常用sql语句的资料给大家作为参考。mysql sql 语句大全_mysql sql语句面试题_mysql的常用sql语句推荐[05-25] MySQL常用SQL语句的介绍[05-25] MySQL 存储过程执行动态SQL语句详解[05-23] MySql 常用操作的SQL语句的介绍[05-22] mysql语句插入多条数据的方法[05-21] 远程用户访问mysql服务sql语句的心得体会[05-21] MySQL 存储过程执行动态SQL语句的介绍[05-20] MySQL 复制表结构、内容到另一张表的SQL语句的介绍[05-20] mysql 查询今天、昨天、近7天、近30天、本月、上一月的SQL语句的介绍[05-20] mysql 获取一天、一周、一月时间的sql语句的方法[05-19] mysql把一个表某字段复制到另一张表的某个字段的SQL语句的方法[05-17] MySQL SQL 语句优化的介绍[05-10] mysql通过查看跟踪日志跟踪执行 的sql语句的方法[05-10] mysql语句运行时间的查看方法[05-10] mysql建表常用sql语句的介绍[05-10] MySQL字段

自增自减的SQL语句的推荐[05-04] SQL语句行列转换的两种方法case...when与pivot函数的介绍[05-04] SQL语句语法汇总的推荐[05-04] sql语句like的用法的介绍[05-04] MySQL查询数据库占用磁盘大小、单个库所有表的大小的sql语句[05-02] sql语句left_join、inner_join中的on与where 的区别介绍[05-02] MySql 获取某字段存在哪个表的sql语句的介绍[04-29] SQL语句Replace INTO与INSERT INTO 不同的介绍[04-29] 数据库字段分组显示数据的sql语句的介绍[04-29] MySQL批量删除指定前缀表的sql语句的推荐[04-29] MySQL优化sql语句查询常用30种方法的推荐[04-25] MySQL 查询当前正在运行的SQL语句[04-19] sql 语句的常用语法[04-19] PHP+Mysql常用SQL语句[04-05] 将数字标识转为汉字展示的sql语句[04-05] 数据表字段删除、添加的SQL语句[04-05] sql批量修改字段值的方法_sql 语句修改字段值[03-03] mysql 实现查看表结构的SQL语句[03-03] mysql 获取当前日期周一和周日的SQL语句[03-03] mysql 实现按年度、季度、月度、周、日查询的SQL语句[12-16] MySQL数据库操作实现的6条SQL语句[12-16] 将blob的char取出来并转成数字保存在其它字段的sql语句[12-16] mysql常用SQL语句小结[10-24] mySQL使用Explain检查测Sql语句执行效率[10-24] mysql查询当天,昨天,近7天,近30天,本月,上一月数据的SQL语句[10-18]

mysql语句用法,添加、修改、删除字段

一,连接MySQL 二,MySQL管理与授权 三,数据库简单操作 四, 数据库备份 五,后记 一,连接MySQL 格式:mysql -h 远程主机地址-u 用户名-p 回车 输入密码进入: mysql -u root -p 回车 Enter password: ,输入密码就可以进入 mysql> 进入了 退出命令:>exit 或者ctrl+D 二,MySQL管理与授权 1.修改密码: 格式:mysqladmin -u 用户名-p 旧密码password 新密码 2.增加新用户: >grant create,select,update....(授予相关的操作权限) ->on 数据库.* -> to 用户名@登录主机identified by '密码' 操作实例: 给root用户添加密码: # mysqladmin -u root password 52netseek 因为开始root没有密码,所以-p旧密码一项可以省略. 登陆测试: # mysql -u root -p 回车 输入密码,成功登陆. 将原有的mysql管理登陆密码52netseek改为52china. # mysqladmin -u root -p 52netseek password '52china' 创建数据库添加用户并授予相应的权限: mysql> create database phpbb;

Query OK, 1 row affected (0.02 sec) mysql> use phpbb; Database changed mysql> grant create,select,update,insert,delete,alter -> on phpbb.* -> to phpbbroot@localhost identified by '52netseek'; Query OK, 0 rows affected (0.00 sec) 授予所有的权限: >grant all privileges >on bbs.* >to bbsroot@localhost identified by '52netseek' 回收权限: revoke create,select,update,insert,delete,alter on phpbb.* from phpbbroot@localhost identified by '52netseek'; 完全将phpbbroot这个用户删除: >use mysql >delete from user where user='phpbbroot' and host='localhost'; >flush privileges; 刷新数据库 三,数据库简单操作 1.显示数据库列表: >show databases; mysql test 2.使其成为当前操作数据库 >use mysql; 打开数据库. >show tables; 显示mysql数据库中的数据表. 3.显示数据表的表结构: >describe 表名; >describe user; 显示user表的表结构: 4.创建数据库,建表 >create database 数据库名; >use 数据库名; >create table 表名(字段设定列表) 5.删除数据库,册除表 >drop database 数据库名; >drop table 表名; 6.显示表中的记录;

常用MySQL语句大全分析

MySQL服务的配置和使用 修改MySQL管理员的口令:mysqladmin –u root password 密码字符串 如:连接MySQL服务器,使用命令:mysql [-h 主机名或IP地址] [-u 用户名] [-p] 如:mysql –u root –p 如已有密码需修改root密码用命令: mysqladmin –u root –p password 新密码字符串 如:mysqladmin –u root –p password 111111 mysqldmin –u root password 111111 创建数据库格式为:CREATE DATABASE 数据库名称; 如:mysql>create database abc; 默认创建数据库保存在/var/lib/mysql 中 查看数据库是 mysql>show abc; 选择数据库是 USE 数据库名称; 如:mysql>use abc; 删除数据库是 DROP DATABASE 数据库名称;如:mysql>drop database abc; 数据库的创建和删除 创建表是CREATE TABLE 表名称(字段1,字段2,…[表级约束]) [TYPE=表类型]; 其中字段(1,2 )格式为:字段名字段类型[字段约束]

如创建一个表student,如下: mysql>create table student ( sno varchar(7) not null, 字段不允许为空 sname varchar (20 )not null, ssex char (1) default ‘t’, sbirthday date, sdepa char (20), primary key (sno) 表的主键 ); 可用describe命令查看表的结构。 默认表的类型为MYISAM,并在/var/lib/mysql/abc 目录下建立student.frm(表定义文件),student.MDY(数据文件),stedent.MYI(索引文件)。 复制表CREATE TABLE 新表名称LIKE 原表名称; 如:mysql>create table xtable like student; 删除表DROP TABLE 表名称1[表名称2…]; 如:mysql> drop table xtale; 修改表ALTER TABLE 表名称更改动作1[动作2]; 动作有ADD(增加) DROP(删除)CHANGE、MODIFY(更改字段名和类型)RENAME

MySQL语法大全

1、启动MySQL服务器 两种方法: 一是用winmysqladmin,如果机器启动时已自动运行,则可直接进入下一步操作。二是在DOS方式下运行 d:mysqlbinmysqld 2、进入mysql交互操作界面 在DOS方式下,运行: d:mysqlbinmysql 出现: mysql 的提示符,此时已进入mysql的交互操作方式。 如果出现 "ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)“, 说明你的MySQL还没有启动。 3、退出MySQL操作界面 在mysql>提示符下输入quit可以随时退出交互操作界面: mysql> quit Bye 你也可以用control-D退出。 4、第一条命令 mysql> select version(),current_date(); 1 row in set (0.01 sec) mysql> 此命令要求mysql服务器告诉你它的版本号和当前日期。尝试用不同大小写操作上述命令,看结果如何。 结果说明mysql命令的大小写结果是一致的。 练习如下操作: mysql>Select (20+5)*4; mysql>Select (20+5)*4,sin(pi()/3); mysql>Select (20+5)*4 AS Result,sin(pi()/3); (AS: 指定假名为Result) 5、多行语句 一条命令可以分成多行输入,直到出现分号“;”为止: mysql> select -> USER() -> ,

-> now() ->; +--------------------+---------------------+ | USER() | now() | +--------------------+---------------------+ | ODBC@localhost | 2001-05-17 22:59:15 | +--------------------+---------------------+ 1 row in set (0.06 sec) mysql> 注意中间的逗号和最后的分号的使用方法。 6、一行多命令 输入如下命令: mysql> SELECT USER(); SELECT NOW(); +------------------+ | USER() | +------------------+ | ODBC@localhost | +------------------+ 1 row in set (0.00 sec) +---------------------+ | NOW() | +---------------------+ | 2001-05-17 23:06:15 | +---------------------+ 1 row in set (0.00 sec) mysql> 注意中间的分号,命令之间用分号隔开。 7、显示当前存在的数据库 mysql> show databases; +----------+ | Database | +----------+ | mysql| | test | +----------+ 2 row in set (0.06 sec) mysql> 8、选择数据库并显示当前选择的数据库 mysql> USE mysql Database changed mysql>

Mysql基本操作

1.Mysql基本操作 1.1修改管理员的密码 1)Mysql刚安装好后无密码,使用”mysqladmin–uroot password 密码”修改管理员密 码 2)以管理员身份登录数据库,使用”mysql–u root” 3)原来有密码,现在要修改,使用”mysqladmin–uroot–p旧密码 password 新密码” 1.2用户的创建、删除、授权与撤权 Mysql安装好后,默认有两个数据库(mysql和test),而且除了root用户外,其他用户只能访问test数据库。 Mysql中设置了5个授权表(user/db/host/tables_priv/columnts_priv)。 1)创建新用户,方法如下: A.mysql–u root –p #以管理员身份登录 B.insert into https://www.360docs.net/doc/6817177510.html,er(host,user,password) values(‘%’,’guest’,password(‘guest’));#创建一个用户名为guest的用 户 C.flush privileges;#重载授权表 2)删除用户,方法如下: A.mysql–u root –p #以管理员身份登录 B.delete from https://www.360docs.net/doc/6817177510.html,er where user=’guest’; C.flush privileges; 3)更改用户密码,方法如下: A.mysql–u root –p #以管理员身份登录 B.update https://www.360docs.net/doc/6817177510.html,er set password=password(‘123’) where user=’guest’; C.flush privileges; 4)用户授权,方法如下: 格式:GRANT 权限列表[(字段列表)] ON 数据库名称.表名 TO 用户名@域名或IP地址[IDENTIFIED BY ‘密码值’] [WITH GRANT OPTION]; 常用权限如下: 全局管理权限: FILE: 在MySQL服务器上读写文件。 PROCESS: 显示或杀死属于其它用户的服务线程。 RELOAD: 重载访问控制表,刷新日志等。 SHUTDOWN: 关闭MySQL服务。 数据库/数据表/数据列权限: Alter: 修改已存在的数据表(例如增加/删除列)和索引。 Create: 建立新的数据库或数据表。

相关文档
最新文档