SQL语法(三)

1.insert表行

操作 示例
插入1行(不安全) insert into customers values(NULL,‘taop’,‘xinhua’,‘mq’,‘ca’,‘66666’,‘ch’,NULL,NULL);
插入1行(安全) insert into customers(cust_id,cust_name,cust_email) values(477,‘fuck’,‘qq.com’);
LOW_PRIORITY insert low_priority关键字降低该插入语句的优先级,使某些操作如查询操作优先
多行插入 insert into customers(cust_id,cust_name)values(…),(…);
插入检索出的数据 insert into aimtable(…) select … from otnertable;

2.update与delete表行

操作 示例
更新操作 update customers set cust_city=‘Sin’ where cust_id=477;
删除操作 delete from customers where cust_id=477;
删除整个表 truncate table; 直接将表清空

3.创建表(create)

操作 示例
创建表 create table customers(列名 数据类型 NULL/(NOT NULL), …)
单列创建主键语法 primary key(column_name);(创建表时内部使用)
多列创建主键语法 primary key(column_name1,column_name2);(创建表时内部使用)
auto_increment 告诉mysql本列每当增加一行时自动增量(创建表时内部使用)
default 指定默认值(创建表时内部使用)
默认引擎 MyISAM,性能高,支持全文本搜索,但不支持事务处理
InnoDB mysql引擎之一,支持事务处理但不支持全文本搜索
  • 可以使用``指定主键
  • mysql语句忽略空格
  • 创建表时NULL为默认值可以省略不写
  • NULL 不等于空串‘’
  • 一个表只能有一个auto_increment列,且必须被索引,若显示指定会覆盖当前自动增量值
  • 数据库事务的三项原则:ACID

4.更新表(alter)

操作 示例
为表添加列 alter table wahaha add phone int;
为表删除列 alter table wahaha drop column phone;
可以用于创建外键 alter table orders add constraint fK_orders_customers foreign key(cust_id) references customers(cust_id);

5.删除表(drop)

操作 示例
drop drop table wahaha;

6.重命名表(rename)

操作 示例
rename rename table ji to haha;

【参考】
SQL语法(一) https://blog.csdn.net/
SQL语法(二) https://blog.csdn.net/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章