zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql中常用的命令

mysql命令 常用
2023-09-14 09:13:14 时间

MySQL中常用的命令

2.使用到的表,数据
#创建表persons
create table Persons(
id int(5) primary key not null auto_increment,
LastName varchar (15) not null,
FirstName varchar (15) not null,
Address varchar (15) not null,
City varchar (15));

#向表中插入数据
insert into persons values
(1,'Adams','John','Oxford Street','London'),
(2,'Bush','George','Fifth Avenue','New York'),
(3,'Carter','Thomas','Changan Street','Beijing');

#创建订单表
create table orders(
id_o int(5) primary key not null auto_increment,
orderno int(10),
id_p int(5));

#向订单表中插入数据
insert into orders values
(1,77895,3),
(2,44678,3),
(3,22456,1),
(4,24562,1),
(5,34764,65);
2.基本命令

2.往表中添加字段名,命令如右: alter table [表名] add [字段名] [字段类型] [[字段位置]可不填]
3.mysql中使用declare命令出错时,请考虑是否是因为没有在存储过程或者是存储函数中申请变量。
4.mysql中删除某一行数据:delete from [表名] where [条件];
5.往数据库中一次插入多条数据:

insert into employee values('LittleLawson',22, 'Enmonster'),
 ('Ltt',22,'baidu'),
 ('dinglei',40,'NetEase'),
 ('Jack',52,'alibaba');

6.创建数据库时,设置字符集CREATE DATABASE [databaseName] CHARACTER SET utf8;
7.SQL Server【对应于Mysql中的limit】
Top字句:用于返回指定记录的数目。对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。
8.like操作符:用于在where子句中搜索列中的指定模式。

%   替代一个或多个字符
_   仅替代一个字符

示例如下【这里的表结构以及数据就不提供了】:

mysql> select * from country where country like '日%';
+---------+------------+
| country | population |
+---------+------------+
| 日本    | 250        |
+---------+------------+
1 row in set (0.00 sec)

mysql> select * from country where country like '日_';
+---------+------------+
| country | population |
+---------+------------+
| 日本    | 250        |
+---------+------------+
1 row in set (0.00 sec)

9.in操作符:在where子句中,规定多个条件值。
10.between and

  • 1.一般会同where子句一起使用
  • 2.between 'aa' and 'bb'在字符串aa和字符串bb之间的(不同的数据库实现细节不同)

11.Join操作

  • inner join 【与join相同】
  • left join
  • right join
  • full join

12.union操作符

  • 01.用于合并两个或多个select语句的结果集
  • 02.UNION结果集中的列名总是等于UNION中第一个SELECT语句中的列名
  • 03.UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。

13.Constraints[约束]:

  • 1.Not null
  • 2.Unique[每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束]
  • 3.primary key
    • 01.在创建表的时候,添加约束:可以为多个列定义主键约束【也就是多个字段同时作为主键】constraint pk_person primary key(id,lastname)
    • 02.表创建好之后,添加primary key约束alter table [tablename] add constraint pk_person primary key(id,lastname)
    • 03.撤销primary key约束:alter table [tableName] drop primary key
  • 4.foreign key:外键约束用于预防破坏表之间连接的动作。同时也能防止非法数据插入外键列,因为它必须是其引用表中的主键的值之一。
    • 01.创建表的时候,指定外键:FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
    • 02.如果需要命名FOREIGN KEY约束,以及为多个列定义FOREIGN KEY约束:CONSTRAINT fk_PerOrders FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
    • 03.单独创建foreign key 约束
ALTER TABLE Orders 
ADD FOREIGN KEY (Id_P) 
REFERENCES Persons(Id_P)
  • 04.如果需要命名 FOREIGN KEY 约束,以及为多个列定义 FOREIGN KEY 约束
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (Id_P)
REFERENCES Persons(Id_P)
  • 05.撤销foreign key
ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders
  • 5.CHECK 约束
    CHECK 约束用于限制列中的值的范围。如果对单个列定义 CHECK 约束,那么该列只允许特定的值。如果对一个表定义 CHECK 约束,那么此约束会在特定的列中对值进行限制
    • 01.创建表时,使用check约束。check (id > 0)
    • 02.如果需要命名 CHECK 约束,以及为多个列定义CHECK 约束:constraint chk_person check (id > 0 and city='sanfrancisco')
    • 03.表已存在,添加一个check约束alter table persons add check (id>0)
    • 04.表已存在,如果需要命名check约束,以及为多个列定义check约束:alter table persons add constraint chk_person check (id>0 and city ='sanfranscico')
    • 05.撤销check约束:alter table persons drop check chk_person

14.修改表名
ALTER TABLE [oldTableName] RENAME TO [newTableName]