增加語句是insert,例如insert into ttt(id) values(1,2,3);
成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司,提供網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,是專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
刪除語句是delete,例如delete from ttt where id=1;
修改語句是update,力圖update ttt set id=4 where id=2;
你是sql語句錯(cuò)了
String sql="insert into information values(information_seq.nextval,?,?,?,?)";錯(cuò)了,mysql中沒有序列,改為
String sql="insert into information values(default,?,?,?,?)";就行了
養(yǎng)成好習(xí)慣,插入的時(shí)候把字段表也帶上;
INSERT?INTO?`tb_column`?(`columnId`,?`columnName`,?`columnDisc`,?`columnOrder`)?
VALUES?
('1',?'新聞',?'國(guó)內(nèi)國(guó)外新聞',?'12'),?
('2',?'財(cái)經(jīng)',?'關(guān)注q',?'7'),?
('3',?'娛樂',?'關(guān)注',?'14'),?
('4',?'體育',?'關(guān)注新聞',?'6'),?
('5',?'讀書',?'讀書內(nèi)容',?'5');
insert into 表名 values(default,"名字","2011-04-15 12:22:25"); //default可以換成null
------------------------------
insert into 表名 (name,datetime) values("名字","2011-04-15 12:22:25");
使用SQL語法大寫,增加可讀性(小寫部分就是自己數(shù)據(jù)庫(kù)寫的表/字段嘍,具體你懂得...)。
創(chuàng)建數(shù)據(jù)庫(kù):CREATE DATABASE mysql_db;
刪除數(shù)據(jù)庫(kù):DROP DATABASE mysql_db;
查看數(shù)據(jù)庫(kù):SHOW DATABASES;
使用數(shù)據(jù)庫(kù):USE mysql_db;
查看數(shù)據(jù)庫(kù)中的表:SHOW TABLES;
創(chuàng)建表:
CREATE TABLE user(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) UNIQUE NOT NULL,
sex ENUM('1','2','3') NOT NULL DEFAULT 3
);
查看表結(jié)構(gòu):①SELECT COLUMN FROM user;
②DESC user;
查看表創(chuàng)建語句(可查看各種自動(dòng)生成的名字):SHOW CREATE TABLE user;
插入表記錄:
①INSERT INTO user(id,username,sex) VALUES(1,'Tom',1);
②INSERT user(username) VALUES ('John');
查找表:SELECT * FROM user;
刪除表中的數(shù)據(jù)(保留表結(jié)構(gòu)):TRUNCATE TABLE user;
將查詢結(jié)果寫入新表:
INSERT [INTO] privinces(sex) SELECT sex FROM user GROUP BY sex;
(若表中有數(shù)據(jù),請(qǐng)不要隨意更改表名、列名)
修改表名:
①ALTER TABLE user RENAME [AS|TO] users;
②RENAME TABLE user TO users;
添加單列:
ALTER TABLE user
ADD [COLUMN] age SMALLINT NOT NULL UNSIGNED DEFUALT 18
[FIRST | AFTER sex];
刪除列:ALTER TABLE user DROP sex[,DROP age];
修改列名稱和定義:
ALTER TABLE user
CHANGE [COLUMN] age a_ge TINYINT NOT NULL UNSIGNED AFTER id;
單表更新:UPDATE user SET age = age + 5,sex = 1 [WHERE id = 2];
單表刪除:DELETE FROM user [WHERE id =3];
單(多)表連接:
SELECT p.userid,p.username FROM user AS p LEFT JOIN user AS s
ON p.userid = s.age;
單(多)表刪除:
DELETE * FROM user [AS] u1 LEFT JOIN (
SELECT p.userid FROM user AS p LEFT JOIN user AS s
ON p.userid =s.age GROUP BY p.userid HAVING COUNT(s.age)1 ) [AS] u2
ON u1.userid = u2.age
WHERE u1.userid u2.userid;
查找記錄:
SELETE select_expr [,select_expr]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name|position}[ASC|DESC],...] //查詢結(jié)果分組
[HAVING where_condition] //設(shè)置分組條件
[ORDER BY {col_name|expr|position}[ASC|DESC]] //對(duì)結(jié)果排序
LIMIT {[offset,]row_count|row_count OFFSET offset}]//限制記錄數(shù)量
]
記錄為2、3行:SELECT * FROM user LIMIT 1,2;
別名使用(在項(xiàng)目中一般都使用別名)AS alias_name:
SELECT id AS uid FROM user AS u ;
子查詢:
SELECT AVG(age) FROM user WHERE userid =
[ALL|SOME|ANY|[NOT] IN|[NOT] EXISTS](SELECT uid FROM privinces WHERE pname = '河南');
外鍵約束:
(父表為user表,必須先在父表中插入數(shù)據(jù),才能在子表中插入數(shù)據(jù))
CASEADE:從父表中刪除或更新行 且 自動(dòng)級(jí)聯(lián)刪除或更新子表中匹配的行;
SET NULL:從父表刪除或更新行,并設(shè)置子表中的外鍵列為NULL;
RESTRICT:拒絕對(duì)父表的更新或刪除操作;
CREATE TABLE privinces(
pid SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
pname VARCHAR(20) NOT NULL ,
uid SMALLINT,
FOREIGN KEY (uid) REFERENCES user (id) //privinces的字段uid與user表的字段id外鍵約束
ON DELETE CASEADE //刪除時(shí) 級(jí)聯(lián)刪除
);
增加主鍵、唯一、外鍵約束:
ALTER TABLE privinces
ADD [CONSTRAINT uq_pname] UNIQUE(pname);
添加/刪除默認(rèn)約束:
ALTER TABLE privinces
ALTER pname {SET DEFAULT literal | DROP DEFAULT};
刪除主鍵約束:
ALTER TABLE privinces DROP PRIMARY KEY;
刪除唯一(索引)約束:
(查看約束名字key_name: SHOW INDEXS FROM privinces;)
ALTER TABLE privinces DROP {INDEX | KEY} 約束名;
刪除外鍵約束:
(查看約束名字:SHOW CREATE TABLE privinces;)
ALTER TABLE privinces DROP FOREIGN KEY 約束名;
多表更新:
UPDATE 表的參照關(guān)系 SET col_name = {[expr | value]};
表的參照關(guān)系:
tab1_reference {[INNER|CROSS] JOIN |{LEFT|RIGHT}[OUTER] JOIN} tab2_refenence ON conditional_expr;
內(nèi)連接:INNER JOIN
左外連接:LEFT JOIN
右外連接:ROGHT JOIN
UPDATE user INNER JOIN privinces ON user_privince = pname
SET user_privince = pid;//(更新user內(nèi)連接privince,條件是 user_privince.=pname,設(shè)置user_privince=pid)
創(chuàng)建數(shù)據(jù)表的同時(shí)將查詢結(jié)果寫入數(shù)據(jù)表:
CREATE TABLE table_name
[create_definition, ...] //創(chuàng)建語句的定義
select_statement; //要插入查詢結(jié)果的 查詢語句
字符函數(shù):
CONCAT()字符連接;CONCAT('I','LOVE','YOU');
CONCAT_WS()使用指定的分隔符進(jìn)行字符連接;
CONCAT_WS('|','A','B');第一個(gè)為分割符
FORMAT();數(shù)字格式化;
FORMAT(99999.99,1);將數(shù)字格式化,并保留1位小數(shù)
LOWER();轉(zhuǎn)換成小寫字符
UPPER();轉(zhuǎn)換成大寫字符
LEFT();獲取左側(cè)字符
RIGHT();獲取右側(cè)字符
LENGTH();獲取字符長(zhǎng)度;
LTRIN();刪除前導(dǎo)字符;
RTRIM();刪除后續(xù)字符
TRIM();刪除前導(dǎo)、后續(xù)字符;
SUBSTRING();字符串的截取;
SUBSTRING('MYSQL',1,2);從第一位中截取2位;(mysql不允許為負(fù)值)
[NOT] LIKE;模式匹配
SELECT * FROM user WHERE username LIKE '%1%%' ESCAPE '1';
REPLACE();替換字符串中的字符
REPLACE('M??Y??SQL','?','*');將M??Y??SQL中的?替換成*;
數(shù)值運(yùn)算符:
CEIL();進(jìn)一去整;
DIV;整數(shù)除法;
FLOOR();舍一去整;
MOD;取余數(shù)(模);
POWER();冪運(yùn)算;
ROUND();四舍五入;ROUND(3.562,1);取一位小數(shù)
TRUNCATE();數(shù)字截??;TRUNCATE(125.89,1);小數(shù)后截取一位;
日期時(shí)間:
NOW();當(dāng)前時(shí)間;
CURDATE();當(dāng)前日期;
CURTIME();當(dāng)前時(shí)間;
DATE_ADD();日期變化;
DATE_ADD('2015-6-23',INTERVAL 365 DAY);
DATEDIFF();倆日期之間的差值;
DATE_FORMAT();進(jìn)行日期格式化;
SELECT DATE_FORMAT('2015-6-26','%m/%d/%Y');
分類: mysql